-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
'skeleton': patch | ||
'@shopify/hydrogen': patch | ||
'@shopify/cli-hydrogen': 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(), | ||
- }, | ||
}, | ||
); | ||
} | ||
``` |