Skip to content
Source
Sign inSign up
TanStack/router

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 intomain fromstart-plugin-core/server-fn-stable-idsopened

Showing 24 changed files with 50 additions and 9 deletions (loading 21).

x
0 of 24 viewed, 24 left
9 additions and 0 deletions.changeset/start-server-fn-stable-ids.mdAdded
View file
@@ -0,0 +1,9 @@
+---
+'@tanstack/start-plugin-core': minor
+---
+​
+Default production server function ids are shorter (22 characters, a hash of the file and the function's variable name), and a custom id must be one URL path segment.
+​
+- **Every default production server function id changes.** Update CDN rules, WAF allowlists, analytics keyed by id and native clients that call `/_serverFn/<id>` directly. Browser tabs loaded before a deploy fail their next server-function call after it: plan a reload or a blue/green window. Ids from a custom `serverFns.generateFunctionId` stay the same.
+- `serverFns.generateFunctionId` receives the same `functionName` as before, the generated handler name (`greetUser_createServerFn_handler`, then `_1`, `_2` for a name the file repeats). Characters of a name outside `A-Z`, `a-z`, `0-9`, `_` and `$` (a non-ASCII variable name) now become `_`, so an id that embeds the name is a valid URL path segment.
+- An id a custom `serverFns.generateFunctionId` returns must be one URL path segment: ASCII letters, digits, `-`, `.`, `_`, `~` and `$`, not `.` or `..` (nor an empty string). Any other id fails the build with an error naming the file, the function and the id. Most such ids already failed at runtime, since the client puts the id in the URL unencoded: a `#` or `?` ended the path and a `/` cut the id short, so the call silently ran another server function of the file or none, and a space, control or non-ASCII character reached the server percent-encoded and matched no function. Ids with `%` or with other characters URLs allow (such as `:`, `@`, `+`, `=`, `,`, `;`, `!`, `*`, `(`, `)`, `&`) did reach the handler on Node, but proxies and servers may decode or rewrite them, so they now fail the build too: hash the inputs instead (the documented example does).
9 additions and 2 deletionsdocs/start/framework/react/guide/server-functions.mdModified
View file
@@ -418,14 +418,21 @@
​
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.
​
-By default, IDs are SHA256 hashes of the same seed to keep bundles compact and avoid leaking file paths.
+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.
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.
​
Customization:
​
You can customize function ID generation for the production build by providing a `generateFunctionId` function when configuring the TanStack Start build tool plugin.
​
-Prefer deterministic inputs (filename + functionName) so IDs remain stable between builds.
+The generator receives:
+​
+- `filename`: the file's path relative to the project root, e.g. `src/routes/users.tsx`.
+- `functionName`: the server function's variable name followed by `_createServerFn_handler`, e.g. `getUser_createServerFn_handler`. Characters of the name other than ASCII letters, digits, `_` and `$` become `_`, so `functionName` contains only those. When this gives more than one server function of a file the same name (a repeated variable name, or names like `café` and `caf_`, which both give `caf__createServerFn_handler`), the first keeps it 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`, and `caf_` gets `caf_createServerFn_handler_1`).