Skip to content

Commit

Permalink
add text content in static html for seo
Browse files Browse the repository at this point in the history
  • Loading branch information
vcoppe committed Sep 10, 2024
1 parent 10e328f commit 723a013
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion website/src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,24 @@ export async function handle({ event, resolve }) {
`;
}

let stringsHTML = stringsToHTML(strings);

const response = await resolve(event, {
transformPageChunk: ({ html }) => html.replace('<head>', head)
transformPageChunk: ({ html }) => html.replace('<head>', head).replace('<body data-sveltekit-preload-data="hover">', `<body data-sveltekit-preload-data="hover"><div class="hidden">${stringsHTML}</div>`)
});

return response;
}

function stringsToHTML(dictionary, strings = new Set(), root = true) {
Object.values(dictionary).forEach((value) => {
if (typeof value === 'object') {
stringsToHTML(value, strings, false);
} else {
strings.add(value);
}
});
if (root) {
return Array.from(strings).map((string) => `<p>${string}</p>`).join('');
}
}

0 comments on commit 723a013

Please sign in to comment.