Skip to content
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ export async function prerender(data) {
// Sets any additional elements you want injected into the `<head>`:
// <link rel="stylesheet" href="foo.css">
// <meta property="og:title" content="Social media title">
// <meta name="description" content="My cool description">
elements: new Set([
{ type: 'link', props: { rel: 'stylesheet', href: 'foo.css' } },
{ type: 'meta', props: { property: 'og:title', content: 'Social media title' } },
// Use `replace` with a CSS selector to remove existing elements before inserting:
{ type: 'meta', props: { name: 'description', content: 'My cool description' }, replace: 'meta[name="description"]' },
]),
},
};
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/prerender-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,15 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
}

if (head.elements) {
// Remove existing elements that should be replaced
for (const element of head.elements) {
if (element.replace) {
for (const existing of htmlHead.querySelectorAll(element.replace)) {
existing.remove();
}
}
}

// Inject HTML links at the end of <head> for any stylesheets injected during rendering of the page:
htmlHead.insertAdjacentHTML(
'beforeend',
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export interface HeadElement {
type: string;
props: Record<string, string>;
children?: string;
/** CSS selector to find and remove existing elements before inserting this one */
replace?: string;
}

export interface Head {
Expand Down