Skip to content

Commit 268aa13

Browse files
fix: resolve SSR hydration mismatch between server.jsx and index.jsx
1 parent fe5b0a2 commit 268aa13

1 file changed

Lines changed: 15 additions & 20 deletions

File tree

src/server.jsx

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,28 @@ function isPrintPage(url) {
1515
// Export method for `SSGPlugin`
1616
export default (locals) => {
1717
const renderedHtml = ReactDOMServer.renderToString(
18-
<HelmetProvider>
19-
<StaticRouter location={locals.path}>
20-
<body>
21-
<div id="root">
22-
<Site
23-
// note that here we use require instead of import
24-
// i.e., can't reuse App.jsx
25-
// eslint-disable-next-line no-undef
26-
import={(path) => require(`./content/${path}`)}
27-
/>
28-
</div>
29-
{isPrintPage(locals.path) ? (
30-
<PrintScript />
31-
) : (
32-
assets.js.map((path) => <script key={path} src={path} />)
33-
)}
34-
</body>
35-
</StaticRouter>
36-
</HelmetProvider>,
18+
<StaticRouter location={locals.path}>
19+
<HelmetProvider>
20+
<Site
21+
// note that here we use require instead of import
22+
// i.e., can't reuse App.jsx
23+
// eslint-disable-next-line no-undef
24+
import={(path) => require(`./content/${path}`)}
25+
/>
26+
</HelmetProvider>
27+
</StaticRouter>,
3728
);
3829

3930
const css = assets.css
4031
.map((path) => `<link rel="stylesheet" href=${`${path}`} />`)
4132
.join("");
4233

34+
const scripts = isPrintPage(locals.path)
35+
? ReactDOMServer.renderToStaticMarkup(<PrintScript />)
36+
: assets.js.map((path) => `<script src="${path}"></script>`).join("");
37+
4338
// React 19 automatically hoists <title>, <meta>, <link> from the component
4439
// tree into <head> during renderToString, so manual helmet context
4540
// extraction is no longer needed.
46-
return `<!DOCTYPE html><html><head>${css}</head>${renderedHtml}`;
41+
return `<!DOCTYPE html><html><head>${css}</head><body><div id="root">${renderedHtml}</div>${scripts}</body></html>`;
4742
};

0 commit comments

Comments
 (0)