Skip to content

FOUC in SPA on (re)load. #13307

@Leftium

Description

@Leftium

Describe the bug

There is a jarring style transition if:

  • Kit in SPA mode.
  • Root +template loads CSS file.

Reproduction

  1. (Re)load https://spa-fouc-no-pico--kit-demos.netlify.app/ in (mobile browser)
  2. Notice header starts black and flashes to red color.
  3. Repo: https://github.com/Leftium/kit-demos/tree/spa-fouc-no-pico

System Info

  • iPhone 13 mini with iOS 18.1.1
  • mobile Safari
System:
    OS: Windows 11 10.0.22631
    CPU: (8) x64 Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
    Memory: 11.28 GB / 23.86 GB
  Binaries:
    Node: 22.12.0 - V:\scoop\apps\nodejs-lts\current\node.EXE
    npm: 10.9.0 - V:\scoop\apps\nodejs-lts\current\npm.CMD
    pnpm: 9.13.2 - V:\pnpm-home\pnpm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @sveltejs/adapter-static: ^3.0.6 => 3.0.8
    @sveltejs/kit: ^2.0.0 => 2.15.2
    @sveltejs/vite-plugin-svelte: ^4.0.0 => 4.0.4
    svelte: ^5.0.0 => 5.17.3
    vite: ^5.4.11 => 5.4.11

Severity

annoyance

Additional Information

  • I started investigating this due to FOUC of white background when (system-prefers) dark mode.

    • The flash of white background can be prevented if global CSS is inlined directly into the app.html page template.
  • Related issue: Global styles or scripts #3127 (comment)

  • I simplified this issue to the absolute minimal reproduction, but it might be worth looking at the original issue I wrote involving PicoCSS:

Original issue with more details, including how to consistently reproduce on desktop.

Describe the bug

There is a jarring style transition if:

  • Kit in SPA mode.
  • Root +template loads CSS file.
  • The CSS file loads PicoCSS.
  • edit: PicoCSS is not required, but makes the FOUC more apparent.

Reproduction

Plain CSS without PicoCSS:

With PicoCSS:

Although visible on desktop, the FOUC is most visible on mobile (iOS Safari).

  • Just reload the page to see header flashing from black to red color.
  • The FOUC seems less visible when served locally (pnpm preview).

To make FOUC easier to see on desktop adjust dev tools (and ensure serving from pnpm preview):

  • Disable cache
  • Throttle network to 3G

Additional Information

  • If PicoCSS is moved from app.css to app.html the FOUC stops occurring is much more subtle and less pronounced:

  • If PicoCSS is loaded in both app.css and app.html there is a limited FOUC:

    • Only the h1 color (set in app.css) flashes black to red.
    • The PicoCSS-related styles are already set and do not flash.
  • If PicoCSS is not loaded at all, there does not seem to be a FOUC. the FOUC is only noticeable on mobile.

  • This FOUC seems more pronounced on mobile (iOS Safari), even without network throttling. Usually quite visible on each page reload.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions