fix: append data script tags to head during partial navigation (#3720)

Fixes #2805

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bartek Iwańczuk
2026-03-29 19:29:39 +00:00
committed by GitHub
co-authored by Claude Opus 4.6
parent 4a59ec260d
commit 4f82c91ae4
2 changed files with 175 additions and 1 deletions
+27 -1
View File
@@ -411,7 +411,33 @@ export async function applyPartials(res: Response): Promise<void> {
} else if (child.nodeName === "SCRIPT") {
const script = child as HTMLScriptElement;
if (script.src === `${INTERNAL_PREFIX}/fresh-runtime.js`) return;
// TODO: What to do with script tags?
// Append data scripts (e.g. application/ld+json for SEO structured
// data) to the document head. Skip executable script types to
// avoid unintended re-execution.
const t = script.type;
if (
t !== "" && t !== "module" && t !== "text/javascript" &&
t !== "importmap"
) {
// Deduplicate: replace existing data script with same type and
// id, or same type and content, to avoid accumulating duplicates
// across repeated partial navigations.
const selector = script.id
? `script[type="${t}"][id="${script.id}"]`
: `script[type="${t}"]`;
const existing = Array.from(
document.head.querySelectorAll<HTMLScriptElement>(selector),
).find((el) =>
script.id ? true : el.textContent === script.textContent
);
if (existing === undefined) {
document.head.appendChild(script);
} else if (existing.textContent !== script.textContent) {
existing.textContent = script.textContent;
}
}
} else if (child.nodeName === "STYLE") {
const style = child as HTMLStyleElement;
// TODO: Do we need a smarter merging strategy?
+148
View File
@@ -2907,3 +2907,151 @@ Deno.test({
}
},
});
Deno.test({
name: "partials - appends data scripts to head",
fn: async () => {
const app = testApp()
.get("/partial", (ctx) => {
return ctx.render(
<html>
<head>
{charset}
{favicon}
<title>Updated</title>
<script
type="application/ld+json"
// deno-lint-ignore react-no-danger
dangerouslySetInnerHTML={{
__html: JSON.stringify({
"@type": "Article",
name: "Updated",
}),
}}
/>
</head>
<body f-client-nav>
<Partial name="body">
<p class="updated">updated</p>
</Partial>
</body>
</html>,
);
})
.get("/", (ctx) => {
return ctx.render(
<html>
<head>
{charset}
{favicon}
<title>Init</title>
</head>
<body f-client-nav>
<Partial name="body">
<p class="init">init</p>
</Partial>
<button type="button" class="update" f-partial="/partial">
update
</button>
</body>
</html>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".init").wait();
await page.locator(".update").click();
await page.locator(".updated").wait();
const count = await page.evaluate(
() =>
document.head.querySelectorAll('script[type="application/ld+json"]')
.length,
);
expect(count).toEqual(1);
const content = await page.evaluate(
() =>
document.head.querySelector('script[type="application/ld+json"]')
?.textContent,
);
expect(JSON.parse(content!)).toEqual({
"@type": "Article",
name: "Updated",
});
});
},
});
Deno.test({
name: "partials - does not duplicate data scripts on repeat navigation",
fn: async () => {
const app = testApp()
.get("/partial", (ctx) => {
return ctx.render(
<html>
<head>
{charset}
{favicon}
<title>Updated</title>
<script
type="application/ld+json"
// deno-lint-ignore react-no-danger
dangerouslySetInnerHTML={{
__html: JSON.stringify({
"@type": "Article",
name: "Same",
}),
}}
/>
</head>
<body f-client-nav>
<Partial name="body">
<p class="updated">updated</p>
</Partial>
</body>
</html>,
);
})
.get("/", (ctx) => {
return ctx.render(
<html>
<head>
{charset}
{favicon}
<title>Init</title>
</head>
<body f-client-nav>
<Partial name="body">
<p class="init">init</p>
</Partial>
<button type="button" class="update" f-partial="/partial">
update
</button>
</body>
</html>,
);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(address, { waitUntil: "load" });
await page.locator(".init").wait();
// Click twice to trigger two partial navigations
await page.locator(".update").click();
await page.locator(".updated").wait();
await page.locator(".update").click();
await page.locator(".updated").wait();
const count = await page.evaluate(
() =>
document.head.querySelectorAll('script[type="application/ld+json"]')
.length,
);
// Should still be 1, not 2
expect(count).toEqual(1);
});
},
});