Skip to content

Commit

Permalink
add session utility for dirty state. Ensure this dirty state is set t…
Browse files Browse the repository at this point in the history
…o true whenever session is changed. And set to false whenever session is commited. Then in server code, check if session is dirty, if its dirty, set cookie with sessin.commit.

move commit logic to template instead

update all the examples

bring back location header

add changeset
  • Loading branch information
michenly committed May 30, 2024
1 parent e29ecf0 commit 9e7e023
Show file tree
Hide file tree
Showing 54 changed files with 477 additions and 421 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-wombats-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

`customerAccount` no longer commit session automatically in any situation.
65 changes: 65 additions & 0 deletions .changeset/red-lamps-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
'skeleton': patch
---

Remove manual setting of session in headers and recommend setting it in server after response is created.

Step 1: Add `isPending` implementation in session

```diff
// in app/lib/session.ts
export class AppSession implements HydrogenSession {
+ public isPending = false;

get unset() {
+ this.isPending = true;
return this.#session.unset;
}

get set() {
+ this.isPending = true;
return this.#session.set;
}

commit() {
+ this.isPending = false;
return this.#sessionStorage.commitSession(this.#session);
}
}
```

Step 2: update response header if `session.isPending` is true

```diff
// in server.ts
export default {
async fetch(request: Request): Promise<Response> {
try {
const response = await handleRequest(request);

+ if (session.isPending) {
+ response.headers.set('Set-Cookie', await session.commit());
+ }

return response;
} catch (error) {
...
}
},
};
```

Step 3: remove setting cookie with session.commit()

```diff
// in route files
export async function loader({context}: LoaderFunctionArgs) {
return json({},
- {
- headers: {
- 'Set-Cookie': await context.session.commit(),
- },
},
);
}
```
5 changes: 5 additions & 0 deletions .changeset/six-pumas-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-hydrogen': patch
---

skeleton template was updated to do session commit in server call
35 changes: 14 additions & 21 deletions examples/b2b/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,21 @@ export async function loader({context}: LoaderFunctionArgs) {
},
});

return defer(
{
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
shop: getShopAnalytics({
storefront,
publicStorefrontId: env.PUBLIC_STOREFRONT_ID,
}),
consent: {
checkoutDomain: env.PUBLIC_CHECKOUT_DOMAIN,
storefrontAccessToken: env.PUBLIC_STOREFRONT_API_TOKEN,
},
},
{
headers: {
'Set-Cookie': await context.session.commit(),
},
return defer({
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
shop: getShopAnalytics({
storefront,
publicStorefrontId: env.PUBLIC_STOREFRONT_ID,
}),
consent: {
checkoutDomain: env.PUBLIC_CHECKOUT_DOMAIN,
storefrontAccessToken: env.PUBLIC_STOREFRONT_API_TOKEN,
},
);
});
}

