Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update inline css url in dev #13007

Merged
merged 7 commits into from
Nov 25, 2024
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
5 changes: 5 additions & 0 deletions .changeset/ninety-dragons-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: update inline css url generation for FOUC prevention in dev
20 changes: 8 additions & 12 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { check_feature } from '../../../utils/features.js';
import { escape_html } from '../../../utils/escape.js';

const cwd = process.cwd();
// vite-specifc queries that we should skip handling for css urls
const vite_css_query_regex = /(?:\?|&)(?:raw|url|inline)(?:&|$)/;
bluwy marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param {import('vite').ViteDevServer} vite
Expand Down Expand Up @@ -188,6 +190,7 @@ export async function dev(vite, vite_config, svelte_config) {
// in dev we inline all styles to avoid FOUC. this gets populated lazily so that
// components/stylesheets loaded via import() during `load` are included
result.inline_styles = async () => {
/** @type {Set<import('vite').ModuleNode>} */
const deps = new Set();

for (const module_node of module_nodes) {
Expand All @@ -198,19 +201,12 @@ export async function dev(vite, vite_config, svelte_config) {
const styles = {};

for (const dep of deps) {
const url = new URL(dep.url, 'dummy:/');
const query = url.searchParams;

if (
(isCSSRequest(dep.file) ||
(query.has('svelte') && query.get('type') === 'style')) &&
Copy link
Member Author

@bluwy bluwy Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this specific handling (line 205) in the new code since isCSSRequest should already be capturing any instances of this. I tested in the playground locally and the inlined-styles to prevent fouc still worked.

!(query.has('raw') || query.has('url') || query.has('inline'))
) {
if (isCSSRequest(dep.url) && !vite_css_query_regex.test(dep.url)) {
const inlineCssUrl = dep.url.includes('?')
? dep.url.replace('?', '?inline&')
: dep.url + '?inline';
try {
query.set('inline', '');
const mod = await vite.ssrLoadModule(
`${decodeURI(url.pathname)}${url.search}${url.hash}`
);
const mod = await vite.ssrLoadModule(inlineCssUrl);
styles[dep.url] = mod.default;
} catch {
// this can happen with dynamically imported modules, I think
Expand Down
Loading