From ddeca5cabe7862d789418855fd6c53b5842f73a9 Mon Sep 17 00:00:00 2001 From: Sheraff Date: Tue, 3 Nov 2026 17:54:50 +0100 Subject: [PATCH 1/5] refactor(start-plugin-core): derive server-fn ids from the stable key, with 22-character defaults `serverFns.generateFunctionId` now receives the server function's stable name (`greetUser`) as `functionName` instead of the generated handler name (`greetUser_createServerFn_handler`). Repeats of the same variable name in one file get `#1`, `#2`, ... Default production ids are the first 22 base64url characters of the SHA256 of `#` instead of the full 64-character hex digest. Split out of #8817, which derives ids the same way. Co-Authored-By: Claude Opus 5.5 --- .changeset/start-server-fn-stable-ids.md | 8 ++ .../framework/react/guide/server-functions.md | 4 +- .../server-functions/start-mode-config.ts | 4 +- .../server-functions/vite.config.ts | 4 +- e2e/vue-start/server-functions/vite.config.ts | 4 +- .../src/start-compiler/compiler.ts | 13 ++- .../start-compiler/handleCreateServerFn.ts | 11 +++ .../src/start-compiler/types.ts | 10 +- .../createServerFn/createServerFn.test.ts | 99 +++++++++++-------- .../client/createServerFnDestructured.tsx | 30 ++---- .../createServerFnDestructuredRename.tsx | 16 +-- .../client/createServerFnStarImport.tsx | 24 +---- .../client/createServerFnValidator.tsx | 4 +- .../snapshots/client/factory.tsx | 8 +- .../snapshots/client/isomorphic-fns.tsx | 8 +- .../createServerFnDestructured.tsx | 32 ++---- .../createServerFnDestructuredRename.tsx | 16 +-- .../createServerFnStarImport.tsx | 24 +---- .../server-caller/createServerFnValidator.tsx | 6 +- .../snapshots/server-caller/factory.tsx | 8 +- .../server-caller/isomorphic-fns.tsx | 10 +- .../createServerFnDestructured.tsx | 14 +-- .../createServerFnDestructuredRename.tsx | 8 +- .../createServerFnStarImport.tsx | 8 +- .../createServerFnValidator.tsx | 2 +- .../snapshots/server-provider/factory.tsx | 8 +- .../server-provider/isomorphic-fns.tsx | 4 +- 27 files changed, 161 insertions(+), 226 deletions(-) create mode 100644 .changeset/start-server-fn-stable-ids.md diff --git a/.changeset/start-server-fn-stable-ids.md b/.changeset/start-server-fn-stable-ids.md new file mode 100644 index 00000000000..695f2386f11 --- /dev/null +++ b/.changeset/start-server-fn-stable-ids.md @@ -0,0 +1,8 @@ +--- +'@tanstack/start-plugin-core': minor +--- + +Server function ids are now derived from the function's stable name, and default production ids are shorter (22 characters). + +- **Every production server function id changes.** Update CDN rules, WAF allowlists, analytics keyed by id and native clients that call `/_serverFn/` directly. Browser tabs loaded before a deploy fail their next server-function call after it: plan a reload or a blue/green window. +- **`serverFns.generateFunctionId` receives the function's stable name** (`greetUser`) as `functionName`, not the generated handler name (`greetUser_createServerFn_handler`). Custom generators keyed on the old names must be updated. diff --git a/docs/start/framework/react/guide/server-functions.md b/docs/start/framework/react/guide/server-functions.md index 68a6b8ac72d..8d083871880 100644 --- a/docs/start/framework/react/guide/server-functions.md +++ b/docs/start/framework/react/guide/server-functions.md @@ -418,14 +418,14 @@ Handle request cancellation with `AbortSignal` for long-running operations. 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) 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. Please note that this customization is **experimental** and subject to change. diff --git a/e2e/react-start/server-functions/start-mode-config.ts b/e2e/react-start/server-functions/start-mode-config.ts index ba75b59693f..abf46563ba3 100644 --- a/e2e/react-start/server-functions/start-mode-config.ts +++ b/e2e/react-start/server-functions/start-mode-config.ts @@ -1,9 +1,9 @@ const FUNCTION_IDS: Record> = { 'src/routes/submit-post-formdata.tsx': { - greetUser_createServerFn_handler: 'submit-post-formdata-greetUser', + greetUser: 'submit-post-formdata-greetUser', }, 'src/routes/formdata-redirect/index.tsx': { - greetUser_createServerFn_handler: 'formdata-redirect-greetUser', + greetUser: 'formdata-redirect-greetUser', }, } diff --git a/e2e/solid-start/server-functions/vite.config.ts b/e2e/solid-start/server-functions/vite.config.ts index fc622bbfb02..d063548aaca 100644 --- a/e2e/solid-start/server-functions/vite.config.ts +++ b/e2e/solid-start/server-functions/vite.config.ts @@ -4,8 +4,8 @@ import viteSolid from 'vite-plugin-solid' import tailwindcss from '@tailwindcss/vite' const FUNCTIONS_WITH_CONSTANT_ID = [ - 'src/routes/submit-post-formdata.tsx/greetUser_createServerFn_handler', - 'src/routes/formdata-redirect/index.tsx/greetUser_createServerFn_handler', + 'src/routes/submit-post-formdata.tsx/greetUser', + 'src/routes/formdata-redirect/index.tsx/greetUser', ] export default defineConfig({ diff --git a/e2e/vue-start/server-functions/vite.config.ts b/e2e/vue-start/server-functions/vite.config.ts index 362cf4e1221..97b2fe2328c 100644 --- a/e2e/vue-start/server-functions/vite.config.ts +++ b/e2e/vue-start/server-functions/vite.config.ts @@ -5,8 +5,8 @@ import vueJsx from '@vitejs/plugin-vue-jsx' import tailwindcss from '@tailwindcss/vite' const FUNCTIONS_WITH_CONSTANT_ID = [ - 'src/routes/submit-post-formdata.tsx/greetUser_createServerFn_handler', - 'src/routes/formdata-redirect/index.tsx/greetUser_createServerFn_handler', + 'src/routes/submit-post-formdata.tsx/greetUser', + 'src/routes/formdata-redirect/index.tsx/greetUser', ] export default defineConfig({ diff --git a/packages/start-plugin-core/src/start-compiler/compiler.ts b/packages/start-plugin-core/src/start-compiler/compiler.ts index 64dc24df343..e6c79439336 100644 --- a/packages/start-plugin-core/src/start-compiler/compiler.ts +++ b/packages/start-plugin-core/src/start-compiler/compiler.ts @@ -629,11 +629,12 @@ export class StartCompiler { /** * Generates a unique function ID for a server function. * In dev mode, uses a base64-encoded JSON with file path and export name. - * In build mode, uses SHA256 hash or custom generator. + * In build mode, uses a 22-character SHA256 hash or custom generator. */ private generateFunctionId(opts: { filename: string functionName: string + stableKey: string extractedFilename: string }): string { if (this.mode === 'dev') { @@ -660,7 +661,7 @@ export class StartCompiler { } // Production build: use custom generator or hash - const entryId = `${opts.filename}--${opts.functionName}` + const entryId = `${opts.filename}#${opts.stableKey}` let functionId = this.entryIdToFunctionId.get(entryId) if (functionId === undefined) { const knownFn = Object.values(this.options.getKnownServerFns()).find( @@ -676,11 +677,15 @@ export class StartCompiler { if (this.options.generateFunctionId) { functionId ??= this.options.generateFunctionId({ filename: opts.filename, - functionName: opts.functionName, + functionName: opts.stableKey, }) } if (!functionId) { - functionId = crypto.createHash('sha256').update(entryId).digest('hex') + functionId = crypto + .createHash('sha256') + .update(entryId) + .digest('base64url') + .slice(0, 22) } // Deduplicate in case the generated id conflicts with an existing id if (this.functionIds.has(functionId)) { diff --git a/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts b/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts index dd441bcc243..ea6815b7f41 100644 --- a/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts +++ b/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts @@ -209,6 +209,7 @@ export function handleCreateServerFn( // Track function names to ensure uniqueness within this file const functionNameSet = new Set() + const stableKeyCount = new Map() const exportNames = new Set() const serverFnsById: Record = {} @@ -251,10 +252,20 @@ export function handleCreateServerFn( } functionNameSet.add(functionName) + // The stable key names the server fn independently of the compiled + // handler name: the variable name, with `#n` for repeats in the file + const keyCount = stableKeyCount.get(existingVariableName) ?? 0 + stableKeyCount.set(existingVariableName, keyCount + 1) + const stableKey = + keyCount === 0 + ? existingVariableName + : `${existingVariableName}#${keyCount}` + // Generate function ID using pre-computed relative filename const functionId = context.generateFunctionId({ filename: relativeFilename, functionName, + stableKey, extractedFilename, }) diff --git a/packages/start-plugin-core/src/start-compiler/types.ts b/packages/start-plugin-core/src/start-compiler/types.ts index 0dde184961a..b31f7872748 100644 --- a/packages/start-plugin-core/src/start-compiler/types.ts +++ b/packages/start-plugin-core/src/start-compiler/types.ts @@ -89,16 +89,20 @@ export interface ServerFn { */ export type GenerateFunctionIdFn = (opts: { filename: string + /** Name of the extracted handler export. */ functionName: string + /** The server fn's variable name, with `#n` for repeats in the same file. */ + stableKey: string extractedFilename: string }) => string /** * Optional version that allows returning undefined to use default ID generation. */ -export type GenerateFunctionIdFnOptional = ( - opts: Omit[0], 'extractedFilename'>, -) => string | undefined +export type GenerateFunctionIdFnOptional = (opts: { + filename: string + functionName: string +}) => string | undefined /** * Function type for generating replacement code for server functions. diff --git a/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts b/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts index 29557e7b9c2..67dd019dace 100644 --- a/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts +++ b/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts @@ -116,9 +116,7 @@ describe('createServerFn compiles correctly', async () => { "import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' const myServerFn = createServerFn().handler( - createClientRpc( - '2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b', - ), + createClientRpc('WYpJIMNQF5bXNZqddn4S08'), ) " `) @@ -190,9 +188,7 @@ describe('createServerFn compiles correctly', async () => { "import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' const myServerFn = createServerFn().handler( - createClientRpc( - '2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b', - ), + createClientRpc('WYpJIMNQF5bXNZqddn4S08'), ) " `) @@ -203,9 +199,7 @@ describe('createServerFn compiles correctly', async () => { "import { createSsrRpc } from '@tanstack/react-start/ssr-rpc' import { createServerFn } from '@tanstack/react-start' const myServerFn = createServerFn().handler( - createSsrRpc( - '2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b', - ), + createSsrRpc('WYpJIMNQF5bXNZqddn4S08'), ) " `) @@ -219,11 +213,7 @@ describe('createServerFn compiles correctly', async () => { return 'hello from the server' } const myServerFn_createServerFn_handler = createServerRpc( - { - id: '2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b', - name: 'myServerFn', - filename: 'src/test.ts', - }, + { id: 'WYpJIMNQF5bXNZqddn4S08', name: 'myServerFn', filename: 'src/test.ts' }, (opts) => myServerFn.__executeServer(opts), ) const myServerFn = createServerFn().handler( @@ -255,9 +245,7 @@ describe('createServerFn compiles correctly', async () => { "import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' export const getUsersFn = createServerFn().handler( - createClientRpc( - 'a78c63d4bb3c0b10a8b70902c73611fbf0b9229e0807b065c03c939f9c0ce100', - ), + createClientRpc('hUsF8j58VORzR8Zh0TWYHu'), ) " `) @@ -285,9 +273,7 @@ describe('createServerFn compiles correctly', async () => { import { createServerFn } from '@tanstack/react-start' import { getUsers } from './server' export const getUsersFn = createServerFn().handler( - createClientRpc( - 'a78c63d4bb3c0b10a8b70902c73611fbf0b9229e0807b065c03c939f9c0ce100', - ), + createClientRpc('hUsF8j58VORzR8Zh0TWYHu'), ) console.log(getUsers) " @@ -357,14 +343,10 @@ describe('createServerFn compiles correctly', async () => { "import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' export const exportedFn = createServerFn().handler( - createClientRpc( - 'c306c96e9256c7604f2a6022c4c94eb89f863274c022bc45b03970f067ea9864', - ), + createClientRpc('2tiakNpA4QDPhHt7gcEQxW'), ) const nonExportedFn = createServerFn().handler( - createClientRpc( - 'f4403dc0b18e216dfe0a9711cab028bc1b9768175daa9236d7115e29c99d76c2', - ), + createClientRpc('p5iUR0EOmj-YLvWU2l_Tw6'), ) " `) @@ -382,14 +364,10 @@ describe('createServerFn compiles correctly', async () => { "import { createSsrRpc } from '@tanstack/react-start/ssr-rpc' import { createServerFn } from '@tanstack/react-start' export const exportedFn = createServerFn().handler( - createSsrRpc( - 'c306c96e9256c7604f2a6022c4c94eb89f863274c022bc45b03970f067ea9864', - ), + createSsrRpc('2tiakNpA4QDPhHt7gcEQxW'), ) const nonExportedFn = createServerFn().handler( - createSsrRpc( - 'f4403dc0b18e216dfe0a9711cab028bc1b9768175daa9236d7115e29c99d76c2', - ), + createSsrRpc('p5iUR0EOmj-YLvWU2l_Tw6'), ) " `) @@ -408,11 +386,7 @@ describe('createServerFn compiles correctly', async () => { import { createServerFn } from '@tanstack/react-start' const exportedVar = 'exported' const exportedFn_createServerFn_handler = createServerRpc( - { - id: 'c306c96e9256c7604f2a6022c4c94eb89f863274c022bc45b03970f067ea9864', - name: 'exportedFn', - filename: 'src/test.ts', - }, + { id: '2tiakNpA4QDPhHt7gcEQxW', name: 'exportedFn', filename: 'src/test.ts' }, (opts) => exportedFn.__executeServer(opts), ) const exportedFn = createServerFn().handler( @@ -424,7 +398,7 @@ describe('createServerFn compiles correctly', async () => { const nonExportedVar = 'non-exported' const nonExportedFn_createServerFn_handler = createServerRpc( { - id: 'f4403dc0b18e216dfe0a9711cab028bc1b9768175daa9236d7115e29c99d76c2', + id: 'p5iUR0EOmj-YLvWU2l_Tw6', name: 'nonExportedFn', filename: 'src/test.ts', }, @@ -708,6 +682,51 @@ describe('createServerFn compiles correctly', async () => { ) }) + test('passes the stable key to generateFunctionId and defaults to short ids', async () => { + const generateFunctionId = vi.fn(() => undefined) + const compiler = new StartCompiler({ + env: 'client', + ...getDefaultTestOptions('client'), + mode: 'build', + loadModule: async () => {}, + lookupKinds: new Set(['ServerFn']), + lookupConfigurations: [ + { + libName: '@tanstack/react-start', + rootExport: 'createServerFn', + kind: 'Root', + }, + ], + resolveId: async (id) => id, + generateFunctionId, + getKnownServerFns: () => ({}), + }) + + const result = await compiler.compile({ + code: ` + import { createServerFn } from '@tanstack/react-start' + export const getUser = createServerFn().handler(async () => 'user') + var twice = createServerFn().handler(async () => 'a') + var twice = createServerFn().handler(async () => 'b') + `, + id: '/test/src/users.ts', + }) + + expect(generateFunctionId.mock.calls).toEqual([ + [{ filename: 'src/users.ts', functionName: 'getUser' }], + [{ filename: 'src/users.ts', functionName: 'twice' }], + [{ filename: 'src/users.ts', functionName: 'twice#1' }], + ]) + const ids = [...result!.code.matchAll(/createClientRpc\("([^"]+)"/g)].map( + (match) => match[1], + ) + expect(ids).toHaveLength(3) + for (const id of ids) { + expect(id).toMatch(/^[\w-]{22}$/) + } + expect(new Set(ids).size).toBe(3) + }) + test('reuses deduped custom IDs across compiler instances', async () => { const serverFnsById: Record< string, @@ -736,9 +755,7 @@ describe('createServerFn compiles correctly', async () => { ], resolveId: async (id) => id, generateFunctionId: ({ functionName }) => - functionName === 'greetUser_createServerFn_handler' - ? 'constant_id' - : undefined, + functionName === 'greetUser' ? 'constant_id' : undefined, getKnownServerFns: () => serverFnsById, onServerFnsById: (discovered) => { Object.assign(serverFnsById, discovered) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructured.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructured.tsx index 88e8607da27..17be276f518 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructured.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructured.tsx @@ -1,39 +1,23 @@ import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' export const withUseServer = createServerFn({ method: 'GET' }).handler( - createClientRpc( - 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', - ), + createClientRpc('80v0H7Z-40eAvJELD4yOZd'), ) export const withArrowFunction = createServerFn({ method: 'GET' }).handler( - createClientRpc( - 'a311dfd9a6554bc0bb9371ef4fbc72c852d0b5532829a2613a29157b8595a793', - ), + createClientRpc('3CuVcIf56mXjOm391b0xfG'), ) export const withArrowFunctionAndFunction = createServerFn({ method: 'GET', -}).handler( - createClientRpc( - 'dbe1f77c6ea7590bce3212a75b06d3cee83b425bcf493014cb47462ef725e36e', - ), -) +}).handler(createClientRpc('7-JuMRVpFOk-B1xNtJ2lhL')) export const withoutUseServer = createServerFn({ method: 'GET' }).handler( - createClientRpc( - 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', - ), + createClientRpc('XbUOHXbrBke2Xebq_cpd9s'), ) export const withVariable = createServerFn({ method: 'GET' }).handler( - createClientRpc( - '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', - ), + createClientRpc('id9L9xtkvHA0G7YPmv4k5B'), ) export const withZodValidator = createServerFn({ method: 'GET' }).handler( - createClientRpc( - '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', - ), + createClientRpc('Y0OXye37qmukl8eWdR6BXL'), ) export const withValidatorFn = createServerFn({ method: 'GET' }).handler( - createClientRpc( - '5cf922ac733d835fee496964cbf0b4b4067cb43d51f2efa484d072e6bb7dae44', - ), + createClientRpc('lJiDYujGD-4bdw-88mWATZ'), ) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructuredRename.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructuredRename.tsx index 34e0c452b62..f464ca8f032 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructuredRename.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructuredRename.tsx @@ -1,22 +1,14 @@ import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn as serverFn } from '@tanstack/react-start' export const withUseServer = serverFn({ method: 'GET' }).handler( - createClientRpc( - 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', - ), + createClientRpc('80v0H7Z-40eAvJELD4yOZd'), ) export const withoutUseServer = serverFn({ method: 'GET' }).handler( - createClientRpc( - 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', - ), + createClientRpc('XbUOHXbrBke2Xebq_cpd9s'), ) export const withVariable = serverFn({ method: 'GET' }).handler( - createClientRpc( - '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', - ), + createClientRpc('id9L9xtkvHA0G7YPmv4k5B'), ) export const withZodValidator = serverFn({ method: 'GET' }).handler( - createClientRpc( - '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', - ), + createClientRpc('Y0OXye37qmukl8eWdR6BXL'), ) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnStarImport.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnStarImport.tsx index 2cea070645a..5172d8108c8 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnStarImport.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnStarImport.tsx @@ -2,29 +2,13 @@ import { createClientRpc } from '@tanstack/react-start/client-rpc' import * as TanStackStart from '@tanstack/react-start' export const withUseServer = TanStackStart.createServerFn({ method: 'GET', -}).handler( - createClientRpc( - 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', - ), -) +}).handler(createClientRpc('80v0H7Z-40eAvJELD4yOZd')) export const withoutUseServer = TanStackStart.createServerFn({ method: 'GET', -}).handler( - createClientRpc( - 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', - ), -) +}).handler(createClientRpc('XbUOHXbrBke2Xebq_cpd9s')) export const withVariable = TanStackStart.createServerFn({ method: 'GET', -}).handler( - createClientRpc( - '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', - ), -) +}).handler(createClientRpc('id9L9xtkvHA0G7YPmv4k5B')) export const withZodValidator = TanStackStart.createServerFn({ method: 'GET', -}).handler( - createClientRpc( - '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', - ), -) +}).handler(createClientRpc('Y0OXye37qmukl8eWdR6BXL')) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnValidator.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnValidator.tsx index 97253672a63..4d3b8dc81a9 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnValidator.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnValidator.tsx @@ -1,7 +1,5 @@ import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' export const withUseServer = createServerFn({ method: 'GET' }).handler( - createClientRpc( - 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', - ), + createClientRpc('80v0H7Z-40eAvJELD4yOZd'), ) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx index 889c1fbf4e3..1fcf8516e27 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx @@ -13,14 +13,10 @@ const adminMiddleware = createMiddleware({ type: 'function' }).server( export const createAuthServerFn = createServerFn().middleware([authMiddleware]) const createAdminServerFn = createAuthServerFn().middleware([adminMiddleware]) export const myAuthedFn = createAuthServerFn().handler( - createClientRpc( - '3bb88b23926fa224cea3dcfb877026757733d95b42e3b66cf7f06bb89dab06a3', - ), + createClientRpc('8wI54n7XLosU5JDSs5CD3r'), ) export const deleteUserFn = createAdminServerFn().handler( - createClientRpc( - '44d9483a9412daaacaa5a8aee6b02294333a0e61b5d3b17ba6bcb34fd2bdce31', - ), + createClientRpc('_zazY1Jgzd8oXXcuPROEv9'), ) function createFakeFn() { return { diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/isomorphic-fns.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/isomorphic-fns.tsx index 8e09a57d372..acd6c922543 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/isomorphic-fns.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/isomorphic-fns.tsx @@ -6,17 +6,13 @@ const getEnv = createIsomorphicFn() .server(() => 'server') .client(() => 'client') const getServerEnv = createServerFn().handler( - createClientRpc( - '6049dd46bc00e0980e387f3e74924b40e92f37634a3fe74a1a1facda9e9207c2', - ), + createClientRpc('Jc6GTcykFaVJAwm-a0BofP'), ) const getEcho = createIsomorphicFn() .server((input: string) => 'server received ' + input) .client((input) => 'client received ' + input) const getServerEcho = createServerFn().handler( - createClientRpc( - 'ebb82e0293952b7b1dde3f664dec4c139471c046d06d4dc4ca172f4f45e386d1', - ), + createClientRpc('s0jNQTWKXVmb2EhbCIBxAU'), ) export const Route = createFileRoute('/isomorphic-fns')({ component: RouteComponent, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructured.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructured.tsx index efa8f395d59..8de2685ba73 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructured.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructured.tsx @@ -2,41 +2,23 @@ import { createSsrRpc } from '@tanstack/react-start/ssr-rpc' import { createServerFn } from '@tanstack/react-start' import { z } from 'zod' export const withUseServer = createServerFn({ method: 'GET' }).handler( - createSsrRpc( - 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', - ), + createSsrRpc('80v0H7Z-40eAvJELD4yOZd'), ) export const withArrowFunction = createServerFn({ method: 'GET' }).handler( - createSsrRpc( - 'a311dfd9a6554bc0bb9371ef4fbc72c852d0b5532829a2613a29157b8595a793', - ), + createSsrRpc('3CuVcIf56mXjOm391b0xfG'), ) export const withArrowFunctionAndFunction = createServerFn({ method: 'GET', -}).handler( - createSsrRpc( - 'dbe1f77c6ea7590bce3212a75b06d3cee83b425bcf493014cb47462ef725e36e', - ), -) +}).handler(createSsrRpc('7-JuMRVpFOk-B1xNtJ2lhL')) export const withoutUseServer = createServerFn({ method: 'GET' }).handler( - createSsrRpc( - 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', - ), + createSsrRpc('XbUOHXbrBke2Xebq_cpd9s'), ) export const withVariable = createServerFn({ method: 'GET' }).handler( - createSsrRpc( - '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', - ), + createSsrRpc('id9L9xtkvHA0G7YPmv4k5B'), ) export const withZodValidator = createServerFn({ method: 'GET' }).handler( - createSsrRpc( - '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', - ), + createSsrRpc('Y0OXye37qmukl8eWdR6BXL'), ) export const withValidatorFn = createServerFn({ method: 'GET' }) .validator(z.number()) - .handler( - createSsrRpc( - '5cf922ac733d835fee496964cbf0b4b4067cb43d51f2efa484d072e6bb7dae44', - ), - ) + .handler(createSsrRpc('lJiDYujGD-4bdw-88mWATZ')) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructuredRename.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructuredRename.tsx index 7088a4bc69f..af091fc1ca5 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructuredRename.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructuredRename.tsx @@ -1,22 +1,14 @@ import { createSsrRpc } from '@tanstack/react-start/ssr-rpc' import { createServerFn as serverFn } from '@tanstack/react-start' export const withUseServer = serverFn({ method: 'GET' }).handler( - createSsrRpc( - 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', - ), + createSsrRpc('80v0H7Z-40eAvJELD4yOZd'), ) export const withoutUseServer = serverFn({ method: 'GET' }).handler( - createSsrRpc( - 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', - ), + createSsrRpc('XbUOHXbrBke2Xebq_cpd9s'), ) export const withVariable = serverFn({ method: 'GET' }).handler( - createSsrRpc( - '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', - ), + createSsrRpc('id9L9xtkvHA0G7YPmv4k5B'), ) export const withZodValidator = serverFn({ method: 'GET' }).handler( - createSsrRpc( - '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', - ), + createSsrRpc('Y0OXye37qmukl8eWdR6BXL'), ) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnStarImport.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnStarImport.tsx index 899aaf3d0db..80ee3e59b74 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnStarImport.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnStarImport.tsx @@ -2,29 +2,13 @@ import { createSsrRpc } from '@tanstack/react-start/ssr-rpc' import * as TanStackStart from '@tanstack/react-start' export const withUseServer = TanStackStart.createServerFn({ method: 'GET', -}).handler( - createSsrRpc( - 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', - ), -) +}).handler(createSsrRpc('80v0H7Z-40eAvJELD4yOZd')) export const withoutUseServer = TanStackStart.createServerFn({ method: 'GET', -}).handler( - createSsrRpc( - 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', - ), -) +}).handler(createSsrRpc('XbUOHXbrBke2Xebq_cpd9s')) export const withVariable = TanStackStart.createServerFn({ method: 'GET', -}).handler( - createSsrRpc( - '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', - ), -) +}).handler(createSsrRpc('id9L9xtkvHA0G7YPmv4k5B')) export const withZodValidator = TanStackStart.createServerFn({ method: 'GET', -}).handler( - createSsrRpc( - '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', - ), -) +}).handler(createSsrRpc('Y0OXye37qmukl8eWdR6BXL')) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnValidator.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnValidator.tsx index e1d136311b3..bef88f4ab3a 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnValidator.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnValidator.tsx @@ -3,8 +3,4 @@ import { createServerFn } from '@tanstack/react-start' import { z } from 'zod' export const withUseServer = createServerFn({ method: 'GET' }) .validator(z.number()) - .handler( - createSsrRpc( - 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', - ), - ) + .handler(createSsrRpc('80v0H7Z-40eAvJELD4yOZd')) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/factory.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/factory.tsx index ab19c3e1f88..6f4c03cc80f 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/factory.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/factory.tsx @@ -13,14 +13,10 @@ const adminMiddleware = createMiddleware({ type: 'function' }).server( export const createAuthServerFn = createServerFn().middleware([authMiddleware]) const createAdminServerFn = createAuthServerFn().middleware([adminMiddleware]) export const myAuthedFn = createAuthServerFn().handler( - createSsrRpc( - '3bb88b23926fa224cea3dcfb877026757733d95b42e3b66cf7f06bb89dab06a3', - ), + createSsrRpc('8wI54n7XLosU5JDSs5CD3r'), ) export const deleteUserFn = createAdminServerFn().handler( - createSsrRpc( - '44d9483a9412daaacaa5a8aee6b02294333a0e61b5d3b17ba6bcb34fd2bdce31', - ), + createSsrRpc('_zazY1Jgzd8oXXcuPROEv9'), ) function createFakeFn() { return { diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/isomorphic-fns.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/isomorphic-fns.tsx index cfefad2bfbd..0615b930e39 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/isomorphic-fns.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/isomorphic-fns.tsx @@ -6,20 +6,14 @@ const getEnv = createIsomorphicFn() .server(() => 'server') .client(() => 'client') const getServerEnv = createServerFn().handler( - createSsrRpc( - '6049dd46bc00e0980e387f3e74924b40e92f37634a3fe74a1a1facda9e9207c2', - ), + createSsrRpc('Jc6GTcykFaVJAwm-a0BofP'), ) const getEcho = createIsomorphicFn() .server((input: string) => 'server received ' + input) .client((input) => 'client received ' + input) const getServerEcho = createServerFn() .validator((input: string) => input) - .handler( - createSsrRpc( - 'ebb82e0293952b7b1dde3f664dec4c139471c046d06d4dc4ca172f4f45e386d1', - ), - ) + .handler(createSsrRpc('s0jNQTWKXVmb2EhbCIBxAU')) export const Route = createFileRoute('/isomorphic-fns')({ component: RouteComponent, loader() { diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructured.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructured.tsx index 3bc4879c141..b58fef66d14 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructured.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructured.tsx @@ -3,7 +3,7 @@ import { createServerFn } from '@tanstack/react-start' import { z } from 'zod' const withUseServer_createServerFn_handler = createServerRpc( { - id: 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + id: '80v0H7Z-40eAvJELD4yOZd', name: 'withUseServer', filename: 'src/test.ts', }, @@ -21,7 +21,7 @@ const withUseServer = createServerFn({ method: 'GET' }).handler( ) const withArrowFunction_createServerFn_handler = createServerRpc( { - id: 'a311dfd9a6554bc0bb9371ef4fbc72c852d0b5532829a2613a29157b8595a793', + id: '3CuVcIf56mXjOm391b0xfG', name: 'withArrowFunction', filename: 'src/test.ts', }, @@ -33,7 +33,7 @@ const withArrowFunction = createServerFn({ method: 'GET' }).handler( ) const withArrowFunctionAndFunction_createServerFn_handler = createServerRpc( { - id: 'dbe1f77c6ea7590bce3212a75b06d3cee83b425bcf493014cb47462ef725e36e', + id: '7-JuMRVpFOk-B1xNtJ2lhL', name: 'withArrowFunctionAndFunction', filename: 'src/test.ts', }, @@ -45,7 +45,7 @@ const withArrowFunctionAndFunction = createServerFn({ method: 'GET' }).handler( ) const withoutUseServer_createServerFn_handler = createServerRpc( { - id: 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', + id: 'XbUOHXbrBke2Xebq_cpd9s', name: 'withoutUseServer', filename: 'src/test.ts', }, @@ -63,7 +63,7 @@ const withoutUseServer = createServerFn({ method: 'GET' }).handler( ) const withVariable_createServerFn_handler = createServerRpc( { - id: '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', + id: 'id9L9xtkvHA0G7YPmv4k5B', name: 'withVariable', filename: 'src/test.ts', }, @@ -90,7 +90,7 @@ function zodValidator( } const withZodValidator_createServerFn_handler = createServerRpc( { - id: '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', + id: 'Y0OXye37qmukl8eWdR6BXL', name: 'withZodValidator', filename: 'src/test.ts', }, @@ -104,7 +104,7 @@ const withZodValidator = createServerFn({ method: 'GET' }).handler( ) const withValidatorFn_createServerFn_handler = createServerRpc( { - id: '5cf922ac733d835fee496964cbf0b4b4067cb43d51f2efa484d072e6bb7dae44', + id: 'lJiDYujGD-4bdw-88mWATZ', name: 'withValidatorFn', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructuredRename.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructuredRename.tsx index 775c32b645d..840e0aead03 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructuredRename.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructuredRename.tsx @@ -3,7 +3,7 @@ import { createServerFn as serverFn } from '@tanstack/react-start' import { z } from 'zod' const withUseServer_createServerFn_handler = createServerRpc( { - id: 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + id: '80v0H7Z-40eAvJELD4yOZd', name: 'withUseServer', filename: 'src/test.ts', }, @@ -21,7 +21,7 @@ const withUseServer = serverFn({ method: 'GET' }).handler( ) const withoutUseServer_createServerFn_handler = createServerRpc( { - id: 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', + id: 'XbUOHXbrBke2Xebq_cpd9s', name: 'withoutUseServer', filename: 'src/test.ts', }, @@ -39,7 +39,7 @@ const withoutUseServer = serverFn({ method: 'GET' }).handler( ) const withVariable_createServerFn_handler = createServerRpc( { - id: '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', + id: 'id9L9xtkvHA0G7YPmv4k5B', name: 'withVariable', filename: 'src/test.ts', }, @@ -66,7 +66,7 @@ function zodValidator( } const withZodValidator_createServerFn_handler = createServerRpc( { - id: '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', + id: 'Y0OXye37qmukl8eWdR6BXL', name: 'withZodValidator', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnStarImport.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnStarImport.tsx index bc18c08855f..0fafe3c9233 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnStarImport.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnStarImport.tsx @@ -3,7 +3,7 @@ import * as TanStackStart from '@tanstack/react-start' import { z } from 'zod' const withUseServer_createServerFn_handler = createServerRpc( { - id: 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + id: '80v0H7Z-40eAvJELD4yOZd', name: 'withUseServer', filename: 'src/test.ts', }, @@ -22,7 +22,7 @@ const withUseServer = TanStackStart.createServerFn({ method: 'GET' }).handler( ) const withoutUseServer_createServerFn_handler = createServerRpc( { - id: 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', + id: 'XbUOHXbrBke2Xebq_cpd9s', name: 'withoutUseServer', filename: 'src/test.ts', }, @@ -39,7 +39,7 @@ const withoutUseServer = TanStackStart.createServerFn({ }) const withVariable_createServerFn_handler = createServerRpc( { - id: '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', + id: 'id9L9xtkvHA0G7YPmv4k5B', name: 'withVariable', filename: 'src/test.ts', }, @@ -66,7 +66,7 @@ function zodValidator( } const withZodValidator_createServerFn_handler = createServerRpc( { - id: '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', + id: 'Y0OXye37qmukl8eWdR6BXL', name: 'withZodValidator', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnValidator.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnValidator.tsx index f62fa718d61..ee180e145ba 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnValidator.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnValidator.tsx @@ -3,7 +3,7 @@ import { createServerFn } from '@tanstack/react-start' import { z } from 'zod' const withUseServer_createServerFn_handler = createServerRpc( { - id: 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + id: '80v0H7Z-40eAvJELD4yOZd', name: 'withUseServer', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/factory.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/factory.tsx index 26298edd567..43b148d6d07 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/factory.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/factory.tsx @@ -13,11 +13,7 @@ const adminMiddleware = createMiddleware({ type: 'function' }).server( const createAuthServerFn = createServerFn().middleware([authMiddleware]) const createAdminServerFn = createAuthServerFn().middleware([adminMiddleware]) const myAuthedFn_createServerFn_handler = createServerRpc( - { - id: '3bb88b23926fa224cea3dcfb877026757733d95b42e3b66cf7f06bb89dab06a3', - name: 'myAuthedFn', - filename: 'src/test.ts', - }, + { id: '8wI54n7XLosU5JDSs5CD3r', name: 'myAuthedFn', filename: 'src/test.ts' }, (opts) => myAuthedFn.__executeServer(opts), ) const myAuthedFn = createAuthServerFn().handler( @@ -28,7 +24,7 @@ const myAuthedFn = createAuthServerFn().handler( ) const deleteUserFn_createServerFn_handler = createServerRpc( { - id: '44d9483a9412daaacaa5a8aee6b02294333a0e61b5d3b17ba6bcb34fd2bdce31', + id: '_zazY1Jgzd8oXXcuPROEv9', name: 'deleteUserFn', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/isomorphic-fns.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/isomorphic-fns.tsx index 3ae86226834..df93893c8bc 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/isomorphic-fns.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/isomorphic-fns.tsx @@ -5,7 +5,7 @@ const getEnv = createIsomorphicFn() .client(() => 'client') const getServerEnv_createServerFn_handler = createServerRpc( { - id: '6049dd46bc00e0980e387f3e74924b40e92f37634a3fe74a1a1facda9e9207c2', + id: 'Jc6GTcykFaVJAwm-a0BofP', name: 'getServerEnv', filename: 'src/test.ts', }, @@ -20,7 +20,7 @@ const getEcho = createIsomorphicFn() .client((input) => 'client received ' + input) const getServerEcho_createServerFn_handler = createServerRpc( { - id: 'ebb82e0293952b7b1dde3f664dec4c139471c046d06d4dc4ca172f4f45e386d1', + id: 's0jNQTWKXVmb2EhbCIBxAU', name: 'getServerEcho', filename: 'src/test.ts', }, From fa5fc4e26f7146eca50f422c5ffcb8d83b904014 Mon Sep 17 00:00:00 2001 From: Sheraff Date: Wed, 4 Nov 2026 08:01:39 +0100 Subject: [PATCH 2/5] refactor(start-plugin-core): keep generateFunctionId's handler-name input, reject custom ids that aren't one URL path segment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follows #8817's current id contract: - `generateFunctionId` receives the generated handler name again (`greetUser_createServerFn_handler`, `_1`, `_2`… for repeats), with characters outside `[A-Za-z0-9_$]` as `_`. The handler export keeps its name. - The default id stays 22 base64url characters of `sha256("#")`, `#n` for repeats. - A custom id outside RFC 3986's unreserved characters and `$` (or `.`, `..`, empty) fails the build, naming the file, the function and the id. - The e2e id maps return to main's keys. Co-Authored-By: Claude Opus 5.5 --- .changeset/start-server-fn-stable-ids.md | 7 +- .../framework/react/guide/server-functions.md | 9 +- .../server-functions/start-mode-config.ts | 4 +- .../server-functions/vite.config.ts | 4 +- e2e/vue-start/server-functions/vite.config.ts | 4 +- .../src/start-compiler/compiler.ts | 28 ++++- .../start-compiler/handleCreateServerFn.ts | 36 ++++-- .../src/start-compiler/types.ts | 4 + .../createServerFn/createServerFn.test.ts | 107 +++++++++++++++--- 9 files changed, 167 insertions(+), 36 deletions(-) diff --git a/.changeset/start-server-fn-stable-ids.md b/.changeset/start-server-fn-stable-ids.md index 695f2386f11..f54fc7b2e5c 100644 --- a/.changeset/start-server-fn-stable-ids.md +++ b/.changeset/start-server-fn-stable-ids.md @@ -2,7 +2,8 @@ '@tanstack/start-plugin-core': minor --- -Server function ids are now derived from the function's stable name, and default production ids are shorter (22 characters). +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 production server function id changes.** Update CDN rules, WAF allowlists, analytics keyed by id and native clients that call `/_serverFn/` directly. Browser tabs loaded before a deploy fail their next server-function call after it: plan a reload or a blue/green window. -- **`serverFns.generateFunctionId` receives the function's stable name** (`greetUser`) as `functionName`, not the generated handler name (`greetUser_createServerFn_handler`). Custom generators keyed on the old names must be updated. +- **Every default production server function id changes.** Update CDN rules, WAF allowlists, analytics keyed by id and native clients that call `/_serverFn/` 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). diff --git a/docs/start/framework/react/guide/server-functions.md b/docs/start/framework/react/guide/server-functions.md index 8d083871880..47c0b615998 100644 --- a/docs/start/framework/react/guide/server-functions.md +++ b/docs/start/framework/react/guide/server-functions.md @@ -425,7 +425,14 @@ Customization: You can customize function ID generation for the production build by providing a `generateFunctionId` function when configuring the TanStack Start build tool plugin. -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. +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`. 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 `_`. + +Return `undefined` to use the default ID. Prefer deterministic inputs (filename + functionName) so IDs remain stable between builds. + +The ID becomes one segment of the server function's URL (`/_serverFn/`), unencoded, so it may contain only ASCII letters, digits, `-`, `.`, `_`, `~` and `$`, and may not be `.` or `..`. Any other ID fails the build with an error naming the file, the function and the ID. `filename` contains `/`, so hash it (as in the example below) rather than embedding it as is. Please note that this customization is **experimental** and subject to change. diff --git a/e2e/react-start/server-functions/start-mode-config.ts b/e2e/react-start/server-functions/start-mode-config.ts index abf46563ba3..ba75b59693f 100644 --- a/e2e/react-start/server-functions/start-mode-config.ts +++ b/e2e/react-start/server-functions/start-mode-config.ts @@ -1,9 +1,9 @@ const FUNCTION_IDS: Record> = { 'src/routes/submit-post-formdata.tsx': { - greetUser: 'submit-post-formdata-greetUser', + greetUser_createServerFn_handler: 'submit-post-formdata-greetUser', }, 'src/routes/formdata-redirect/index.tsx': { - greetUser: 'formdata-redirect-greetUser', + greetUser_createServerFn_handler: 'formdata-redirect-greetUser', }, } diff --git a/e2e/solid-start/server-functions/vite.config.ts b/e2e/solid-start/server-functions/vite.config.ts index d063548aaca..fc622bbfb02 100644 --- a/e2e/solid-start/server-functions/vite.config.ts +++ b/e2e/solid-start/server-functions/vite.config.ts @@ -4,8 +4,8 @@ import viteSolid from 'vite-plugin-solid' import tailwindcss from '@tailwindcss/vite' const FUNCTIONS_WITH_CONSTANT_ID = [ - 'src/routes/submit-post-formdata.tsx/greetUser', - 'src/routes/formdata-redirect/index.tsx/greetUser', + 'src/routes/submit-post-formdata.tsx/greetUser_createServerFn_handler', + 'src/routes/formdata-redirect/index.tsx/greetUser_createServerFn_handler', ] export default defineConfig({ diff --git a/e2e/vue-start/server-functions/vite.config.ts b/e2e/vue-start/server-functions/vite.config.ts index 97b2fe2328c..362cf4e1221 100644 --- a/e2e/vue-start/server-functions/vite.config.ts +++ b/e2e/vue-start/server-functions/vite.config.ts @@ -5,8 +5,8 @@ import vueJsx from '@vitejs/plugin-vue-jsx' import tailwindcss from '@tailwindcss/vite' const FUNCTIONS_WITH_CONSTANT_ID = [ - 'src/routes/submit-post-formdata.tsx/greetUser', - 'src/routes/formdata-redirect/index.tsx/greetUser', + 'src/routes/submit-post-formdata.tsx/greetUser_createServerFn_handler', + 'src/routes/formdata-redirect/index.tsx/greetUser_createServerFn_handler', ] export default defineConfig({ diff --git a/packages/start-plugin-core/src/start-compiler/compiler.ts b/packages/start-plugin-core/src/start-compiler/compiler.ts index e6c79439336..1b696f38cec 100644 --- a/packages/start-plugin-core/src/start-compiler/compiler.ts +++ b/packages/start-plugin-core/src/start-compiler/compiler.ts @@ -96,6 +96,19 @@ export function isStartCompilerPluginEnabledForEnv( return isStartCompilerEnvironmentEnabled(plugin.environment, env) } +/** + * An id the URL carries whole as one path segment (`TSS_SERVER_FN_BASE + id` + * unencoded; the server reads the segment back as is): RFC 3986 unreserved + * characters, which no client, proxy or server encodes, decodes or + * normalises, and `$`, which `functionName` can hold; not a dot segment, + * which URL parsing removes. + */ +const URL_SEGMENT_ID = /^[A-Za-z0-9\-._~$]+$/ + +function isUrlSegmentId(id: string): boolean { + return URL_SEGMENT_ID.test(id) && id !== '.' && id !== '..' +} + function isStartCompilerEnvironmentEnabled( environment: | StartCompilerEnvironment @@ -634,7 +647,9 @@ export class StartCompiler { private generateFunctionId(opts: { filename: string functionName: string + generatorFunctionName: string stableKey: string + variableName: string extractedFilename: string }): string { if (this.mode === 'dev') { @@ -674,13 +689,18 @@ export class StartCompiler { functionId = knownFn.functionId } - if (this.options.generateFunctionId) { - functionId ??= this.options.generateFunctionId({ + if (functionId === undefined && this.options.generateFunctionId) { + functionId = this.options.generateFunctionId({ filename: opts.filename, - functionName: opts.stableKey, + functionName: opts.generatorFunctionName, }) + if (functionId !== undefined && !isUrlSegmentId(functionId)) { + throw new Error( + `[tanstack-start] serverFns.generateFunctionId returned ${JSON.stringify(functionId)} for the server function ${opts.variableName} (functionName "${opts.generatorFunctionName}") of ${opts.filename}. A server function id is one path segment of its URL: use only ASCII letters, digits, "-", ".", "_", "~" and "$" (not "." or "..").`, + ) + } } - if (!functionId) { + if (functionId === undefined) { functionId = crypto .createHash('sha256') .update(entryId) diff --git a/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts b/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts index ea6815b7f41..8ed9d2bfb4d 100644 --- a/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts +++ b/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts @@ -209,6 +209,7 @@ export function handleCreateServerFn( // Track function names to ensure uniqueness within this file const functionNameSet = new Set() + const generatorFunctionNameSet = new Set() const stableKeyCount = new Map() const exportNames = new Set() @@ -246,14 +247,20 @@ export function handleCreateServerFn( // Generate unique function name with _createServerFn_handler suffix // The function name is derived from the variable name - let functionName = `${existingVariableName}_createServerFn_handler` - while (functionNameSet.has(functionName)) { - functionName = incrementFunctionNameVersion(functionName) - } - functionNameSet.add(functionName) + const functionName = allocateHandlerName( + existingVariableName, + functionNameSet, + ) + // What a custom `generateFunctionId` receives: the same name, with + // characters outside `[A-Za-z0-9_$]` (a non-ASCII identifier) as `_`, so + // an id that embeds it stays a URL path segment + const generatorFunctionName = allocateHandlerName( + existingVariableName.replace(/[^A-Za-z0-9_$]/g, '_'), + generatorFunctionNameSet, + ) - // The stable key names the server fn independently of the compiled - // handler name: the variable name, with `#n` for repeats in the file + // The stable key seeds the default id: the variable name, with `#n` for + // repeats in the file const keyCount = stableKeyCount.get(existingVariableName) ?? 0 stableKeyCount.set(existingVariableName, keyCount + 1) const stableKey = @@ -265,7 +272,9 @@ export function handleCreateServerFn( const functionId = context.generateFunctionId({ filename: relativeFilename, functionName, + generatorFunctionName, stableKey, + variableName: existingVariableName, extractedFilename, }) @@ -474,6 +483,19 @@ function makeIdentifierSafe(identifier: string): string { .replace(/^_|_$/g, '') // Trim leading/trailing underscores } +/** + * `_createServerFn_handler`, then `_1`, `_2`… for a name taken earlier + * in the file. + */ +function allocateHandlerName(base: string, taken: Set): string { + let name = `${base}_createServerFn_handler` + while (taken.has(name)) { + name = incrementFunctionNameVersion(name) + } + taken.add(name) + return name +} + /** * Increments the version number suffix on a function name. */ diff --git a/packages/start-plugin-core/src/start-compiler/types.ts b/packages/start-plugin-core/src/start-compiler/types.ts index b31f7872748..85320307e4e 100644 --- a/packages/start-plugin-core/src/start-compiler/types.ts +++ b/packages/start-plugin-core/src/start-compiler/types.ts @@ -91,8 +91,12 @@ export type GenerateFunctionIdFn = (opts: { filename: string /** Name of the extracted handler export. */ functionName: string + /** The `functionName` a custom `generateFunctionId` receives. */ + generatorFunctionName: string /** The server fn's variable name, with `#n` for repeats in the same file. */ stableKey: string + /** The server fn's variable name. */ + variableName: string extractedFilename: string }) => string diff --git a/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts b/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts index 67dd019dace..88c6d44f454 100644 --- a/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts +++ b/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts @@ -682,9 +682,13 @@ describe('createServerFn compiles correctly', async () => { ) }) - test('passes the stable key to generateFunctionId and defaults to short ids', async () => { - const generateFunctionId = vi.fn(() => undefined) - const compiler = new StartCompiler({ + function createBuildCompiler( + generateFunctionId: (opts: { + filename: string + functionName: string + }) => string | undefined, + ) { + return new StartCompiler({ env: 'client', ...getDefaultTestOptions('client'), mode: 'build', @@ -701,6 +705,11 @@ describe('createServerFn compiles correctly', async () => { generateFunctionId, getKnownServerFns: () => ({}), }) + } + + test('passes the handler name to generateFunctionId and defaults to short ids of the file and variable name', async () => { + const generateFunctionId = vi.fn(() => undefined) + const compiler = createBuildCompiler(generateFunctionId) const result = await compiler.compile({ code: ` @@ -708,23 +717,89 @@ describe('createServerFn compiles correctly', async () => { export const getUser = createServerFn().handler(async () => 'user') var twice = createServerFn().handler(async () => 'a') var twice = createServerFn().handler(async () => 'b') + export const getÜser = createServerFn().handler(async () => 'ü') `, id: '/test/src/users.ts', }) expect(generateFunctionId.mock.calls).toEqual([ - [{ filename: 'src/users.ts', functionName: 'getUser' }], - [{ filename: 'src/users.ts', functionName: 'twice' }], - [{ filename: 'src/users.ts', functionName: 'twice#1' }], + [ + { + filename: 'src/users.ts', + functionName: 'getUser_createServerFn_handler', + }, + ], + [ + { + filename: 'src/users.ts', + functionName: 'twice_createServerFn_handler', + }, + ], + [ + { + filename: 'src/users.ts', + functionName: 'twice_createServerFn_handler_1', + }, + ], + [ + { + filename: 'src/users.ts', + functionName: 'get_ser_createServerFn_handler', + }, + ], ]) - const ids = [...result!.code.matchAll(/createClientRpc\("([^"]+)"/g)].map( - (match) => match[1], - ) - expect(ids).toHaveLength(3) - for (const id of ids) { - expect(id).toMatch(/^[\w-]{22}$/) - } - expect(new Set(ids).size).toBe(3) + // base64url of sha256("src/users.ts#getUser"), "…#twice", "…#twice#1", + // "…#getÜser", first 22 characters + expect( + [...result!.code.matchAll(/createClientRpc\("([^"]+)"/g)].map( + (match) => match[1], + ), + ).toEqual([ + '5khyYluQNdT_FnYJdeNjO7', + '86c3ovB8BBp6j3lZ7Ys3-q', + 'VDV9uQu-DjlBpN8wR9Hg81', + 'fR2z51S12hblu5q1t0xfsF', + ]) + }) + + test.each([ + 'users/get', + 'users#get', + 'users get', + 'utilisateurs-é', + '..', + '', + ])( + 'fails on a custom id that is not one URL path segment: %j', + async (id) => { + const compiler = createBuildCompiler(() => id) + + await expect( + compiler.compile({ + code: ` + import { createServerFn } from '@tanstack/react-start' + export const getUser = createServerFn().handler(async () => 'user') + `, + id: '/test/src/users.ts', + }), + ).rejects.toThrow( + `serverFns.generateFunctionId returned ${JSON.stringify(id)} for the server function getUser (functionName "getUser_createServerFn_handler") of src/users.ts`, + ) + }, + ) + + test('accepts a custom id of unreserved characters and $', async () => { + const compiler = createBuildCompiler(() => 'Users-get.v1_~$') + + const result = await compiler.compile({ + code: ` + import { createServerFn } from '@tanstack/react-start' + export const getUser = createServerFn().handler(async () => 'user') + `, + id: '/test/src/users.ts', + }) + + expect(result!.code).toContain('createClientRpc("Users-get.v1_~$"') }) test('reuses deduped custom IDs across compiler instances', async () => { @@ -755,7 +830,9 @@ describe('createServerFn compiles correctly', async () => { ], resolveId: async (id) => id, generateFunctionId: ({ functionName }) => - functionName === 'greetUser' ? 'constant_id' : undefined, + functionName === 'greetUser_createServerFn_handler' + ? 'constant_id' + : undefined, getKnownServerFns: () => serverFnsById, onServerFnsById: (discovered) => { Object.assign(serverFnsById, discovered) From 6c60ab3d9acbcb99a794a6d9d2ff14634053e984 Mon Sep 17 00:00:00 2001 From: Sheraff Date: Wed, 4 Nov 2026 08:29:31 +0100 Subject: [PATCH 3/5] docs(start-plugin-core): a repeated server function name loses leading and doubled underscores MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The generator's `functionName` for a repeated name goes through the old increment rule, which strips leading underscores and collapses runs of underscores (a second `_x` gets `x_createServerFn_handler_1`). Say so, and pin it with `_x` twice and `café` then `caf_`. Co-Authored-By: Claude Opus 5.5 --- .../framework/react/guide/server-functions.md | 2 +- .../createServerFn/createServerFn.test.ts | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/start/framework/react/guide/server-functions.md b/docs/start/framework/react/guide/server-functions.md index 47c0b615998..ebfb44afd82 100644 --- a/docs/start/framework/react/guide/server-functions.md +++ b/docs/start/framework/react/guide/server-functions.md @@ -428,7 +428,7 @@ You can customize function ID generation for the production build by providing a 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`. 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 `_`. +- `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 `_`. Return `undefined` to use the default ID. Prefer deterministic inputs (filename + functionName) so IDs remain stable between builds. diff --git a/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts b/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts index 88c6d44f454..a6a1d952430 100644 --- a/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts +++ b/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts @@ -762,6 +762,33 @@ describe('createServerFn compiles correctly', async () => { ]) }) + test('a repeated name loses its leading and doubled underscores in the generator input', async () => { + const generateFunctionId = vi.fn( + (_opts: { filename: string; functionName: string }) => undefined, + ) + const compiler = createBuildCompiler(generateFunctionId) + + await compiler.compile({ + code: ` + import { createServerFn } from '@tanstack/react-start' + var _x = createServerFn().handler(async () => 'a') + var _x = createServerFn().handler(async () => 'b') + export const café = createServerFn().handler(async () => 'c') + export const caf_ = createServerFn().handler(async () => 'd') + `, + id: '/test/src/users.ts', + }) + + expect( + generateFunctionId.mock.calls.map(([opts]) => opts.functionName), + ).toEqual([ + '_x_createServerFn_handler', + 'x_createServerFn_handler_1', + 'caf__createServerFn_handler', + 'caf_createServerFn_handler_1', + ]) + }) + test.each([ 'users/get', 'users#get', From 09b7f3a475979c2faf1f2782ae8028c71dc8de0e Mon Sep 17 00:00:00 2001 From: Sheraff Date: Wed, 4 Nov 2026 08:40:10 +0100 Subject: [PATCH 4/5] docs(start-plugin-core): names that become equal after the `_` replacement get suffixes too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `café` and `caf_` both give `caf__createServerFn_handler`, so the later one gets `caf_createServerFn_handler_1`, like a repeated variable name. Describe the replacement first, then the suffix rule for any resulting duplicate. Co-Authored-By: Claude Opus 5.5 --- docs/start/framework/react/guide/server-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/start/framework/react/guide/server-functions.md b/docs/start/framework/react/guide/server-functions.md index ebfb44afd82..10e3a0a6f04 100644 --- a/docs/start/framework/react/guide/server-functions.md +++ b/docs/start/framework/react/guide/server-functions.md @@ -428,7 +428,7 @@ You can customize function ID generation for the production build by providing a 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`. 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 `_`. +- `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`). Return `undefined` to use the default ID. Prefer deterministic inputs (filename + functionName) so IDs remain stable between builds. From d7c8128d63faba828b49880d3a5d52a480f17346 Mon Sep 17 00:00:00 2001 From: Sheraff Date: Sun, 8 Nov 2026 08:16:09 +0100 Subject: [PATCH 5/5] refactor(start-plugin-core): keep main's server-fn ids; refuse custom ids that never worked over HTTP #8817 now prints main's default ids and passes main's functionName to generateFunctionId, so this reverts the shorter default ids, the generator-name split and the empty-id failure. What stays is #8817's custom-id check: an id the URL never carries whole fails the build, one with other characters outside the unreserved set and $ builds with a deprecation warning, and the resolver manifest escapes a `'` in an id. Co-Authored-By: Claude Opus 5.5 --- .changeset/start-server-fn-custom-id-check.md | 7 + .changeset/start-server-fn-stable-ids.md | 9 - .../framework/react/guide/server-functions.md | 11 +- .../src/start-compiler/compiler.ts | 68 +++-- .../start-compiler/handleCreateServerFn.ts | 42 +-- .../src/start-compiler/server-fn-id-check.ts | 75 ++++++ .../server-fn-resolver-module.ts | 3 +- .../src/start-compiler/types.ts | 17 +- .../createServerFn/createServerFn.test.ts | 251 +++++++----------- .../client/createServerFnDestructured.tsx | 30 ++- .../createServerFnDestructuredRename.tsx | 16 +- .../client/createServerFnStarImport.tsx | 24 +- .../client/createServerFnValidator.tsx | 4 +- .../snapshots/client/factory.tsx | 8 +- .../snapshots/client/isomorphic-fns.tsx | 8 +- .../createServerFnDestructured.tsx | 32 ++- .../createServerFnDestructuredRename.tsx | 16 +- .../createServerFnStarImport.tsx | 24 +- .../server-caller/createServerFnValidator.tsx | 6 +- .../snapshots/server-caller/factory.tsx | 8 +- .../server-caller/isomorphic-fns.tsx | 10 +- .../createServerFnDestructured.tsx | 14 +- .../createServerFnDestructuredRename.tsx | 8 +- .../createServerFnStarImport.tsx | 8 +- .../createServerFnValidator.tsx | 2 +- .../snapshots/server-provider/factory.tsx | 8 +- .../server-provider/isomorphic-fns.tsx | 4 +- .../tests/integration/start-app.ts | 11 +- .../start-build-function-id-url.test.ts | 130 +++++++++ 29 files changed, 533 insertions(+), 321 deletions(-) create mode 100644 .changeset/start-server-fn-custom-id-check.md delete mode 100644 .changeset/start-server-fn-stable-ids.md create mode 100644 packages/start-plugin-core/src/start-compiler/server-fn-id-check.ts create mode 100644 packages/start-plugin-core/tests/integration/start-build-function-id-url.test.ts diff --git a/.changeset/start-server-fn-custom-id-check.md b/.changeset/start-server-fn-custom-id-check.md new file mode 100644 index 00000000000..384f0ade068 --- /dev/null +++ b/.changeset/start-server-fn-custom-id-check.md @@ -0,0 +1,7 @@ +--- +'@tanstack/start-plugin-core': minor +--- + +A server function id that a custom `serverFns.generateFunctionId` returns and that never worked over HTTP now fails the build, with an error naming the file, the function and the id: whitespace, a control or non-ASCII character, any of `"`, `#`, `/`, `<`, `>`, `?`, `\`, `^`, `` ` ``, `{`, `}`, the ids `.` and `..`, and a percent-encoding the server reads as another id (`%41`, lowercase hex, `%2E`) or rejects. The client puts the id in the URL unencoded, so a `#` or `?` ended the path and a `/` cut the id short (the call silently ran another server function of the file or none), and the other characters reached the server percent-encoded and matched no function. An id embedding a non-ASCII `functionName` is one of these. One case did work and now fails the build: such an id on a server function only SSR code called. + +Ids with `%` or the other characters URLs allow (such as `:`, `@`, `+`, `=`, `,`, `;`, `!`, `*`, `(`, `)`, `&`, `'`, `|`, `[`, `]`) still build and reach their handler, with a deprecation warning (from the Vite plugin; the Rsbuild plugin does not print compiler warnings yet): the next major version rejects them, since proxies and servers may decode or rewrite them (hash the inputs instead, as the documented example does). An id with `'` no longer breaks the server bundle. diff --git a/.changeset/start-server-fn-stable-ids.md b/.changeset/start-server-fn-stable-ids.md deleted file mode 100644 index f54fc7b2e5c..00000000000 --- a/.changeset/start-server-fn-stable-ids.md +++ /dev/null @@ -1,9 +0,0 @@ ---- -'@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/` 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). diff --git a/docs/start/framework/react/guide/server-functions.md b/docs/start/framework/react/guide/server-functions.md index 10e3a0a6f04..879aa9e8670 100644 --- a/docs/start/framework/react/guide/server-functions.md +++ b/docs/start/framework/react/guide/server-functions.md @@ -418,21 +418,16 @@ Handle request cancellation with `AbortSignal` for long-running operations. 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 short hashes (22 characters, from SHA256) of the file and function name, to keep bundles compact and avoid leaking file paths. +By default, IDs are SHA256 hashes of the same seed 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. -The generator receives: +Return `undefined` (or an empty string) to use the default ID. Prefer deterministic inputs (filename + functionName) so IDs remain stable between builds. -- `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`). - -Return `undefined` to use the default ID. Prefer deterministic inputs (filename + functionName) so IDs remain stable between builds. - -The ID becomes one segment of the server function's URL (`/_serverFn/`), unencoded, so it may contain only ASCII letters, digits, `-`, `.`, `_`, `~` and `$`, and may not be `.` or `..`. Any other ID fails the build with an error naming the file, the function and the ID. `filename` contains `/`, so hash it (as in the example below) rather than embedding it as is. +The ID becomes one segment of the server function's URL (`/_serverFn/`), unencoded, so use only ASCII letters, digits, `-`, `.`, `_`, `~` and `$`, and not `.` or `..`. An ID with whitespace, a control or non-ASCII character, or any of `"`, `#`, `/`, `<`, `>`, `?`, `\`, `^`, `` ` ``, `{` and `}`, an ID that is `.` or `..`, or one with a percent-encoding the server reads as another ID (`%41` for `A`, lowercase hex, `%2E`) or rejects, fails the build with an error naming the file, the function and the ID: the request would reach another function or none. Other characters (`%`, `+`, `:`, `;`, `@`, `'`, `|`, `[`, `]`, …) still work, with a deprecation warning: the next major version rejects them, since proxies and servers may encode or normalise them in the URL. `filename` contains `/`, so hash it (as in the example below) rather than embedding it as is. Please note that this customization is **experimental** and subject to change. diff --git a/packages/start-plugin-core/src/start-compiler/compiler.ts b/packages/start-plugin-core/src/start-compiler/compiler.ts index 1b696f38cec..13193f7249a 100644 --- a/packages/start-plugin-core/src/start-compiler/compiler.ts +++ b/packages/start-plugin-core/src/start-compiler/compiler.ts @@ -16,6 +16,11 @@ import { handleCreateIsomorphicFn } from './handleCreateIsomorphicFn' import { handleEnvOnlyFn } from './handleEnvOnly' import { handleClientOnlyJSX } from './handleClientOnlyJSX' import { cleanId } from './utils' +import { + customIdCheck, + customIdError, + customIdWarning, +} from './server-fn-id-check' import type { CompilationContext, DevServerFnModuleSpecifierEncoder, @@ -96,19 +101,6 @@ export function isStartCompilerPluginEnabledForEnv( return isStartCompilerEnvironmentEnabled(plugin.environment, env) } -/** - * An id the URL carries whole as one path segment (`TSS_SERVER_FN_BASE + id` - * unencoded; the server reads the segment back as is): RFC 3986 unreserved - * characters, which no client, proxy or server encodes, decodes or - * normalises, and `$`, which `functionName` can hold; not a dot segment, - * which URL parsing removes. - */ -const URL_SEGMENT_ID = /^[A-Za-z0-9\-._~$]+$/ - -function isUrlSegmentId(id: string): boolean { - return URL_SEGMENT_ID.test(id) && id !== '.' && id !== '..' -} - function isStartCompilerEnvironmentEnabled( environment: | StartCompilerEnvironment @@ -642,16 +634,17 @@ export class StartCompiler { /** * Generates a unique function ID for a server function. * In dev mode, uses a base64-encoded JSON with file path and export name. - * In build mode, uses a 22-character SHA256 hash or custom generator. + * In build mode, uses SHA256 hash or custom generator. */ - private generateFunctionId(opts: { - filename: string - functionName: string - generatorFunctionName: string - stableKey: string - variableName: string - extractedFilename: string - }): string { + private generateFunctionId( + opts: { + filename: string + functionName: string + variableName: string + extractedFilename: string + }, + warn: ((message: string) => void) | undefined, + ): string { if (this.mode === 'dev') { // In dev, encode the file path and export name for direct lookup. // Each bundler adapter supplies its own strategy for encoding @@ -676,7 +669,7 @@ export class StartCompiler { } // Production build: use custom generator or hash - const entryId = `${opts.filename}#${opts.stableKey}` + const entryId = `${opts.filename}--${opts.functionName}` let functionId = this.entryIdToFunctionId.get(entryId) if (functionId === undefined) { const knownFn = Object.values(this.options.getKnownServerFns()).find( @@ -692,20 +685,25 @@ export class StartCompiler { if (functionId === undefined && this.options.generateFunctionId) { functionId = this.options.generateFunctionId({ filename: opts.filename, - functionName: opts.generatorFunctionName, + functionName: opts.functionName, }) - if (functionId !== undefined && !isUrlSegmentId(functionId)) { - throw new Error( - `[tanstack-start] serverFns.generateFunctionId returned ${JSON.stringify(functionId)} for the server function ${opts.variableName} (functionName "${opts.generatorFunctionName}") of ${opts.filename}. A server function id is one path segment of its URL: use only ASCII letters, digits, "-", ".", "_", "~" and "$" (not "." or "..").`, - ) + if (functionId) { + const subject = { + name: opts.variableName, + functionName: opts.functionName, + rootRel: opts.filename, + } + const check = customIdCheck(functionId) + if (check === 'error') { + throw new Error(customIdError(subject, functionId)) + } + if (check === 'deprecated') { + warn?.(customIdWarning(subject, functionId)) + } } } - if (functionId === undefined) { - functionId = crypto - .createHash('sha256') - .update(entryId) - .digest('base64url') - .slice(0, 22) + if (!functionId) { + functionId = crypto.createHash('sha256').update(entryId).digest('hex') } // Deduplicate in case the generated id conflicts with an existing id if (this.functionIds.has(functionId)) { @@ -1425,7 +1423,7 @@ export class StartCompiler { })(), warn: warnFn, - generateFunctionId: (opts) => this.generateFunctionId(opts), + generateFunctionId: (opts) => this.generateFunctionId(opts, warnFn), getKnownServerFns: this.options.getKnownServerFns, serverFnProviderModuleDirectives: this.options.serverFnProviderModuleDirectives, diff --git a/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts b/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts index 8ed9d2bfb4d..dd152bdaa46 100644 --- a/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts +++ b/packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts @@ -209,8 +209,6 @@ export function handleCreateServerFn( // Track function names to ensure uniqueness within this file const functionNameSet = new Set() - const generatorFunctionNameSet = new Set() - const stableKeyCount = new Map() const exportNames = new Set() const serverFnsById: Record = {} @@ -247,33 +245,16 @@ export function handleCreateServerFn( // Generate unique function name with _createServerFn_handler suffix // The function name is derived from the variable name - const functionName = allocateHandlerName( - existingVariableName, - functionNameSet, - ) - // What a custom `generateFunctionId` receives: the same name, with - // characters outside `[A-Za-z0-9_$]` (a non-ASCII identifier) as `_`, so - // an id that embeds it stays a URL path segment - const generatorFunctionName = allocateHandlerName( - existingVariableName.replace(/[^A-Za-z0-9_$]/g, '_'), - generatorFunctionNameSet, - ) - - // The stable key seeds the default id: the variable name, with `#n` for - // repeats in the file - const keyCount = stableKeyCount.get(existingVariableName) ?? 0 - stableKeyCount.set(existingVariableName, keyCount + 1) - const stableKey = - keyCount === 0 - ? existingVariableName - : `${existingVariableName}#${keyCount}` + let functionName = `${existingVariableName}_createServerFn_handler` + while (functionNameSet.has(functionName)) { + functionName = incrementFunctionNameVersion(functionName) + } + functionNameSet.add(functionName) // Generate function ID using pre-computed relative filename const functionId = context.generateFunctionId({ filename: relativeFilename, functionName, - generatorFunctionName, - stableKey, variableName: existingVariableName, extractedFilename, }) @@ -483,19 +464,6 @@ function makeIdentifierSafe(identifier: string): string { .replace(/^_|_$/g, '') // Trim leading/trailing underscores } -/** - * `_createServerFn_handler`, then `_1`, `_2`… for a name taken earlier - * in the file. - */ -function allocateHandlerName(base: string, taken: Set): string { - let name = `${base}_createServerFn_handler` - while (taken.has(name)) { - name = incrementFunctionNameVersion(name) - } - taken.add(name) - return name -} - /** * Increments the version number suffix on a function name. */ diff --git a/packages/start-plugin-core/src/start-compiler/server-fn-id-check.ts b/packages/start-plugin-core/src/start-compiler/server-fn-id-check.ts new file mode 100644 index 00000000000..7ddaafce5b1 --- /dev/null +++ b/packages/start-plugin-core/src/start-compiler/server-fn-id-check.ts @@ -0,0 +1,75 @@ +/** + * Which ids a custom `serverFns.generateFunctionId` may return. The client + * puts the id after `TSS_SERVER_FN_BASE` unencoded and the server reads that + * path segment back, so an id has to survive that trip whole. + */ + +interface CustomIdSubject { + /** The server function's variable name. */ + name: string + /** The `functionName` the generator received. */ + functionName: string + /** The defining file, relative to the root. */ + rootRel: string +} + +/** + * `error`: the id never reached its handler: a character that splits or + * ends the path or that a client percent-encodes, a dot segment, which URL + * parsing removes, or a percent-encoding the server decodes to another id + * or rejects (`readBack`). `deprecated`: an ASCII character outside RFC + * 3986's unreserved set and `$`, which reaches the server but which proxies + * and servers may encode or normalise. + */ +export function customIdCheck(id: string): 'ok' | 'deprecated' | 'error' { + if ( + id === '.' || + id === '..' || + /[^\x21-\x7e]|["#/<>?\\^`{}]/.test(id) || + (id.includes('%') && readBack(id) !== id) + ) { + return 'error' + } + return /^[A-Za-z0-9\-._~$]+$/.test(id) ? 'ok' : 'deprecated' +} + +/** + * The id the server reads from the URL of an id holding `%`: the request + * URL's path, decoded as Start's server does when the path holds `%` + * (router-core `getNormalizedURL`, `decodePath`: `decodeURI` keeping `%25` + * and `%5C`, then control characters and `"<>\`{}` encoded again) and + * parsed again; `undefined` when the decode throws (the server answers 400). + */ +function readBack(id: string): string | undefined { + const base = '/_/' + const { pathname } = new URL(base + id, 'http://localhost') + let decoded = '' + try { + for (const part of pathname.split(/(%25|%5C)/i)) { + decoded += /^%(25|5C)$/i.test(part) + ? part + : decodeURI(part).replace( + // eslint-disable-next-line no-control-regex + /[\x00-\x1f\x7f"<>`{}]/g, + (ch) => + `%${ch.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0')}`, + ) + } + } catch { + return undefined + } + const read = new URL(decoded, 'http://localhost').pathname + return read.startsWith(base) + ? read.slice(base.length).split('/')[0] + : undefined +} + +/** The build error of a custom id `customIdCheck` refuses. */ +export function customIdError(fn: CustomIdSubject, id: string): string { + return `[tanstack-start] serverFns.generateFunctionId returned ${JSON.stringify(id)} for the server function ${fn.name} (functionName "${fn.functionName}") of ${fn.rootRel}. A server function id is one path segment of its URL: it cannot hold whitespace, control or non-ASCII characters or any of " # / < > ? \\ ^ \` { }, cannot be "." or "..", and cannot hold a percent-encoding the server reads as another id ("%41") or rejects. Use ASCII letters, digits, "-", ".", "_", "~" and "$".` +} + +/** The build warning of a custom id `customIdCheck` deprecates. */ +export function customIdWarning(fn: CustomIdSubject, id: string): string { + return `[tanstack-start] serverFns.generateFunctionId returned ${JSON.stringify(id)} for the server function ${fn.name} (functionName "${fn.functionName}") of ${fn.rootRel}. Server function ids with characters other than ASCII letters, digits, "-", ".", "_", "~" and "$" are deprecated, and the next major version of TanStack Start rejects them: proxies and servers may encode or normalise them in the URL.` +} diff --git a/packages/start-plugin-core/src/start-compiler/server-fn-resolver-module.ts b/packages/start-plugin-core/src/start-compiler/server-fn-resolver-module.ts index d03bec33413..2d50376a39e 100644 --- a/packages/start-plugin-core/src/start-compiler/server-fn-resolver-module.ts +++ b/packages/start-plugin-core/src/start-compiler/server-fn-resolver-module.ts @@ -31,7 +31,8 @@ export function generateServerFnResolverModule( ? `,\n isClientReferenced: ${fn.isClientReferenced ?? true}` : '' - return `'${id}': { + // (a custom id can hold `'`) + return `'${id.replace(/[\\']/g, '\\$&')}': { functionName: '${fn.functionName}', ${moduleAccess}${clientReferenced} }` diff --git a/packages/start-plugin-core/src/start-compiler/types.ts b/packages/start-plugin-core/src/start-compiler/types.ts index 85320307e4e..91b39f5f8a4 100644 --- a/packages/start-plugin-core/src/start-compiler/types.ts +++ b/packages/start-plugin-core/src/start-compiler/types.ts @@ -89,13 +89,8 @@ export interface ServerFn { */ export type GenerateFunctionIdFn = (opts: { filename: string - /** Name of the extracted handler export. */ functionName: string - /** The `functionName` a custom `generateFunctionId` receives. */ - generatorFunctionName: string - /** The server fn's variable name, with `#n` for repeats in the same file. */ - stableKey: string - /** The server fn's variable name. */ + /** The server function's variable name, for diagnostics. */ variableName: string extractedFilename: string }) => string @@ -103,10 +98,12 @@ export type GenerateFunctionIdFn = (opts: { /** * Optional version that allows returning undefined to use default ID generation. */ -export type GenerateFunctionIdFnOptional = (opts: { - filename: string - functionName: string -}) => string | undefined +export type GenerateFunctionIdFnOptional = ( + opts: Omit< + Parameters[0], + 'extractedFilename' | 'variableName' + >, +) => string | undefined /** * Function type for generating replacement code for server functions. diff --git a/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts b/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts index a6a1d952430..e80f292891a 100644 --- a/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts +++ b/packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts @@ -116,7 +116,9 @@ describe('createServerFn compiles correctly', async () => { "import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' const myServerFn = createServerFn().handler( - createClientRpc('WYpJIMNQF5bXNZqddn4S08'), + createClientRpc( + '2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b', + ), ) " `) @@ -188,7 +190,9 @@ describe('createServerFn compiles correctly', async () => { "import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' const myServerFn = createServerFn().handler( - createClientRpc('WYpJIMNQF5bXNZqddn4S08'), + createClientRpc( + '2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b', + ), ) " `) @@ -199,7 +203,9 @@ describe('createServerFn compiles correctly', async () => { "import { createSsrRpc } from '@tanstack/react-start/ssr-rpc' import { createServerFn } from '@tanstack/react-start' const myServerFn = createServerFn().handler( - createSsrRpc('WYpJIMNQF5bXNZqddn4S08'), + createSsrRpc( + '2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b', + ), ) " `) @@ -213,7 +219,11 @@ describe('createServerFn compiles correctly', async () => { return 'hello from the server' } const myServerFn_createServerFn_handler = createServerRpc( - { id: 'WYpJIMNQF5bXNZqddn4S08', name: 'myServerFn', filename: 'src/test.ts' }, + { + id: '2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b', + name: 'myServerFn', + filename: 'src/test.ts', + }, (opts) => myServerFn.__executeServer(opts), ) const myServerFn = createServerFn().handler( @@ -245,7 +255,9 @@ describe('createServerFn compiles correctly', async () => { "import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' export const getUsersFn = createServerFn().handler( - createClientRpc('hUsF8j58VORzR8Zh0TWYHu'), + createClientRpc( + 'a78c63d4bb3c0b10a8b70902c73611fbf0b9229e0807b065c03c939f9c0ce100', + ), ) " `) @@ -273,7 +285,9 @@ describe('createServerFn compiles correctly', async () => { import { createServerFn } from '@tanstack/react-start' import { getUsers } from './server' export const getUsersFn = createServerFn().handler( - createClientRpc('hUsF8j58VORzR8Zh0TWYHu'), + createClientRpc( + 'a78c63d4bb3c0b10a8b70902c73611fbf0b9229e0807b065c03c939f9c0ce100', + ), ) console.log(getUsers) " @@ -343,10 +357,14 @@ describe('createServerFn compiles correctly', async () => { "import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' export const exportedFn = createServerFn().handler( - createClientRpc('2tiakNpA4QDPhHt7gcEQxW'), + createClientRpc( + 'c306c96e9256c7604f2a6022c4c94eb89f863274c022bc45b03970f067ea9864', + ), ) const nonExportedFn = createServerFn().handler( - createClientRpc('p5iUR0EOmj-YLvWU2l_Tw6'), + createClientRpc( + 'f4403dc0b18e216dfe0a9711cab028bc1b9768175daa9236d7115e29c99d76c2', + ), ) " `) @@ -364,10 +382,14 @@ describe('createServerFn compiles correctly', async () => { "import { createSsrRpc } from '@tanstack/react-start/ssr-rpc' import { createServerFn } from '@tanstack/react-start' export const exportedFn = createServerFn().handler( - createSsrRpc('2tiakNpA4QDPhHt7gcEQxW'), + createSsrRpc( + 'c306c96e9256c7604f2a6022c4c94eb89f863274c022bc45b03970f067ea9864', + ), ) const nonExportedFn = createServerFn().handler( - createSsrRpc('p5iUR0EOmj-YLvWU2l_Tw6'), + createSsrRpc( + 'f4403dc0b18e216dfe0a9711cab028bc1b9768175daa9236d7115e29c99d76c2', + ), ) " `) @@ -386,7 +408,11 @@ describe('createServerFn compiles correctly', async () => { import { createServerFn } from '@tanstack/react-start' const exportedVar = 'exported' const exportedFn_createServerFn_handler = createServerRpc( - { id: '2tiakNpA4QDPhHt7gcEQxW', name: 'exportedFn', filename: 'src/test.ts' }, + { + id: 'c306c96e9256c7604f2a6022c4c94eb89f863274c022bc45b03970f067ea9864', + name: 'exportedFn', + filename: 'src/test.ts', + }, (opts) => exportedFn.__executeServer(opts), ) const exportedFn = createServerFn().handler( @@ -398,7 +424,7 @@ describe('createServerFn compiles correctly', async () => { const nonExportedVar = 'non-exported' const nonExportedFn_createServerFn_handler = createServerRpc( { - id: 'p5iUR0EOmj-YLvWU2l_Tw6', + id: 'f4403dc0b18e216dfe0a9711cab028bc1b9768175daa9236d7115e29c99d76c2', name: 'nonExportedFn', filename: 'src/test.ts', }, @@ -682,153 +708,6 @@ describe('createServerFn compiles correctly', async () => { ) }) - function createBuildCompiler( - generateFunctionId: (opts: { - filename: string - functionName: string - }) => string | undefined, - ) { - return new StartCompiler({ - env: 'client', - ...getDefaultTestOptions('client'), - mode: 'build', - loadModule: async () => {}, - lookupKinds: new Set(['ServerFn']), - lookupConfigurations: [ - { - libName: '@tanstack/react-start', - rootExport: 'createServerFn', - kind: 'Root', - }, - ], - resolveId: async (id) => id, - generateFunctionId, - getKnownServerFns: () => ({}), - }) - } - - test('passes the handler name to generateFunctionId and defaults to short ids of the file and variable name', async () => { - const generateFunctionId = vi.fn(() => undefined) - const compiler = createBuildCompiler(generateFunctionId) - - const result = await compiler.compile({ - code: ` - import { createServerFn } from '@tanstack/react-start' - export const getUser = createServerFn().handler(async () => 'user') - var twice = createServerFn().handler(async () => 'a') - var twice = createServerFn().handler(async () => 'b') - export const getÜser = createServerFn().handler(async () => 'ü') - `, - id: '/test/src/users.ts', - }) - - expect(generateFunctionId.mock.calls).toEqual([ - [ - { - filename: 'src/users.ts', - functionName: 'getUser_createServerFn_handler', - }, - ], - [ - { - filename: 'src/users.ts', - functionName: 'twice_createServerFn_handler', - }, - ], - [ - { - filename: 'src/users.ts', - functionName: 'twice_createServerFn_handler_1', - }, - ], - [ - { - filename: 'src/users.ts', - functionName: 'get_ser_createServerFn_handler', - }, - ], - ]) - // base64url of sha256("src/users.ts#getUser"), "…#twice", "…#twice#1", - // "…#getÜser", first 22 characters - expect( - [...result!.code.matchAll(/createClientRpc\("([^"]+)"/g)].map( - (match) => match[1], - ), - ).toEqual([ - '5khyYluQNdT_FnYJdeNjO7', - '86c3ovB8BBp6j3lZ7Ys3-q', - 'VDV9uQu-DjlBpN8wR9Hg81', - 'fR2z51S12hblu5q1t0xfsF', - ]) - }) - - test('a repeated name loses its leading and doubled underscores in the generator input', async () => { - const generateFunctionId = vi.fn( - (_opts: { filename: string; functionName: string }) => undefined, - ) - const compiler = createBuildCompiler(generateFunctionId) - - await compiler.compile({ - code: ` - import { createServerFn } from '@tanstack/react-start' - var _x = createServerFn().handler(async () => 'a') - var _x = createServerFn().handler(async () => 'b') - export const café = createServerFn().handler(async () => 'c') - export const caf_ = createServerFn().handler(async () => 'd') - `, - id: '/test/src/users.ts', - }) - - expect( - generateFunctionId.mock.calls.map(([opts]) => opts.functionName), - ).toEqual([ - '_x_createServerFn_handler', - 'x_createServerFn_handler_1', - 'caf__createServerFn_handler', - 'caf_createServerFn_handler_1', - ]) - }) - - test.each([ - 'users/get', - 'users#get', - 'users get', - 'utilisateurs-é', - '..', - '', - ])( - 'fails on a custom id that is not one URL path segment: %j', - async (id) => { - const compiler = createBuildCompiler(() => id) - - await expect( - compiler.compile({ - code: ` - import { createServerFn } from '@tanstack/react-start' - export const getUser = createServerFn().handler(async () => 'user') - `, - id: '/test/src/users.ts', - }), - ).rejects.toThrow( - `serverFns.generateFunctionId returned ${JSON.stringify(id)} for the server function getUser (functionName "getUser_createServerFn_handler") of src/users.ts`, - ) - }, - ) - - test('accepts a custom id of unreserved characters and $', async () => { - const compiler = createBuildCompiler(() => 'Users-get.v1_~$') - - const result = await compiler.compile({ - code: ` - import { createServerFn } from '@tanstack/react-start' - export const getUser = createServerFn().handler(async () => 'user') - `, - id: '/test/src/users.ts', - }) - - expect(result!.code).toContain('createClientRpc("Users-get.v1_~$"') - }) - test('reuses deduped custom IDs across compiler instances', async () => { const serverFnsById: Record< string, @@ -911,6 +790,60 @@ describe('createServerFn compiles correctly', async () => { expect(secondResult!.code).toContain('createSsrRpc("constant_id_1"') }) + // The client puts the id in the URL unencoded and the server reads that + // path segment back: a `/`, `#` or `?` cut it short (the call ran another + // server function of the file or none), a client percent-encodes `"`, + // `\`, whitespace, control and non-ASCII characters, URL parsing removes + // dot segments, and the server decodes a percent-encoding to another id or + // rejects it. + test.each([ + 'users/get', + 'users#get', + 'users?get', + 'users"get', + 'users\\get', + 'users get', + 'users\u0007get', + 'utilisateurs-é', + '..', + 'users%41get', + 'x%c3%a9', + '%2E', + 'users%zz', + ])( + 'a custom id the URL never carries whole fails the build: %j', + async (id) => { + const compiler = new StartCompiler({ + env: 'client', + ...getDefaultTestOptions('client'), + mode: 'build', + loadModule: async () => {}, + lookupKinds: new Set(['ServerFn']), + lookupConfigurations: [ + { + libName: '@tanstack/react-start', + rootExport: 'createServerFn', + kind: 'Root', + }, + ], + resolveId: async (id) => id, + generateFunctionId: () => id, + getKnownServerFns: () => ({}), + }) + await expect( + compiler.compile({ + code: ` + import { createServerFn } from '@tanstack/react-start' + export const getUser = createServerFn().handler(async () => 'user') + `, + id: '/test/src/users.ts', + }), + ).rejects.toThrow( + `[tanstack-start] serverFns.generateFunctionId returned ${JSON.stringify(id)} for the server function getUser (functionName "getUser_createServerFn_handler") of src/users.ts.`, + ) + }, + ) + test('should resolve createServerFn from the same binding as a known root export', async () => { const virtualModules: Record = { '@tanstack/start-client-core': ` diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructured.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructured.tsx index 17be276f518..88e8607da27 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructured.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructured.tsx @@ -1,23 +1,39 @@ import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' export const withUseServer = createServerFn({ method: 'GET' }).handler( - createClientRpc('80v0H7Z-40eAvJELD4yOZd'), + createClientRpc( + 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + ), ) export const withArrowFunction = createServerFn({ method: 'GET' }).handler( - createClientRpc('3CuVcIf56mXjOm391b0xfG'), + createClientRpc( + 'a311dfd9a6554bc0bb9371ef4fbc72c852d0b5532829a2613a29157b8595a793', + ), ) export const withArrowFunctionAndFunction = createServerFn({ method: 'GET', -}).handler(createClientRpc('7-JuMRVpFOk-B1xNtJ2lhL')) +}).handler( + createClientRpc( + 'dbe1f77c6ea7590bce3212a75b06d3cee83b425bcf493014cb47462ef725e36e', + ), +) export const withoutUseServer = createServerFn({ method: 'GET' }).handler( - createClientRpc('XbUOHXbrBke2Xebq_cpd9s'), + createClientRpc( + 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', + ), ) export const withVariable = createServerFn({ method: 'GET' }).handler( - createClientRpc('id9L9xtkvHA0G7YPmv4k5B'), + createClientRpc( + '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', + ), ) export const withZodValidator = createServerFn({ method: 'GET' }).handler( - createClientRpc('Y0OXye37qmukl8eWdR6BXL'), + createClientRpc( + '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', + ), ) export const withValidatorFn = createServerFn({ method: 'GET' }).handler( - createClientRpc('lJiDYujGD-4bdw-88mWATZ'), + createClientRpc( + '5cf922ac733d835fee496964cbf0b4b4067cb43d51f2efa484d072e6bb7dae44', + ), ) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructuredRename.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructuredRename.tsx index f464ca8f032..34e0c452b62 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructuredRename.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnDestructuredRename.tsx @@ -1,14 +1,22 @@ import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn as serverFn } from '@tanstack/react-start' export const withUseServer = serverFn({ method: 'GET' }).handler( - createClientRpc('80v0H7Z-40eAvJELD4yOZd'), + createClientRpc( + 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + ), ) export const withoutUseServer = serverFn({ method: 'GET' }).handler( - createClientRpc('XbUOHXbrBke2Xebq_cpd9s'), + createClientRpc( + 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', + ), ) export const withVariable = serverFn({ method: 'GET' }).handler( - createClientRpc('id9L9xtkvHA0G7YPmv4k5B'), + createClientRpc( + '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', + ), ) export const withZodValidator = serverFn({ method: 'GET' }).handler( - createClientRpc('Y0OXye37qmukl8eWdR6BXL'), + createClientRpc( + '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', + ), ) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnStarImport.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnStarImport.tsx index 5172d8108c8..2cea070645a 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnStarImport.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnStarImport.tsx @@ -2,13 +2,29 @@ import { createClientRpc } from '@tanstack/react-start/client-rpc' import * as TanStackStart from '@tanstack/react-start' export const withUseServer = TanStackStart.createServerFn({ method: 'GET', -}).handler(createClientRpc('80v0H7Z-40eAvJELD4yOZd')) +}).handler( + createClientRpc( + 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + ), +) export const withoutUseServer = TanStackStart.createServerFn({ method: 'GET', -}).handler(createClientRpc('XbUOHXbrBke2Xebq_cpd9s')) +}).handler( + createClientRpc( + 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', + ), +) export const withVariable = TanStackStart.createServerFn({ method: 'GET', -}).handler(createClientRpc('id9L9xtkvHA0G7YPmv4k5B')) +}).handler( + createClientRpc( + '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', + ), +) export const withZodValidator = TanStackStart.createServerFn({ method: 'GET', -}).handler(createClientRpc('Y0OXye37qmukl8eWdR6BXL')) +}).handler( + createClientRpc( + '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', + ), +) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnValidator.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnValidator.tsx index 4d3b8dc81a9..97253672a63 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnValidator.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/createServerFnValidator.tsx @@ -1,5 +1,7 @@ import { createClientRpc } from '@tanstack/react-start/client-rpc' import { createServerFn } from '@tanstack/react-start' export const withUseServer = createServerFn({ method: 'GET' }).handler( - createClientRpc('80v0H7Z-40eAvJELD4yOZd'), + createClientRpc( + 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + ), ) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx index 1fcf8516e27..889c1fbf4e3 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/factory.tsx @@ -13,10 +13,14 @@ const adminMiddleware = createMiddleware({ type: 'function' }).server( export const createAuthServerFn = createServerFn().middleware([authMiddleware]) const createAdminServerFn = createAuthServerFn().middleware([adminMiddleware]) export const myAuthedFn = createAuthServerFn().handler( - createClientRpc('8wI54n7XLosU5JDSs5CD3r'), + createClientRpc( + '3bb88b23926fa224cea3dcfb877026757733d95b42e3b66cf7f06bb89dab06a3', + ), ) export const deleteUserFn = createAdminServerFn().handler( - createClientRpc('_zazY1Jgzd8oXXcuPROEv9'), + createClientRpc( + '44d9483a9412daaacaa5a8aee6b02294333a0e61b5d3b17ba6bcb34fd2bdce31', + ), ) function createFakeFn() { return { diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/client/isomorphic-fns.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/client/isomorphic-fns.tsx index acd6c922543..8e09a57d372 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/client/isomorphic-fns.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/client/isomorphic-fns.tsx @@ -6,13 +6,17 @@ const getEnv = createIsomorphicFn() .server(() => 'server') .client(() => 'client') const getServerEnv = createServerFn().handler( - createClientRpc('Jc6GTcykFaVJAwm-a0BofP'), + createClientRpc( + '6049dd46bc00e0980e387f3e74924b40e92f37634a3fe74a1a1facda9e9207c2', + ), ) const getEcho = createIsomorphicFn() .server((input: string) => 'server received ' + input) .client((input) => 'client received ' + input) const getServerEcho = createServerFn().handler( - createClientRpc('s0jNQTWKXVmb2EhbCIBxAU'), + createClientRpc( + 'ebb82e0293952b7b1dde3f664dec4c139471c046d06d4dc4ca172f4f45e386d1', + ), ) export const Route = createFileRoute('/isomorphic-fns')({ component: RouteComponent, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructured.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructured.tsx index 8de2685ba73..efa8f395d59 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructured.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructured.tsx @@ -2,23 +2,41 @@ import { createSsrRpc } from '@tanstack/react-start/ssr-rpc' import { createServerFn } from '@tanstack/react-start' import { z } from 'zod' export const withUseServer = createServerFn({ method: 'GET' }).handler( - createSsrRpc('80v0H7Z-40eAvJELD4yOZd'), + createSsrRpc( + 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + ), ) export const withArrowFunction = createServerFn({ method: 'GET' }).handler( - createSsrRpc('3CuVcIf56mXjOm391b0xfG'), + createSsrRpc( + 'a311dfd9a6554bc0bb9371ef4fbc72c852d0b5532829a2613a29157b8595a793', + ), ) export const withArrowFunctionAndFunction = createServerFn({ method: 'GET', -}).handler(createSsrRpc('7-JuMRVpFOk-B1xNtJ2lhL')) +}).handler( + createSsrRpc( + 'dbe1f77c6ea7590bce3212a75b06d3cee83b425bcf493014cb47462ef725e36e', + ), +) export const withoutUseServer = createServerFn({ method: 'GET' }).handler( - createSsrRpc('XbUOHXbrBke2Xebq_cpd9s'), + createSsrRpc( + 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', + ), ) export const withVariable = createServerFn({ method: 'GET' }).handler( - createSsrRpc('id9L9xtkvHA0G7YPmv4k5B'), + createSsrRpc( + '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', + ), ) export const withZodValidator = createServerFn({ method: 'GET' }).handler( - createSsrRpc('Y0OXye37qmukl8eWdR6BXL'), + createSsrRpc( + '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', + ), ) export const withValidatorFn = createServerFn({ method: 'GET' }) .validator(z.number()) - .handler(createSsrRpc('lJiDYujGD-4bdw-88mWATZ')) + .handler( + createSsrRpc( + '5cf922ac733d835fee496964cbf0b4b4067cb43d51f2efa484d072e6bb7dae44', + ), + ) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructuredRename.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructuredRename.tsx index af091fc1ca5..7088a4bc69f 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructuredRename.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnDestructuredRename.tsx @@ -1,14 +1,22 @@ import { createSsrRpc } from '@tanstack/react-start/ssr-rpc' import { createServerFn as serverFn } from '@tanstack/react-start' export const withUseServer = serverFn({ method: 'GET' }).handler( - createSsrRpc('80v0H7Z-40eAvJELD4yOZd'), + createSsrRpc( + 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + ), ) export const withoutUseServer = serverFn({ method: 'GET' }).handler( - createSsrRpc('XbUOHXbrBke2Xebq_cpd9s'), + createSsrRpc( + 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', + ), ) export const withVariable = serverFn({ method: 'GET' }).handler( - createSsrRpc('id9L9xtkvHA0G7YPmv4k5B'), + createSsrRpc( + '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', + ), ) export const withZodValidator = serverFn({ method: 'GET' }).handler( - createSsrRpc('Y0OXye37qmukl8eWdR6BXL'), + createSsrRpc( + '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', + ), ) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnStarImport.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnStarImport.tsx index 80ee3e59b74..899aaf3d0db 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnStarImport.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnStarImport.tsx @@ -2,13 +2,29 @@ import { createSsrRpc } from '@tanstack/react-start/ssr-rpc' import * as TanStackStart from '@tanstack/react-start' export const withUseServer = TanStackStart.createServerFn({ method: 'GET', -}).handler(createSsrRpc('80v0H7Z-40eAvJELD4yOZd')) +}).handler( + createSsrRpc( + 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + ), +) export const withoutUseServer = TanStackStart.createServerFn({ method: 'GET', -}).handler(createSsrRpc('XbUOHXbrBke2Xebq_cpd9s')) +}).handler( + createSsrRpc( + 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', + ), +) export const withVariable = TanStackStart.createServerFn({ method: 'GET', -}).handler(createSsrRpc('id9L9xtkvHA0G7YPmv4k5B')) +}).handler( + createSsrRpc( + '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', + ), +) export const withZodValidator = TanStackStart.createServerFn({ method: 'GET', -}).handler(createSsrRpc('Y0OXye37qmukl8eWdR6BXL')) +}).handler( + createSsrRpc( + '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', + ), +) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnValidator.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnValidator.tsx index bef88f4ab3a..e1d136311b3 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnValidator.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/createServerFnValidator.tsx @@ -3,4 +3,8 @@ import { createServerFn } from '@tanstack/react-start' import { z } from 'zod' export const withUseServer = createServerFn({ method: 'GET' }) .validator(z.number()) - .handler(createSsrRpc('80v0H7Z-40eAvJELD4yOZd')) + .handler( + createSsrRpc( + 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', + ), + ) diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/factory.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/factory.tsx index 6f4c03cc80f..ab19c3e1f88 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/factory.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/factory.tsx @@ -13,10 +13,14 @@ const adminMiddleware = createMiddleware({ type: 'function' }).server( export const createAuthServerFn = createServerFn().middleware([authMiddleware]) const createAdminServerFn = createAuthServerFn().middleware([adminMiddleware]) export const myAuthedFn = createAuthServerFn().handler( - createSsrRpc('8wI54n7XLosU5JDSs5CD3r'), + createSsrRpc( + '3bb88b23926fa224cea3dcfb877026757733d95b42e3b66cf7f06bb89dab06a3', + ), ) export const deleteUserFn = createAdminServerFn().handler( - createSsrRpc('_zazY1Jgzd8oXXcuPROEv9'), + createSsrRpc( + '44d9483a9412daaacaa5a8aee6b02294333a0e61b5d3b17ba6bcb34fd2bdce31', + ), ) function createFakeFn() { return { diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/isomorphic-fns.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/isomorphic-fns.tsx index 0615b930e39..cfefad2bfbd 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/isomorphic-fns.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-caller/isomorphic-fns.tsx @@ -6,14 +6,20 @@ const getEnv = createIsomorphicFn() .server(() => 'server') .client(() => 'client') const getServerEnv = createServerFn().handler( - createSsrRpc('Jc6GTcykFaVJAwm-a0BofP'), + createSsrRpc( + '6049dd46bc00e0980e387f3e74924b40e92f37634a3fe74a1a1facda9e9207c2', + ), ) const getEcho = createIsomorphicFn() .server((input: string) => 'server received ' + input) .client((input) => 'client received ' + input) const getServerEcho = createServerFn() .validator((input: string) => input) - .handler(createSsrRpc('s0jNQTWKXVmb2EhbCIBxAU')) + .handler( + createSsrRpc( + 'ebb82e0293952b7b1dde3f664dec4c139471c046d06d4dc4ca172f4f45e386d1', + ), + ) export const Route = createFileRoute('/isomorphic-fns')({ component: RouteComponent, loader() { diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructured.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructured.tsx index b58fef66d14..3bc4879c141 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructured.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructured.tsx @@ -3,7 +3,7 @@ import { createServerFn } from '@tanstack/react-start' import { z } from 'zod' const withUseServer_createServerFn_handler = createServerRpc( { - id: '80v0H7Z-40eAvJELD4yOZd', + id: 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', name: 'withUseServer', filename: 'src/test.ts', }, @@ -21,7 +21,7 @@ const withUseServer = createServerFn({ method: 'GET' }).handler( ) const withArrowFunction_createServerFn_handler = createServerRpc( { - id: '3CuVcIf56mXjOm391b0xfG', + id: 'a311dfd9a6554bc0bb9371ef4fbc72c852d0b5532829a2613a29157b8595a793', name: 'withArrowFunction', filename: 'src/test.ts', }, @@ -33,7 +33,7 @@ const withArrowFunction = createServerFn({ method: 'GET' }).handler( ) const withArrowFunctionAndFunction_createServerFn_handler = createServerRpc( { - id: '7-JuMRVpFOk-B1xNtJ2lhL', + id: 'dbe1f77c6ea7590bce3212a75b06d3cee83b425bcf493014cb47462ef725e36e', name: 'withArrowFunctionAndFunction', filename: 'src/test.ts', }, @@ -45,7 +45,7 @@ const withArrowFunctionAndFunction = createServerFn({ method: 'GET' }).handler( ) const withoutUseServer_createServerFn_handler = createServerRpc( { - id: 'XbUOHXbrBke2Xebq_cpd9s', + id: 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', name: 'withoutUseServer', filename: 'src/test.ts', }, @@ -63,7 +63,7 @@ const withoutUseServer = createServerFn({ method: 'GET' }).handler( ) const withVariable_createServerFn_handler = createServerRpc( { - id: 'id9L9xtkvHA0G7YPmv4k5B', + id: '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', name: 'withVariable', filename: 'src/test.ts', }, @@ -90,7 +90,7 @@ function zodValidator( } const withZodValidator_createServerFn_handler = createServerRpc( { - id: 'Y0OXye37qmukl8eWdR6BXL', + id: '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', name: 'withZodValidator', filename: 'src/test.ts', }, @@ -104,7 +104,7 @@ const withZodValidator = createServerFn({ method: 'GET' }).handler( ) const withValidatorFn_createServerFn_handler = createServerRpc( { - id: 'lJiDYujGD-4bdw-88mWATZ', + id: '5cf922ac733d835fee496964cbf0b4b4067cb43d51f2efa484d072e6bb7dae44', name: 'withValidatorFn', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructuredRename.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructuredRename.tsx index 840e0aead03..775c32b645d 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructuredRename.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnDestructuredRename.tsx @@ -3,7 +3,7 @@ import { createServerFn as serverFn } from '@tanstack/react-start' import { z } from 'zod' const withUseServer_createServerFn_handler = createServerRpc( { - id: '80v0H7Z-40eAvJELD4yOZd', + id: 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', name: 'withUseServer', filename: 'src/test.ts', }, @@ -21,7 +21,7 @@ const withUseServer = serverFn({ method: 'GET' }).handler( ) const withoutUseServer_createServerFn_handler = createServerRpc( { - id: 'XbUOHXbrBke2Xebq_cpd9s', + id: 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', name: 'withoutUseServer', filename: 'src/test.ts', }, @@ -39,7 +39,7 @@ const withoutUseServer = serverFn({ method: 'GET' }).handler( ) const withVariable_createServerFn_handler = createServerRpc( { - id: 'id9L9xtkvHA0G7YPmv4k5B', + id: '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', name: 'withVariable', filename: 'src/test.ts', }, @@ -66,7 +66,7 @@ function zodValidator( } const withZodValidator_createServerFn_handler = createServerRpc( { - id: 'Y0OXye37qmukl8eWdR6BXL', + id: '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', name: 'withZodValidator', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnStarImport.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnStarImport.tsx index 0fafe3c9233..bc18c08855f 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnStarImport.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnStarImport.tsx @@ -3,7 +3,7 @@ import * as TanStackStart from '@tanstack/react-start' import { z } from 'zod' const withUseServer_createServerFn_handler = createServerRpc( { - id: '80v0H7Z-40eAvJELD4yOZd', + id: 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', name: 'withUseServer', filename: 'src/test.ts', }, @@ -22,7 +22,7 @@ const withUseServer = TanStackStart.createServerFn({ method: 'GET' }).handler( ) const withoutUseServer_createServerFn_handler = createServerRpc( { - id: 'XbUOHXbrBke2Xebq_cpd9s', + id: 'c0d9fdeb2b01aafecc782f439416b86f666cb5334ac9a934546b177e5698bbb2', name: 'withoutUseServer', filename: 'src/test.ts', }, @@ -39,7 +39,7 @@ const withoutUseServer = TanStackStart.createServerFn({ }) const withVariable_createServerFn_handler = createServerRpc( { - id: 'id9L9xtkvHA0G7YPmv4k5B', + id: '7d36cb30c30bd688b9773bd7f85a0d51d83f868255e127605e4f236615fefe70', name: 'withVariable', filename: 'src/test.ts', }, @@ -66,7 +66,7 @@ function zodValidator( } const withZodValidator_createServerFn_handler = createServerRpc( { - id: 'Y0OXye37qmukl8eWdR6BXL', + id: '43606a369c85444a332f701d6399564b6a3691aabab63dfc9a0b7c341fb798f3', name: 'withZodValidator', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnValidator.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnValidator.tsx index ee180e145ba..f62fa718d61 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnValidator.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/createServerFnValidator.tsx @@ -3,7 +3,7 @@ import { createServerFn } from '@tanstack/react-start' import { z } from 'zod' const withUseServer_createServerFn_handler = createServerRpc( { - id: '80v0H7Z-40eAvJELD4yOZd', + id: 'b6fe31e85836ae6c65d8ab262dd1af6a68620aeef609029d50127f4cceaaf320', name: 'withUseServer', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/factory.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/factory.tsx index 43b148d6d07..26298edd567 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/factory.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/factory.tsx @@ -13,7 +13,11 @@ const adminMiddleware = createMiddleware({ type: 'function' }).server( const createAuthServerFn = createServerFn().middleware([authMiddleware]) const createAdminServerFn = createAuthServerFn().middleware([adminMiddleware]) const myAuthedFn_createServerFn_handler = createServerRpc( - { id: '8wI54n7XLosU5JDSs5CD3r', name: 'myAuthedFn', filename: 'src/test.ts' }, + { + id: '3bb88b23926fa224cea3dcfb877026757733d95b42e3b66cf7f06bb89dab06a3', + name: 'myAuthedFn', + filename: 'src/test.ts', + }, (opts) => myAuthedFn.__executeServer(opts), ) const myAuthedFn = createAuthServerFn().handler( @@ -24,7 +28,7 @@ const myAuthedFn = createAuthServerFn().handler( ) const deleteUserFn_createServerFn_handler = createServerRpc( { - id: '_zazY1Jgzd8oXXcuPROEv9', + id: '44d9483a9412daaacaa5a8aee6b02294333a0e61b5d3b17ba6bcb34fd2bdce31', name: 'deleteUserFn', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/isomorphic-fns.tsx b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/isomorphic-fns.tsx index df93893c8bc..3ae86226834 100644 --- a/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/isomorphic-fns.tsx +++ b/packages/start-plugin-core/tests/createServerFn/snapshots/server-provider/isomorphic-fns.tsx @@ -5,7 +5,7 @@ const getEnv = createIsomorphicFn() .client(() => 'client') const getServerEnv_createServerFn_handler = createServerRpc( { - id: 'Jc6GTcykFaVJAwm-a0BofP', + id: '6049dd46bc00e0980e387f3e74924b40e92f37634a3fe74a1a1facda9e9207c2', name: 'getServerEnv', filename: 'src/test.ts', }, @@ -20,7 +20,7 @@ const getEcho = createIsomorphicFn() .client((input) => 'client received ' + input) const getServerEcho_createServerFn_handler = createServerRpc( { - id: 's0jNQTWKXVmb2EhbCIBxAU', + id: 'ebb82e0293952b7b1dde3f664dec4c139471c046d06d4dc4ca172f4f45e386d1', name: 'getServerEcho', filename: 'src/test.ts', }, diff --git a/packages/start-plugin-core/tests/integration/start-app.ts b/packages/start-plugin-core/tests/integration/start-app.ts index 94ef27774f0..ca5603ac2c6 100644 --- a/packages/start-plugin-core/tests/integration/start-app.ts +++ b/packages/start-plugin-core/tests/integration/start-app.ts @@ -23,7 +23,7 @@ import os from 'node:os' import path from 'node:path' import { fileURLToPath, pathToFileURL } from 'node:url' import { createRsbuild } from '@rsbuild/core' -import { createBuilder, createServer } from 'vite' +import { createBuilder, createLogger, createServer } from 'vite' import { onTestFinished } from 'vitest' import type { ViteDevServer } from 'vite' @@ -129,6 +129,8 @@ export interface StartBuild { client: string /** The server build's JavaScript. */ server: string + /** The warnings a Vite build logged (not collected for Rsbuild). */ + warnings: Array } /** Builds the app as `vite build` or `rsbuild build` does. */ @@ -138,11 +140,15 @@ export async function buildStartApp( options: StartOptions = {}, ): Promise { const plugin = await startPlugin(bundler, options) + const warnings: Array = [] if (bundler === 'vite') { + const customLogger = createLogger('error') + customLogger.warn = (message) => warnings.push(message) + customLogger.warnOnce = customLogger.warn const builder = await createBuilder({ root: app.root, configFile: false, - logLevel: 'error', + customLogger, plugins: [plugin], }) await builder.buildApp() @@ -157,6 +163,7 @@ export async function buildStartApp( return { client: await emittedCode(path.join(app.root, 'dist/client')), server: await emittedCode(path.join(app.root, 'dist/server')), + warnings, } } diff --git a/packages/start-plugin-core/tests/integration/start-build-function-id-url.test.ts b/packages/start-plugin-core/tests/integration/start-build-function-id-url.test.ts new file mode 100644 index 00000000000..a5a2b229fc1 --- /dev/null +++ b/packages/start-plugin-core/tests/integration/start-build-function-id-url.test.ts @@ -0,0 +1,130 @@ +/** + * The client puts a server function's id in its URL unencoded, as one path + * segment, and the server reads that segment back. An id a custom + * `serverFns.generateFunctionId` returns with an ASCII character outside + * RFC 3986's unreserved set and `$` (`'`, `:`, `%`…) reaches its handler, + * but proxies and servers may encode or normalise it: it builds, with a + * deprecation warning naming the file. An id of unreserved characters and + * `$` builds without one. (Ids that never reached their handler fail the + * build: `tests/createServerFn/createServerFn.test.ts`.) + */ +import { execFile } from 'node:child_process' +import { readdir } from 'node:fs/promises' +import path from 'node:path' +import { pathToFileURL } from 'node:url' +import { promisify } from 'node:util' +import { describe, expect, test } from 'vitest' +import { appFiles, buildStartApp, createStartApp } from './start-app' + +const timeout = 60_000 + +const execFileAsync = promisify(execFile) + +/** Ids that reach their handler, deprecated, by file. */ +const deprecated: Record = { + 'src/fns/apostrophe.ts': "users'get", + 'src/fns/colon.ts': 'users:get', + 'src/fns/percent.ts': 'users%2Fget', + 'src/fns/brackets.ts': 'users[get]', +} + +const fine: Record = { 'src/fns/fine.ts': 'Users-get.v1_~$' } + +const ids = { ...deprecated, ...fine } + +/** A route whose component references a server function of each file, which returns the file's name. */ +function app() { + const files: Record = {} + const imports: Array = [] + Object.keys(ids).forEach((file, index) => { + files[file] = `import { createServerFn } from '@tanstack/react-start' +export const fn = createServerFn().handler(async () => new Response(${JSON.stringify(file)})) +` + imports.push( + `import { fn as fn${index} } from '${file.replace(/^src\/(.*)\.ts$/, '../$1')}'`, + ) + }) + files['src/routes/index.tsx'] = + `import { createFileRoute } from '@tanstack/react-router' +${imports.join('\n')} +export const Route = createFileRoute('/')({ + component: () => , +}) +` + return appFiles(files) +} + +/** + * Sends a request for each id (`/_serverFn/`, as a page of the same + * origin does) to the server build's entry, in a Node process of its own, + * and gives each response's text. + */ +async function callServer(root: string, ids: Array) { + const dir = path.join(root, 'dist/server') + const entry = (await readdir(dir)).find((file) => + /^(server|index)\.m?js$/.test(file), + ) + if (entry === undefined) { + throw new Error(`no server entry in ${dir}`) + } + const script = `const { default: server } = await import(${JSON.stringify(pathToFileURL(path.join(dir, entry)).href)}) +const texts = [] +for (const id of ${JSON.stringify(ids)}) { + const response = await server.fetch( + new Request(new URL('/_serverFn/' + id, 'http://localhost'), { + headers: { 'sec-fetch-site': 'same-origin' }, + }), + ) + texts.push(await response.text()) +} +process.stdout.write(JSON.stringify(texts)) +` + const { stdout } = await execFileAsync( + process.execPath, + ['--input-type=module', '-e', script], + { cwd: root, env: { ...process.env, NODE_ENV: 'production' } }, + ) + return JSON.parse(stdout) as Array +} + +const options = { + serverFns: { + generateFunctionId: ({ filename }: { filename: string }) => ids[filename], + }, +} + +describe.each(['vite', 'rsbuild'] as const)('%s build', (bundler) => { + // `'` broke the server bundle: the resolver's manifest key was quoted with it + test( + 'a custom id with characters outside the unreserved set builds and reaches its handler', + async () => { + const start = await createStartApp(app()) + await buildStartApp(start, bundler, options) + expect(await callServer(start.root, Object.values(ids))).toEqual( + Object.keys(ids), + ) + }, + timeout, + ) +}) + +// (Rsbuild's transform context has no channel for the compiler's warnings) +describe('vite build', () => { + test( + 'a custom id with characters outside the unreserved set warns, naming the file and the id', + async () => { + const start = await createStartApp(app()) + const build = await buildStartApp(start, 'vite', options) + for (const [file, id] of Object.entries(ids)) { + expect( + build.warnings.some( + (warning) => + warning.includes(file) && warning.includes(JSON.stringify(id)), + ), + `a warning naming ${file}`, + ).toBe(file in deprecated) + } + }, + timeout, + ) +})