diff --git a/.changeset/server-function-ids-relative-path.md b/.changeset/server-function-ids-relative-path.md new file mode 100644 index 000000000..c1c761e7f --- /dev/null +++ b/.changeset/server-function-ids-relative-path.md @@ -0,0 +1,5 @@ +--- +"@solidjs/start": patch +--- + +Give server functions the same ids when the same code is built in different directories. The ids hashed the module's absolute path, so machines building one deploy in their own directories got different ids and client chunks, even with the same `skewProtection.buildId`: a page from one machine called another with ids it did not know, and its chunks were missing there. The ids now hash the path relative to the Vite root. diff --git a/packages/start/src/directives/compile.ts b/packages/start/src/directives/compile.ts index 9b6b368d7..f978cc15c 100644 --- a/packages/start/src/directives/compile.ts +++ b/packages/start/src/directives/compile.ts @@ -13,7 +13,10 @@ export interface CompileResult { export type CompileOptions = Omit< StateContext, "count" | "names" | "hash" | "imports" | "valid" | "warnings" ->; +> & { + /** Prefix of the function ids. Defaults to a hash of `id`. */ + hash?: string; +}; export async function compile( id: string, @@ -24,7 +27,7 @@ export async function compile( ...options, valid: false, warnings: [], - hash: xxHash32(id).toString(16), + hash: options.hash ?? xxHash32(id).toString(16), count: 0, names: new Map(), imports: new Map(), diff --git a/packages/start/src/directives/index.spec.ts b/packages/start/src/directives/index.spec.ts index a84158b78..627bf63b7 100644 --- a/packages/start/src/directives/index.spec.ts +++ b/packages/start/src/directives/index.spec.ts @@ -18,13 +18,13 @@ export async function getMessage() { } `; -function clientTransform(filter?: ServerFunctionsFilter) { +function clientTransform(filter?: ServerFunctionsFilter, projectRoot = root) { const plugins = serverFunctionsPlugin({ manifest: "virtual:server-fn-manifest", runtime: { server: "virtual:server-runtime", client: "virtual:client-runtime" }, filter, }) as any[]; - for (const plugin of plugins) plugin.configResolved?.({ root, mode: "production" }); + for (const plugin of plugins) plugin.configResolved?.({ root: projectRoot, mode: "production" }); const compiler = plugins.find(p => p.name === "solid-start:server-functions/compiler"); const context = { environment: { config: { consumer: "client" } }, warn() {} }; @@ -61,4 +61,15 @@ describe("serverFunctionsPlugin", () => { expect(await transform(`${root}/app/message.ts`)).not.toBeNull(); expect(await transform(`${root}/src/message.ts`)).toBeNull(); }); + + it("gives the same function ids to the same code built in another directory", async () => { + const otherRoot = normalizePath(join(tmpdir(), "solid-start-other", "app")); + + const first = await clientTransform(undefined, root)(`${root}/src/message.ts`); + const second = await clientTransform(undefined, otherRoot)(`${otherRoot}/src/message.ts`); + const other = await clientTransform(undefined, root)(`${root}/src/other.ts`); + + expect(second.code).toBe(first.code); + expect(other.code).not.toBe(first.code); + }); }); diff --git a/packages/start/src/directives/index.ts b/packages/start/src/directives/index.ts index 2926b938a..636fae569 100644 --- a/packages/start/src/directives/index.ts +++ b/packages/start/src/directives/index.ts @@ -5,6 +5,7 @@ import { type Plugin, type ViteDevServer, } from "vite"; +import { relative } from "node:path"; import fg from "fast-glob"; import { compile, type CompileOptions } from "./compile.ts"; import xxHash32 from "./xxhash32.ts"; @@ -129,6 +130,12 @@ export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[] let env: CompileOptions["env"]; + // Function ids hash the module path relative to the root, so two builds of + // the same code in different directories give the same ids and chunks. + let root: string; + const fileHash = (file: string) => + xxHash32(relative(root, file).split("\\").join("/")).toString(16); + const manifest = createManifest(); const preload: Record | undefined> = { @@ -173,6 +180,7 @@ export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[] name: "solid-start:server-functions/setup", enforce: "pre", configResolved(config) { + root = config.root; env = config.mode !== "production" ? "development" : "production"; // Vite resolves its root like every module id: to the real path since // Vite 8.2, unless `resolve.preserveSymlinks`. Patterns resolved against @@ -215,18 +223,18 @@ export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[] throw new Error(`${LOOKUP_ID} is only available in dev`); } const functionId = new URLSearchParams(id.slice(LOOKUP_ID.length + 1)).get("id"); - // dev function ids are `${xxHash32(file)}-${count}-${name}` + // dev function ids are `${fileHash(file)}-${name}` const hash = functionId?.split("-")[0]; if (!hash) return "export {};"; // Look through the files already discovered by the transform first; // fall back to scanning the project so a function is found even when // the browser kept a chunk from before a dev server restart. - let files = [...manifest.server].filter(file => xxHash32(file).toString(16) === hash); + let files = [...manifest.server].filter(file => fileHash(file) === hash); if (files.length === 0) { files = fg .sync(DEFAULT_INCLUDE, { cwd: this.environment.config.root, absolute: true }) - .filter(file => filter(file) && xxHash32(file).toString(16) === hash); + .filter(file => filter(file) && fileHash(file) === hash); } return files.map(file => `import ${JSON.stringify(file)};`).join("\n") || "export {};"; } @@ -250,6 +258,7 @@ export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[] const result = await compile(id!, code, { ...(mode === "server" ? serverOptions : clientOptions), + hash: fileHash(id!), mode, env, });