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
+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);
});
},
});