Skip to content

Commit 4b7258b

Browse files
fix: warn on an unresolvable publicPath and wait for a pending server stylesheet
`SSRManifestPlugin` cannot resolve `output.publicPath: "auto"` — the browser computes it from the script url — so it falls back to `/`. Served from anywhere else, the urls the server prints are not the ones the runtime builds: nothing is adopted and every stylesheet is fetched again. Warn instead of failing silently. The css loading runtime treated any adopted `<link>` as already applied, which holds for one the parser saw in `<head>` but not for one a streamed render inserts later. Wait on a same-origin link that has no `sheet` yet; a cross-origin sheet never exposes one, so it keeps the render-blocking assumption. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01K8n6KpLAPpLZdLrovcDnAu
1 parent e2643a0 commit 4b7258b

13 files changed

Lines changed: 155 additions & 4 deletions

File tree

‎.changeset/019-ssr-manifest-plugin.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": minor
33
---
44

5-
Add `SSRManifestPlugin` and collect server styles without a DOM.
5+
Add `SSRManifestPlugin` and fix server-rendered CSS collection and adoption.

‎lib/SSRManifestPlugin.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Compilation = require("./Compilation");
1010
const HotUpdateChunk = require("./HotUpdateChunk");
1111
const NormalModule = require("./NormalModule");
1212
const CssModulesPlugin = require("./css/CssModulesPlugin");
13+
const SSRManifestAutoPublicPathWarning = require("./errors/SSRManifestAutoPublicPathWarning");
1314
const ConcatenatedModule = require("./optimize/ConcatenatedModule");
1415
const { first } = require("./util/SetHelpers");
1516
const { makePathsRelative } = require("./util/identifier");
@@ -195,7 +196,11 @@ class SSRManifestPlugin {
195196
);
196197
// "auto" is resolved from the script url, which a manifest cannot
197198
// do; root-absolute paths inject from any route instead.
198-
const base = publicPath === "auto" ? "/" : publicPath;
199+
let base = publicPath;
200+
if (publicPath === "auto") {
201+
base = "/";
202+
compilation.warnings.push(new SSRManifestAutoPublicPathWarning());
203+
}
199204
const dependencies = getChunkDependencies(
200205
compilation.modules,
201206
chunkGraph,

‎lib/css/CssLoadingRuntimeModule.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,15 @@ class CssLoadingRuntimeModule extends RuntimeModule {
297297
"if(prev) return prev(event);"
298298
])
299299
)};`,
300-
"if(link.getAttribute(loadingAttribute)) {",
300+
// An adopted stylesheet without a `sheet` has not applied yet, so a streamed
301+
// server render no longer resolves before its rules are in.
302+
'if(link.getAttribute(loadingAttribute) || (!link.sheet && link.href.indexOf(location.origin + "/") == 0)) {',
301303
Template.indent([
302304
`timeout = setTimeout(onLinkComplete.bind(null, undefined, { type: 'timeout', target: link }), ${loadTimeout});`,
303305
"link.onerror = onLinkComplete.bind(null, link.onerror);",
304306
"link.onload = onLinkComplete.bind(null, link.onload);"
305307
]),
306-
"} else onLinkComplete(undefined, { type: 'load', target: link });", // We assume any existing stylesheet is render blocking
308+
"} else onLinkComplete(undefined, { type: 'load', target: link });", // a cross-origin sheet never exposes `sheet`, so it keeps the render-blocking assumption
307309
withHmr && withFetchPriority
308310
? 'if (hmr && hmr.getAttribute("fetchpriority")) link.setAttribute("fetchpriority", hmr.getAttribute("fetchpriority"));'
309311
: "",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Alexander Akait @alexander-akait
4+
*/
5+
6+
"use strict";
7+
8+
const WebpackError = require("./WebpackError");
9+
10+
class SSRManifestAutoPublicPathWarning extends WebpackError {
11+
/**
12+
* Creates an instance of SSRManifestAutoPublicPathWarning.
13+
*/
14+
constructor() {
15+
super(
16+
"SSRManifestPlugin cannot resolve 'output.publicPath: \"auto\"'. That value is computed in the browser from the script url, which a manifest written at build time has no way to know, so the manifest falls back to '/'. Served from anywhere else, the urls the server prints are not the ones the runtime builds: nothing is adopted, every stylesheet is fetched a second time and its rules applied twice. Set 'output.publicPath' explicitly in the client build."
17+
);
18+
19+
/** @type {string} */
20+
this.name = "SSRManifestAutoPublicPathWarning";
21+
}
22+
}
23+
24+
module.exports = SSRManifestAutoPublicPathWarning;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.button {
2+
background: rebeccapurple;
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./button.css";
2+
3+
export function renderButton(label) {
4+
return `<button class="button">${label}</button>`;
5+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// This config need to be set on initial evaluation to be effective
2+
__webpack_public_path__ = "https://test.cases/";
3+
4+
it("waits for a server-rendered stylesheet that has not applied yet", async () => {
5+
// A streamed render inserts its <link> after the shell, so it is not render
6+
// blocking: no `sheet` yet means the rules are still on their way.
7+
const printed = document.createElement("link");
8+
printed.rel = "stylesheet";
9+
printed.href = "https://test.cases/button.css";
10+
printed.sheet = undefined;
11+
document.head.appendChild(printed);
12+
13+
let loaded = false;
14+
const promise = import(/* webpackChunkName: "button" */ "./button.js").then(
15+
(module) => {
16+
loaded = true;
17+
return module;
18+
}
19+
);
20+
21+
// drive the <script> load for chunk "button" so only the stylesheet is pending
22+
const script = document.head._children[document.head._children.length - 1];
23+
expect(script._type).toBe("script");
24+
__non_webpack_require__("./button.js");
25+
script.onload();
26+
27+
// the runtime adopted the printed link rather than creating its own
28+
expect(typeof printed.onload).toBe("function");
29+
30+
await new Promise((resolve) => setTimeout(resolve, 0));
31+
expect(loaded).toBe(false);
32+
33+
printed.sheet = {};
34+
printed.onload({ type: "load", target: printed });
35+
36+
const { renderButton } = await promise;
37+
38+
expect(renderButton("Buy")).toBe('<button class="button">Buy</button>');
39+
40+
const stylesheets = document.head._children.filter(
41+
(element) => element._type === "link" && element.rel === "stylesheet"
42+
);
43+
44+
expect(stylesheets).toHaveLength(1);
45+
expect(stylesheets[0]).toBe(printed);
46+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: "web",
6+
mode: "development",
7+
devtool: false,
8+
experiments: {
9+
css: true
10+
},
11+
output: {
12+
chunkFilename: "[name].js",
13+
cssChunkFilename: "[name].css"
14+
},
15+
performance: {
16+
hints: false
17+
},
18+
optimization: {
19+
minimize: false
20+
}
21+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
require("./page.js");
7+
8+
const manifest = JSON.parse(
9+
fs.readFileSync(path.join(__STATS__.outputPath, "ssr-manifest.json"), "utf-8")
10+
);
11+
12+
it("falls back to a root-absolute base and warns about it", () => {
13+
const files = manifest["./index.js"];
14+
expect(files).toBeDefined();
15+
for (const file of files) expect(file.startsWith("/")).toBe(true);
16+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports = "page";

0 commit comments

Comments
 (0)