Skip to content

Commit 91fc972

Browse files
authored
fix: respect Vite base in production asset URLs (#2305)
1 parent 97fee2a commit 91fc972

3 files changed

Lines changed: 90 additions & 3 deletions

File tree

‎.changeset/tidy-bags-listen.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/start": patch
3+
---
4+
5+
Prefix production entry scripts, stylesheets, modulepreloads, and serialized manifest paths with Vite's configured base URL.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
vi.mock("solid-start:client-vite-manifest", () => ({
4+
clientViteManifest: {
5+
"src/entry-client.tsx": {
6+
file: "_build/assets/entry-client.js",
7+
css: ["_build/assets/entry-client.css"],
8+
imports: ["_shared.js"],
9+
isEntry: true,
10+
},
11+
"_shared.js": {
12+
file: "_build/assets/shared.js",
13+
},
14+
},
15+
}));
16+
17+
vi.stubEnv("START_CLIENT_ENTRY", "./src/entry-client.tsx");
18+
19+
const { getSsrProdManifest } = await import("./prod-ssr-manifest.ts");
20+
21+
describe("getSsrProdManifest", () => {
22+
beforeEach(() => {
23+
vi.stubEnv("BASE_URL", "/foo/");
24+
});
25+
26+
it("prefixes entry paths with the Vite base", () => {
27+
expect(getSsrProdManifest().path("./src/entry-client.tsx")).toBe(
28+
"/foo/_build/assets/entry-client.js",
29+
);
30+
});
31+
32+
it("prefixes stylesheet and modulepreload URLs with the Vite base", async () => {
33+
const assets = await getSsrProdManifest().getAssets("./src/entry-client.tsx");
34+
35+
expect(assets).toEqual([
36+
{
37+
tag: "link",
38+
attrs: {
39+
href: "/foo/_build/assets/entry-client.css",
40+
key: "_build/assets/entry-client.css",
41+
rel: "stylesheet",
42+
},
43+
},
44+
{
45+
tag: "link",
46+
attrs: {
47+
href: "/foo/_build/assets/shared.js",
48+
key: "_build/assets/shared.js",
49+
rel: "modulepreload",
50+
},
51+
},
52+
{
53+
tag: "link",
54+
attrs: {
55+
href: "/foo/_build/assets/entry-client.js",
56+
key: "_build/assets/entry-client.js",
57+
rel: "modulepreload",
58+
},
59+
},
60+
]);
61+
});
62+
63+
it("prefixes serialized manifest paths with the Vite base", async () => {
64+
await expect(getSsrProdManifest().json()).resolves.toMatchObject({
65+
"src/entry-client.tsx": {
66+
output: "/foo/_build/assets/entry-client.js",
67+
},
68+
});
69+
});
70+
71+
it("preserves root-based URLs when the Vite base is root", async () => {
72+
vi.stubEnv("BASE_URL", "/");
73+
const manifest = getSsrProdManifest();
74+
75+
expect(manifest.path("./src/entry-client.tsx")).toBe("/_build/assets/entry-client.js");
76+
await expect(manifest.getAssets("./src/entry-client.tsx")).resolves.toMatchObject([
77+
{ attrs: { href: "/_build/assets/entry-client.css" } },
78+
{ attrs: { href: "/_build/assets/shared.js" } },
79+
{ attrs: { href: "/_build/assets/entry-client.js" } },
80+
]);
81+
});
82+
});

‎packages/start/src/server/manifest/prod-ssr-manifest.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function getSsrProdManifest() {
1313
const viteManifestEntry = clientViteManifest[id /*import.meta.env.START_CLIENT_ENTRY*/];
1414
if (!viteManifestEntry) throw new Error(`No entry found in vite manifest for '${id}'`);
1515

16-
return join("/", viteManifestEntry.file);
16+
return join(import.meta.env.BASE_URL, viteManifestEntry.file);
1717
},
1818
async getAssets(id) {
1919
if (id.startsWith("./")) id = id.slice(2);
@@ -29,7 +29,7 @@ export function getSsrProdManifest() {
2929

3030
for (const entryKey of entryKeys) {
3131
json[entryKey] = {
32-
output: join("/", viteManifest[entryKey]!.file),
32+
output: join(import.meta.env.BASE_URL, viteManifest[entryKey]!.file),
3333
assets: await this.getAssets(entryKey),
3434
};
3535
}
@@ -54,7 +54,7 @@ function createHtmlTagsForAssets(assets: string[]) {
5454
.map<Asset>(asset => ({
5555
tag: "link",
5656
attrs: {
57-
href: "/" + asset,
57+
href: join(import.meta.env.BASE_URL, asset),
5858
key: asset,
5959
...(asset.endsWith(".css") ? { rel: "stylesheet" } : { rel: "modulepreload" }),
6060
},

0 commit comments

Comments
 (0)