Skip to content

Commit ba9879b

Browse files
authored
Implement information byte in Server Reference ID and other optimizations (#71463)
With this PR, we're adding one extra leading byte to Server Reference IDs (both Server Actions and `"use cache"` functions), to include some static information about the function itself. The information byte has the following format: ``` 0 000000 0 ^type ^arg mask ^rest args ``` The type bit represents if the action is a cache function or not. For cache functions, the type bit is set to `1`. Otherwise, it's `0`. The arg mask bit is used to determine which arguments are used by the function itself, up to 6 arguments. The bit is set to `1` if the argument is used, or being spread or destructured (so it can be indirectly or partially used). The bit is set to `0` otherwise. The rest args bit is used to determine if there's a `...` rest argument in the function signature. If there is, the bit is set to `1`. For example: ```tsx async function foo(a, foo, b, bar, ...baz) { 'use cache'; return a + b; } ``` will have it encoded as `[1][101011][1]`. The first bit is set to `1` because it's a cache function. The second part has `1010` because the only arguments used are `a` and `b`. The subsequent `11` bits are set to `1` because there's a `...baz` argument starting from the 5th. The last bit is set to `1` as well for the same reason. Note: Currently in this PR we don't track if an argument is actually referenced in the function body or not. That will be implemented as a follow-up optimization. Also, the reference ID is currently hex-encoded so there will be exact 2 characters for easier decoding. This encoding might change though. With this extra byte, the client can do some further optimizations. More details can be found in the code comment.
1 parent 35d757b commit ba9879b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+979
-380
lines changed

crates/next-custom-transforms/src/transforms/server_actions.rs

Lines changed: 398 additions & 161 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/* __next_internal_action_entry_do_not_use__ {"ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ import { registerServerReference } from "private-next-rsc-server-reference";
1+
/* __next_internal_action_entry_do_not_use__ {"00ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ import { registerServerReference } from "private-next-rsc-server-reference";
22
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
33
export function foo() {}
44
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
55
ensureServerEntryExports([
66
foo
77
]);
8-
registerServerReference(foo, "ab21efdafbe611287bc25c0462b1e0510d13e48b", null);
8+
registerServerReference(foo, "00ab21efdafbe611287bc25c0462b1e0510d13e48b", null);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/* __next_internal_action_entry_do_not_use__ {"ac840dcaf5e8197cb02b7f3a43c119b7a770b272":"bar"} */ import { registerServerReference } from "private-next-rsc-server-reference";
1+
/* __next_internal_action_entry_do_not_use__ {"00ac840dcaf5e8197cb02b7f3a43c119b7a770b272":"bar"} */ import { registerServerReference } from "private-next-rsc-server-reference";
22
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
33
'use strict';
44
export function bar() {}
55
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
66
ensureServerEntryExports([
77
bar
88
]);
9-
registerServerReference(bar, "ac840dcaf5e8197cb02b7f3a43c119b7a770b272", null);
9+
registerServerReference(bar, "00ac840dcaf5e8197cb02b7f3a43c119b7a770b272", null);

crates/next-custom-transforms/tests/errors/server-actions/server-graph/6/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* __next_internal_action_entry_do_not_use__ {"c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":"default"} */ import { registerServerReference } from "private-next-rsc-server-reference";
1+
/* __next_internal_action_entry_do_not_use__ {"00c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":"default"} */ import { registerServerReference } from "private-next-rsc-server-reference";
22
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
33
export default $$RSC_SERVER_ACTION_0 = ()=>{};
44
var $$RSC_SERVER_ACTION_0;
@@ -10,4 +10,4 @@ import { ensureServerEntryExports } from "private-next-rsc-action-validate";
1010
ensureServerEntryExports([
1111
$$RSC_SERVER_ACTION_0
1212
]);
13-
registerServerReference($$RSC_SERVER_ACTION_0, "c18c215a6b7cdc64bf709f3a714ffdef1bf9651d", null);
13+
registerServerReference($$RSC_SERVER_ACTION_0, "00c18c215a6b7cdc64bf709f3a714ffdef1bf9651d", null);

crates/next-custom-transforms/tests/errors/server-actions/server-graph/8/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/* __next_internal_action_entry_do_not_use__ {"6a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0"} */ import { registerServerReference } from "private-next-rsc-server-reference";
1+
/* __next_internal_action_entry_do_not_use__ {"006a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0"} */ import { registerServerReference } from "private-next-rsc-server-reference";
22
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
33
export const $$RSC_SERVER_ACTION_0 = async function foo() {
44
'use strict';
55
};
6-
const foo = registerServerReference($$RSC_SERVER_ACTION_0, "6a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null);
6+
const foo = registerServerReference($$RSC_SERVER_ACTION_0, "006a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null);
77
const bar = async ()=>{
88
const x = 1;
99
// prettier-ignore

crates/next-custom-transforms/tests/fixture/server-actions/client/1/output.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// app/send.ts
2-
/* __next_internal_action_entry_do_not_use__ {"ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
3-
export var foo = /*#__PURE__*/ createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b", callServer, void 0, findSourceMapURL, "foo");
2+
/* __next_internal_action_entry_do_not_use__ {"7fab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
3+
export var foo = /*#__PURE__*/ createServerReference("7fab21efdafbe611287bc25c0462b1e0510d13e48b", callServer, void 0, findSourceMapURL, "foo");

crates/next-custom-transforms/tests/fixture/server-actions/client/3/output.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/next-custom-transforms/tests/fixture/server-actions/client/4/output.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/next-custom-transforms/tests/fixture/server-actions/client/5/output.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)