refactor(start-plugin-core): shorter default server-fn ids; a custom id must be one URL path segment #8858
OpenSheraff(GitHub profile, opens in a new tab)wants to merge 5 commits into
main fromstart-plugin-core/server-fn-stable-idsopened github-actions[bot](GitHub profile, opens in a new tab) added labelsdocumentation
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: start-plugin-core
- @@ -418,11 +418,11 @@418418419419 Server functions are addressed by a generated, stable function ID under the hood. These IDs are embedded into the client/SSR builds and used by the server to locate and import the correct module at runtime.420420421-By default, IDs are SHA256 hashes of the same seed to keep bundles compact and avoid leaking file paths.421+By default, IDs are short hashes (22 characters, from SHA256) of the file and function name, to keep bundles compact and avoid leaking file paths.422422 If two server functions end up with the same ID (including when using a custom generator), the system de-duplicates by appending an incrementing suffix like `_1`, `_2`, etc.423423424424 Customization:425425426426 You can customize function ID generation for the production build by providing a `generateFunctionId` function when configuring the TanStack Start build tool plugin.427427428-Prefer deterministic inputs (filename + functionName) so IDs remain stable between builds.428+The generator receives `filename` (the file's path relative to the project root) and `functionName` (the server function's export or variable name, e.g. `getUser`). Return `undefined` to use the default ID. Prefer deterministic inputs (filename + functionName) so IDs remain stable between builds.@@ -251,3 +252,11 @@251252 }252253 functionNameSet.add(functionName)253254255+ // The stable key names the server fn independently of the compiled256+ // handler name: the variable name, with `#n` for repeats in the file257+ const keyCount = stableKeyCount.get(existingVariableName) ?? 0258+ stableKeyCount.set(existingVariableName, keyCount + 1)259+ const stableKey =260+ keyCount === 0261+ ? existingVariableName262+ : `${existingVariableName}#${keyCount}`
Sheraff(GitHub profile, opens in a new tab) changed the title
- docs/start/framework/react/guide/server-functions.md line 421Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -418,4 +418,4 @@418418419419 Server functions are addressed by a generated, stable function ID under the hood. These IDs are embedded into the client/SSR builds and used by the server to locate and import the correct module at runtime.420420421-By default, IDs are SHA256 hashes of the same seed to keep bundles compact and avoid leaking file paths.421+By default, IDs are short hashes (22 characters, from SHA256) of the file and function name, to keep bundles compact and avoid leaking file paths.docs/start/framework/react/guide/server-functions.md line 431OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -418,11 +418,14 @@418418419419 Server functions are addressed by a generated, stable function ID under the hood. These IDs are embedded into the client/SSR builds and used by the server to locate and import the correct module at runtime.420420421-By default, IDs are SHA256 hashes of the same seed to keep bundles compact and avoid leaking file paths.421+By default, IDs are short hashes (22 characters, from SHA256) of the file and function name, to keep bundles compact and avoid leaking file paths.422422 If two server functions end up with the same ID (including when using a custom generator), the system de-duplicates by appending an incrementing suffix like `_1`, `_2`, etc.423423424424 Customization:425425426426 You can customize function ID generation for the production build by providing a `generateFunctionId` function when configuring the TanStack Start build tool plugin.427427428-Prefer deterministic inputs (filename + functionName) so IDs remain stable between builds.428+The generator receives:429+430+`filename`: the file's path relative to the project root, e.g. `src/routes/users.tsx`.431+`functionName`: the server function's variable name followed by `_createServerFn_handler`, e.g. `getUser_createServerFn_handler`. When a file uses the same name for more than one server function, the later ones, in source order, get `_1`, `_2`, … appended. `functionName` contains only ASCII letters, digits, `_` and `$`; any other character of the name becomes `_`.packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts lines 250 to 277Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -253,5 +257,21 @@257+ const generatorFunctionName = allocateHandlerName(258+ existingVariableName.replace(/[^A-Za-z0-9_$]/g, '_'),259+ generatorFunctionNameSet,260+ )261+262+ // The stable key seeds the default id: the variable name, with `#n` for263+ // repeats in the file264+ const keyCount = stableKeyCount.get(existingVariableName) ?? 0265+ stableKeyCount.set(existingVariableName, keyCount + 1)266+ const stableKey =267+ keyCount === 0268+ ? existingVariableName269+ : `${existingVariableName}#${keyCount}`253270254271 // Generate function ID using pre-computed relative filename255272 const functionId = context.generateFunctionId({256273 filename: relativeFilename,257274 functionName,275+ generatorFunctionName,276+ stableKey,277+ variableName: existingVariableName,
- docs/start/framework/react/guide/server-functions.md line 431OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -418,11 +418,14 @@418418419419 Server functions are addressed by a generated, stable function ID under the hood. These IDs are embedded into the client/SSR builds and used by the server to locate and import the correct module at runtime.420420421-By default, IDs are SHA256 hashes of the same seed to keep bundles compact and avoid leaking file paths.421+By default, IDs are short hashes (22 characters, from SHA256) of the file and function name, to keep bundles compact and avoid leaking file paths.422422 If two server functions end up with the same ID (including when using a custom generator), the system de-duplicates by appending an incrementing suffix like `_1`, `_2`, etc.423423424424 Customization:425425426426 You can customize function ID generation for the production build by providing a `generateFunctionId` function when configuring the TanStack Start build tool plugin.427427428-Prefer deterministic inputs (filename + functionName) so IDs remain stable between builds.428+The generator receives:429+430+`filename`: the file's path relative to the project root, e.g. `src/routes/users.tsx`.431+`functionName`: the server function's variable name followed by `_createServerFn_handler`, e.g. `getUser_createServerFn_handler`. When a file uses the same name for more than one server function, the first keeps that name and the later ones, in source order, get `_1`, `_2`, … appended; their name also loses its leading underscores and each run of underscores becomes one (a second `_x` gets `x_createServerFn_handler_1`). `functionName` contains only ASCII letters, digits, `_` and `$`; any other character of the name becomes `_`.
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 09b7f3a475979c2faf1f2782ae8028c71dc8de0e
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR