-
Notifications
You must be signed in to change notification settings - Fork 26.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Server Reference being double registered (#61244)
When we apply `createActionProxy` twice to the same value, it currently errors because we can't override the ID of it. This PR makes sure that 1) when the value already have an ID defined, we skip setting it again; 2) all the action IDs are being tracked even for duplicated ones. Note that it's technically impossible to "detect" the duplication here (via static analyzation) and only set it once. For example: ```ts 'use server' export async function foo () {} export const bar = a_value_we_dont_know_yet ? foo : async () => {} ``` So, the compiler will always wrap `createActionProxy` for every exported value and assume they're different Server Actions (hence different IDs). With this fix, if we find that it's already defined before, we just return the defined reference as they're strictly identical so the ID does't matter. Closes #54655, closes #61183. Closes NEXT-2264
- Loading branch information
Showing
9 changed files
with
49 additions
and
9 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
2 changes: 1 addition & 1 deletion
2
...xt-swc/crates/next-custom-transforms/tests/errors/server-actions/server-graph/6/output.js
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
2 changes: 1 addition & 1 deletion
2
...s/next-swc/crates/next-custom-transforms/tests/fixture/server-actions/server/15/output.js
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
2 changes: 1 addition & 1 deletion
2
...s/next-swc/crates/next-custom-transforms/tests/fixture/server-actions/server/17/output.js
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
2 changes: 1 addition & 1 deletion
2
...s/next-swc/crates/next-custom-transforms/tests/fixture/server-actions/server/22/output.js
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
8 changes: 8 additions & 0 deletions
8
...es/next-swc/crates/next-custom-transforms/tests/fixture/server-actions/server/29/input.js
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,8 @@ | ||
'use server' | ||
|
||
export const dec = async (value) => { | ||
return value - 1 | ||
} | ||
|
||
// Test case for https://github.com/vercel/next.js/issues/54655 | ||
export default dec |
15 changes: 15 additions & 0 deletions
15
...s/next-swc/crates/next-custom-transforms/tests/fixture/server-actions/server/29/output.js
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,15 @@ | ||
/* __next_internal_action_entry_do_not_use__ {"28baf972d345b86b747ad0df73d75a0088a42214":"dec","6d53ce510b2e36499b8f56038817b9bad86cabb4":"$$ACTION_0","c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":"default"} */ import { createActionProxy } from "private-next-rsc-action-proxy"; | ||
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; | ||
export const dec = createActionProxy("6d53ce510b2e36499b8f56038817b9bad86cabb4", $$ACTION_0); | ||
export async function $$ACTION_0(value) { | ||
return value - 1; | ||
} | ||
// Test case for https://github.com/vercel/next.js/issues/54655 | ||
export default dec; | ||
import { ensureServerEntryExports } from "private-next-rsc-action-validate"; | ||
ensureServerEntryExports([ | ||
dec, | ||
dec | ||
]); | ||
createActionProxy("28baf972d345b86b747ad0df73d75a0088a42214", dec); | ||
createActionProxy("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d", dec); |
11 changes: 11 additions & 0 deletions
11
packages/next/src/build/webpack/loaders/next-flight-loader/action-proxy.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,6 +1,17 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { registerServerReference } from 'react-server-dom-webpack/server.edge' | ||
|
||
const SERVER_REFERENCE_TAG = Symbol.for('react.server.reference') | ||
|
||
function isServerReference(reference: any) { | ||
return reference && reference.$$typeof === SERVER_REFERENCE_TAG | ||
} | ||
|
||
export function createActionProxy(id: string, action: any) { | ||
// Avoid registering the same action twice | ||
if (isServerReference(action)) { | ||
return action | ||
} | ||
|
||
return registerServerReference(action, id, null) | ||
} |
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