Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion packages/fresh/src/runtime/client/partials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
148 changes: 148 additions & 0 deletions packages/fresh/tests/partials_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
},
});
Loading