Add support for customizable `functionId` generation. #5373
Mergedzenios(GitHub profile, opens in a new tab)merged 12 commits into
main fromrefs/pull/5373/headmerged by schiller-manuel(GitHub profile, opens in a new tab) github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: directive-functions-plugin
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: server-functions-plugin
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: start-plugin-core
- packages/start-plugin-core/src/schema.ts line 162Outdated@@ -159,3 +159,4 @@159159 serverFns: z160160 .object({161161 base: z.string().optional().default('/_serverFn'),162+ functionId: z
- packages/directive-functions-plugin/src/compilers.ts line 494Outdated@@ -469,3 +477,18 @@469- const functionId = makeFileLocationUrlSafe(470- `${baseFilename}--${functionName}`.replace(opts.root, ''),471- )477+ // Relative in order to have constant functionId regardless of the machine478+ // that we are executing479+ const relativeFilename = path.relative(opts.root, baseFilename)480+ let functionId = `${relativeFilename}--${functionName}`481+ if (opts.functionId) {482+ functionId = opts.functionId({ currentId: functionId })483+ // Handle cases in which the returned id conflicts with484+ // one of the already defined ids485+ if (functionId in directiveFnsById) {486+ let deduplicatedId = functionId487+ let iteration = 0488+ do {489+ deduplicatedId = `${deduplicatedId}_${++iteration}`490+ } while (deduplicatedId in directiveFnsById)491+ functionId = deduplicatedId492+ }493+ }494+ functionId = makeFunctionIdUrlSafe(functionId)
- packages/server-functions-plugin/src/index.ts line 285Outdated@@ -262,3 +272,14 @@262272 return `\0${id}`263273 }264274275+function buildFunctionId(opts: {276+ functionId: FunctionIdFn277+ directiveFnsById: Record<string, DirectiveFn>278+}): FunctionIdFn {279+ return (functionIdOps) => {280+ let generatedId = opts.functionId(functionIdOps)281+ if (generatedId in opts.directiveFnsById) {282+ let deduplicatedId = generatedId283+ let iteration = 0284+ do {285+ deduplicatedId = `${deduplicatedId}_${++iteration}`
- @@ -470,3 +477,19 @@470- `${baseFilename}--${functionName}`.replace(opts.root, ''),471- )477+ // Relative to have constant functionId regardless of the machine478+ // that we are executing479+ const relativeFilename = path.relative(opts.root, baseFilename)480+ let functionId = `${relativeFilename}--${functionName}`481+ if (opts.functionId) {482+ functionId = opts.functionId({ currentId: functionId }) ?? functionId483+ // Handle cases in which the returned id conflicts with484+ // one of the already defined ids485+ if (functionId in directiveFnsById) {486+ let deduplicatedId487+ let iteration = 0488+ do {489+ deduplicatedId = `${functionId}_${++iteration}`490+ } while (deduplicatedId in directiveFnsById)491+ functionId = deduplicatedId492+ }493+ }494+ functionId = makeFunctionIdUrlSafe(functionId)472495
github-actions[bot](GitHub profile, opens in a new tab) added labelsdocumentation
Sign in to join this conversation.
Walkthrough#
Adds a customizable generateFunctionId hook and threads it through directive and server function plugins, Start plugin schema/options, and E2E configs; normalizes filenames with path.relative, centralizes deduplicated URL-safe functionId generation, and adds the
pathedependency.Changes#
packages/directive-functions-plugin/src/compilers.ts,packages/directive-functions-plugin/src/index.tsGenerateFunctionIdFnandgenerateFunctionIdoption; computes relative filenames withpath.relative, invokes provided generator, applies URL-safety and deduplication, and threads the option through transform/compile flows.packages/directive-functions-plugin/package.json"pathe": "^2.0.3".packages/server-functions-plugin/src/index.tsgenerateFunctionId?: GenerateFunctionIdFnOptionalandenvName?: string; replaces exported env opts shape withOmit<ServerFnPluginOpts,'ssr'>; implementsbuildGenerateFunctionIdwith dev/prod seeding and collision deduplication; threads generation through client/ssr/server/manifest flows.packages/start-plugin-core/src/plugin.tsgenerateFunctionIdtoTanStackServerFnPluginEnv(sourced fromstartPluginOpts?.serverFns?.generateFunctionId) and passes it into manifest resolution.packages/start-plugin-core/src/schema.tstanstackStartOptionsSchema.serverFnswith optionalgenerateFunctionIdtaking `{ filename: string, functionName: string } => stringe2e/react-start/custom-basepath/tests/navigation.spec.ts/custom/basepath/_serverFn/.e2e/react-start/server-functions/tests/server-functions.spec.ts,e2e/react-start/server-functions/vite.config.ts'constant_id'viaserverFns.generateFunctionId; adds test asserting form action starts with/ _serverFn/constant_id.packages/directive-functions-plugin/tests/compiler.test.tsmakeFunctionIdUrlSafeandgenerateFunctionIdhelpers; wiresgenerateFunctionIdinto client/ssr/server test configs and adjusts test filenames for determinism.docs/start/framework/react/server-functions.mdgenerateFunctionId.Sequence Diagram(s)#
Estimated code review effort#
🎯 4 (Complex) | ⏱️ ~60 minutes
Suggested reviewers#
Poem#
Pre-merge checks and finishing touches#
✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 3a67fc9952edae52a984a4c54707ab39de17c911 and ad7bd9d385d65b3a565b5f888a1a5d75042fd81c.
📒 Files selected for processing (1)
docs/start/framework/react/server-functions.md(1 hunks)🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share