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

FOUC while loading javascript style library that needs to be loaded before DOM loading #6227

Closed
bhvngt opened this issue Aug 24, 2022 · 4 comments

Comments

@bhvngt
Copy link

bhvngt commented Aug 24, 2022

Describe the bug

As per this comment from @Rich-Harris, after #6174 FOUC should not be possible when we are importing global css within root layout.

I have been using a style javascript library that needs to be loaded before any DOM content gets loaded. I could see FOUC happening. This is different than the scenario where styles are imported which, as per the comment, gets imported as style tag.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-7xrgjy?file=src%2Froutes%2F%2Blayout.svelte,package.json,package-lock.json&terminal=dev

In the following reproduction one can see a flicker of FOUC TEST happening on the right hand side of the band. It's best to see the preview in a separate tab.

Logs

No response

System Info

@sveltejs/kit: 1.0.0-next.431
vite: 3.0.9

Severity

annoyance

Additional Information

No response

@Rich-Harris
Copy link
Member

This isn't a bug in SvelteKit. It's because of how @master/css works — it's a big bag of JavaScript that injects styles into the DOM, rather than an actual CSS file that tooling can reason about. Unfortunately I don't have any better advice than 'don't use it'.

@bhvngt
Copy link
Author

bhvngt commented Aug 25, 2022

Fair enough. Leaving aside the pros/cons of @master/css, there are use cases which falls outside the purview of framework. Maybe this is one of those cases.

@1aron
Copy link

1aron commented Mar 13, 2024

I didn't find this thread until just now. 👀

@Rich-Harris Master CSS provides three rendering modes, including pre-rendering, runtime rendering, and static extraction same as UnoCSS and TailwindCSS.

image

It's recommended to use Master CSS Progressive Rendering with Svelte. It only generates the required CSS rules based on the page, rather than bundling the entire application.

// src/hooks.server.ts
import { render } from '@master/css-server'
import { config } from '../master.css'
 
/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ event, resolve }) {
    return await resolve(event, {
        transformPageChunk: ({ html }) => render(html, config).html
    })
}

See the full guide for Svelte integration: https://rc.css.master.co/docs/installation/svelte

@1aron
Copy link

1aron commented Mar 13, 2024

@bhvngt If you are using a pure SPA, you only need to set html to display: none in advance to avoid FOUC. The Master CSS Runtime will immediately inject styles into the running stylesheet after initialization and remove display: none.

<!DOCTYPE html>
<html lang="en" style="display:none">

https://rc.css.master.co/docs/installation/svelte/runtime-rendering#prevent-flash-of-unstyled-content

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants