-
Notifications
You must be signed in to change notification settings - Fork 27.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement MutableRequestCookies in server entries (#48847)
Similar to #47922 but based off the latest server implementation and #48626: > This PR implements the MutableRequestCookies instance for cookies() based on the current async context, so we can allow setting cookies in certain places such as Server Functions and Route handlers. Note that to support Route Handlers, we need to also implement the logic of merging Response's Set-Cookie header and the cookies() mutations, hence it's not included in this PR. > > fix [NEXT-942](https://linear.app/vercel/issue/NEXT-942) This PR also adds the same support for Custom Routes. cc @styfle. fix NEXT-942, fix NEXT-941.
- Loading branch information
Showing
12 changed files
with
320 additions
and
123 deletions.
There are no files selected for viewing
42 changes: 3 additions & 39 deletions
42
packages/next/src/client/components/action-async-storage.ts
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 |
---|---|---|
@@ -1,47 +1,11 @@ | ||
import type { AsyncLocalStorage } from 'async_hooks' | ||
import { createAsyncLocalStorage } from './async-local-storage' | ||
|
||
export interface ActionStore { | ||
readonly isAction: boolean | ||
readonly isAction?: boolean | ||
readonly isAppRoute?: boolean | ||
} | ||
|
||
export type ActionAsyncStorage = AsyncLocalStorage<ActionStore> | ||
|
||
let createAsyncLocalStorage: () => ActionAsyncStorage | ||
if (process.env.NEXT_RUNTIME === 'edge') { | ||
createAsyncLocalStorage = () => { | ||
let store: ActionStore | undefined | ||
return { | ||
disable() { | ||
throw new Error( | ||
'Invariant: AsyncLocalStorage accessed in runtime where it is not available' | ||
) | ||
}, | ||
getStore() { | ||
return store | ||
}, | ||
async run<R>(s: ActionStore, fn: () => R): Promise<R> { | ||
store = s | ||
try { | ||
return await fn() | ||
} finally { | ||
store = undefined | ||
} | ||
}, | ||
exit() { | ||
throw new Error( | ||
'Invariant: AsyncLocalStorage accessed in runtime where it is not available' | ||
) | ||
}, | ||
enterWith() { | ||
throw new Error( | ||
'Invariant: AsyncLocalStorage accessed in runtime where it is not available' | ||
) | ||
}, | ||
} as ActionAsyncStorage | ||
} | ||
} else { | ||
createAsyncLocalStorage = | ||
require('./async-local-storage').createAsyncLocalStorage | ||
} | ||
|
||
export const actionAsyncStorage: ActionAsyncStorage = createAsyncLocalStorage() |
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
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
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
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
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
Oops, something went wrong.