export function Layout({children}: {children?: React.ReactNode}) {
Expand Down
9 changes: 1 addition & 8 deletions examples/b2b/app/routes/b2blocations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ export async function loader({context}: LoaderFunctionArgs) {

const modalOpen = Boolean(company) && !companyLocationId;

return defer(
{company, companyLocationId, modalOpen},
{
headers: {
'Set-Cookie': await context.session.commit(),
},
},
);
return defer({company, companyLocationId, modalOpen});
}

export default function CartRoute() {
Expand Down
4 changes: 4 additions & 0 deletions examples/b2b/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export default {

const response = await handleRequest(request);

if (session.isPending) {
response.headers.set('Set-Cookie', await session.commit());
}

if (response.status === 404) {
/**
* Check for redirects only when there's a 404 from the app.
Expand Down
35 changes: 14 additions & 21 deletions examples/classic-remix/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,21 @@ export async function loader({context}: LoaderFunctionArgs) {
},
});

return defer(
{
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
shop: getShopAnalytics({
storefront,
publicStorefrontId: env.PUBLIC_STOREFRONT_ID,
}),
consent: {
checkoutDomain: env.PUBLIC_CHECKOUT_DOMAIN,
storefrontAccessToken: env.PUBLIC_STOREFRONT_API_TOKEN,
},
},
{
headers: {
'Set-Cookie': await context.session.commit(),
},
return defer({
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
shop: getShopAnalytics({
storefront,
publicStorefrontId: env.PUBLIC_STOREFRONT_ID,
}),
consent: {
checkoutDomain: env.PUBLIC_CHECKOUT_DOMAIN,
storefrontAccessToken: env.PUBLIC_STOREFRONT_API_TOKEN,
},
);
});
}

export function Layout({children}: {children?: React.ReactNode}) {
Expand Down
4 changes: 4 additions & 0 deletions examples/classic-remix/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export default {

const response = await handleRequest(request);

if (session.isPending) {
response.headers.set('Set-Cookie', await session.commit());
}

if (response.status === 404) {
/**
* Check for redirects only when there's a 404 from the app.
Expand Down
2 changes: 0 additions & 2 deletions examples/custom-cart-method/app/routes/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ export async function action({request, context}: ActionFunctionArgs) {
headers.set('Location', redirectTo);
}

headers.append('Set-Cookie', await context.session.commit());

return json(
{
cart: cartResult,
Expand Down
4 changes: 4 additions & 0 deletions examples/custom-cart-method/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ export default {

const response = await handleRequest(request);

if (session.isPending) {
response.headers.set('Set-Cookie', await session.commit());
}

if (response.status === 404) {
/**
* Check for redirects only when there's a 404 from the app.
Expand Down
35 changes: 14 additions & 21 deletions examples/gtm/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,21 @@ export async function loader({context}: LoaderFunctionArgs) {
},
});

return defer(
{
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
shop: getShopAnalytics({
storefront,
publicStorefrontId: env.PUBLIC_STOREFRONT_ID,
}),
consent: {
checkoutDomain: env.PUBLIC_CHECKOUT_DOMAIN,
storefrontAccessToken: env.PUBLIC_STOREFRONT_API_TOKEN,
},
},
{
headers: {
'Set-Cookie': await context.session.commit(),
},
return defer({
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
shop: getShopAnalytics({
storefront,
publicStorefrontId: env.PUBLIC_STOREFRONT_ID,
}),
consent: {
checkoutDomain: env.PUBLIC_CHECKOUT_DOMAIN,
storefrontAccessToken: env.PUBLIC_STOREFRONT_API_TOKEN,
},
);
});
}

export function Layout({children}: {children?: React.ReactNode}) {
Expand Down
1 change: 0 additions & 1 deletion examples/legacy-customer-account-flow/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ async function validateCustomerAccessToken(

if (customerAccessTokenExpired) {
session.unset('customerAccessToken');
headers.append('Set-Cookie', await session.commit());
} else {
isLoggedIn = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,7 @@ export async function action({request, context}: ActionFunctionArgs) {
);
}

return json(
{error: null, customer: updated.customerUpdate?.customer},
{
headers: {
'Set-Cookie': await session.commit(),
},
},
);
return json({error: null, customer: updated.customerUpdate?.customer});
} catch (error: any) {
return json({error: error.message, customer: null}, {status: 400});
}
Expand Down
12 changes: 2 additions & 10 deletions examples/legacy-customer-account-flow/app/routes/account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ export async function loader({request, context}: LoaderFunctionArgs) {
if (!isLoggedIn) {
if (isPrivateRoute || isAccountHome) {
session.unset('customerAccessToken');
return redirect('/account/login', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account/login');
} else {
// public subroute such as /account/login...
return json({
Expand Down Expand Up @@ -67,11 +63,7 @@ export async function loader({request, context}: LoaderFunctionArgs) {
// eslint-disable-next-line no-console
console.error('There was a problem loading account', error);
session.unset('customerAccessToken');
return redirect('/account/login', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account/login');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ export async function action({request, context, params}: ActionFunctionArgs) {
}
session.set('customerAccessToken', customerAccessToken);

return redirect('/account', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account');
} catch (error: unknown) {
if (error instanceof Error) {
return json({error: error.message}, {status: 400});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ export async function action({request, context}: ActionFunctionArgs) {
const {customerAccessToken} = customerAccessTokenCreate;
session.set('customerAccessToken', customerAccessToken);

return redirect('/account', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account');
} catch (error: unknown) {
if (error instanceof Error) {
return json({error: error.message}, {status: 400});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ export async function action({request, context}: ActionFunctionArgs) {
return json({error: 'Method not allowed'}, {status: 405});
}

return redirect('/', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/');
}

export default function Logout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export async function action({request, context}: ActionFunctionArgs) {
{
status: 302,
headers: {
'Set-Cookie': await session.commit(),
Location: '/account',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ export async function action({request, context, params}: ActionFunctionArgs) {
}
session.set('customerAccessToken', customerReset.customerAccessToken);

return redirect('/account', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account');
} catch (error: unknown) {
if (error instanceof Error) {
return json({error: error.message}, {status: 400});
Expand Down
2 changes: 0 additions & 2 deletions examples/legacy-customer-account-flow/app/routes/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ export async function action({request, context}: ActionFunctionArgs) {
headers.set('Location', redirectTo);
}

headers.append('Set-Cookie', await context.session.commit());

return json(
{
cart: cartResult,
Expand Down
4 changes: 4 additions & 0 deletions examples/legacy-customer-account-flow/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export default {

const response = await handleRequest(request);

if (session.isPending) {
response.headers.set('Set-Cookie', await session.commit());
}

if (response.status === 404) {
/**
* Check for redirects only when there's a 404 from the app.
Expand Down
Loading

0 comments on commit 9e7e023

Please sign in to comment.