Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/server-function-ids-relative-path.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 5 additions & 2 deletions packages/start/src/directives/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
15 changes: 13 additions & 2 deletions packages/start/src/directives/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {} };
Expand Down Expand Up @@ -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);
});
});
15 changes: 12 additions & 3 deletions packages/start/src/directives/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<CompileOptions["mode"], Debouncer<string> | undefined> = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {};";
}
Expand All @@ -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,
});
Expand Down
Loading