Skip to content

Commit

Permalink
always include <link rel="stylesheet"> even if excluded from Link h…
Browse files Browse the repository at this point in the history
…eaders - closes #8238 (#9255)
  • Loading branch information
Rich-Harris committed Mar 3, 2023
1 parent 3124296 commit fbe4fe7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-impalas-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: always include `<link rel="stylesheet">`, even for stylesheets excluded from Link headers
20 changes: 10 additions & 10 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,20 @@ export async function render_response({
for (const dep of stylesheets) {
const path = prefixed(dep);

if (resolve_opts.preload({ type: 'css', path })) {
const attributes = ['rel="stylesheet"'];

if (inline_styles.has(dep)) {
// don't load stylesheets that are already inlined
// include them in disabled state so that Vite can detect them and doesn't try to add them
attributes.push('disabled', 'media="(max-width: 0)"');
} else {
const attributes = ['rel="stylesheet"'];

if (inline_styles.has(dep)) {
// don't load stylesheets that are already inlined
// include them in disabled state so that Vite can detect them and doesn't try to add them
attributes.push('disabled', 'media="(max-width: 0)"');
} else {
if (resolve_opts.preload({ type: 'css', path })) {
const preload_atts = ['rel="preload"', 'as="style"'];
link_header_preloads.add(`<${encodeURI(path)}>; ${preload_atts.join(';')}; nopush`);
}

head += `\n\t\t<link href="${path}" ${attributes.join(' ')}>`;
}

head += `\n\t\t<link href="${path}" ${attributes.join(' ')}>`;
}

for (const dep of fonts) {
Expand Down
8 changes: 8 additions & 0 deletions packages/kit/test/apps/options/source/hooks.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('@sveltejs/kit').Handle} */
export function handle({ event, resolve }) {
return resolve(event, {
// this allows us to check that <link rel="stylesheet"> is still added
// to the DOM even if they're not included by `preload`
preload: ({ type }) => type !== 'css'
});
}

0 comments on commit fbe4fe7

Please sign in to comment.