Skip to content

Commit

Permalink
feat: add option for css preloads when prerendering
Browse files Browse the repository at this point in the history
`KitConfig.prerender.generateCssPreloadTags` will configure whether
preload tags for stylesheets are generated during prerendering.
  • Loading branch information
kevinji committed Sep 14, 2024
1 parent 247aeb7 commit 16ba0ff
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-planets-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": minor
---

feat: add option for css preloads when prerendering
1 change: 1 addition & 0 deletions packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ const options = object(
return input;
}),

generateCssPreloadTags: boolean(false),
handleHttpError: validate(
(/** @type {any} */ { message }) => {
throw new Error(
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/sync/write_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const options = {
embedded: ${config.kit.embedded},
env_public_prefix: '${config.kit.env.publicPrefix}',
env_private_prefix: '${config.kit.env.privatePrefix}',
generate_css_preload_tags: ${config.kit.prerender.generateCssPreloadTags};
hooks: null, // added lazily, via \`get_hooks\`
preload_strategy: ${s(config.kit.output.preloadStrategy)},
root,
Expand Down
7 changes: 7 additions & 0 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,13 @@ export interface KitConfig {
* @default ["*"]
*/
entries?: Array<'*' | `/${string}`>;
/**
* Whether SvelteKit should generate `<link rel="preload">` tags for stylesheets.
* This is useful for Cloudflare Pages's [Early Hints](https://developers.cloudflare.com/pages/configuration/early-hints/) feature, which looks for these tags to automatically generate Link headers.
*
* @default false
*/
generateCssPreloadTags?: boolean;
/**
* How to respond to HTTP errors encountered while prerendering the app.
*
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ export async function render_response({
if (resolve_opts.preload({ type: 'css', path })) {
const preload_atts = ['rel="preload"', 'as="style"'];
link_header_preloads.add(`<${encodeURI(path)}>; ${preload_atts.join(';')}; nopush`);
if (options.generate_css_preload_tags && state.prerendering) {
head += `\n\t\t<link href="${path}" ${preload_atts.join(' ')}>`;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export interface SSROptions {
embedded: boolean;
env_public_prefix: string;
env_private_prefix: string;
generate_css_preload_tags: boolean;
hooks: ServerHooks;
preload_strategy: ValidatedConfig['kit']['output']['preloadStrategy'];
root: SSRComponent['default'];
Expand Down

0 comments on commit 16ba0ff

Please sign in to comment.