Skip to content

Commit 28608d3

Browse files
authored
fix(html): overriding default head (#1088)
Let React handle html headers instead of injecting them as strings
1 parent e6b0534 commit 28608d3

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

packages/waku/src/lib/plugins/vite-plugin-rsc-index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type { Plugin } from 'vite';
22

33
import { SRC_MAIN } from '../constants.js';
44

5-
export const DEFAULT_HTML_HEAD = `
5+
// This should be consistent with the one in renderers/html.ts
6+
const DEFAULT_HTML_HEAD = `
67
<meta charset="utf-8" />
78
<meta name="viewport" content="width=device-width, initial-scale=1" />
89
<meta name="generator" content="Waku" />

packages/waku/src/lib/renderers/html.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ import { renderRsc, renderRscElement, getExtractFormState } from './rsc.js';
1414
// TODO move types somewhere
1515
import type { HandlerContext } from '../middleware/types.js';
1616

17-
// HACK depending on these constants is not ideal
18-
import { DEFAULT_HTML_HEAD } from '../plugins/vite-plugin-rsc-index.js';
17+
// This should be consistent with the one in vite-plugin-rsc-index.ts
18+
const DEFAULT_HTML_HEAD = [
19+
createElement('meta', { charSet: 'utf-8' }),
20+
createElement('meta', {
21+
name: 'viewport',
22+
content: 'width=device-width, initial-scale=1',
23+
}),
24+
createElement('meta', { name: 'generator', content: 'Waku' }),
25+
];
1926

2027
type Elements = Record<string, ReactNode>;
2128

@@ -56,11 +63,7 @@ const injectHtmlHead = (
5663
data.slice(0, closingHeadIndex + CLOSING_HEAD.length),
5764
data.slice(closingHeadIndex + CLOSING_HEAD.length),
5865
];
59-
head =
60-
head.slice(0, -CLOSING_HEAD.length) +
61-
DEFAULT_HTML_HEAD +
62-
htmlHead +
63-
CLOSING_HEAD;
66+
head = head.slice(0, -CLOSING_HEAD.length) + htmlHead + CLOSING_HEAD;
6467
const matchPrefetched = head.match(
6568
// HACK This is very brittle
6669
/(.*<script[^>]*>\nglobalThis\.__WAKU_PREFETCHED__ = {\n)(.*?)(\n};.*)/s,
@@ -226,6 +229,7 @@ export async function renderHtml(
226229
Omit<ComponentProps<typeof ServerRoot>, 'children'>
227230
>,
228231
{ elements: elementsPromise },
232+
...DEFAULT_HTML_HEAD,
229233
htmlNode as any,
230234
),
231235
{

0 commit comments

Comments
 (0)