Skip to content

HMR not working with imported CSS (Module) files #10905

@aradalvand

Description

@aradalvand

Describe the bug

Editing CSS Module files that are imported in .svelte components causes a full page reload, while hot-reloading is expected (and demonstrably possible, but more on that in the "Additional information" section).

Before somebody asks "why aren't you using the <style> tag directly in your Svelte components and taking advantage of Svelte's built-in CSS scoping?", because they are not good enough, and I'm not alone in thinking that.

Reproduction

  1. Clone https://github.com/aradalvand/sveltekit-hmr-css-test
  2. Run pnpm install
  3. Run pnpm run dev
  4. Go to the src/lib/Button.module.css file, and change the background-color to blue.
  5. Save the file.
  6. Notice how a full page reload is triggered in the browser

Logs

No response

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (4) x64 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
    Memory: 7.21 GB / 11.63 GB
    Container: Yes
    Shell: 3.6.1 - /home/linuxbrew/.linuxbrew/bin/fish
  Binaries:
    Node: 20.6.0 - /home/linuxbrew/.linuxbrew/bin/node
    npm: 9.8.1 - /home/linuxbrew/.linuxbrew/bin/npm
    pnpm: 8.7.1 - /home/linuxbrew/.linuxbrew/bin/pnpm
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.1.0
    @sveltejs/kit: ^1.20.4 => 1.26.0
    svelte: ^4.0.5 => 4.2.2
    vite: ^4.4.2 => 4.5.0

Severity

annoyance

Additional Information

  1. Put the following code in the <script> section of your root +layout.svelte file (or, in the repro app linked above, it could be in the +page.svelte file):
if (import.meta.hot) {
    import.meta.hot.on('vite:beforeFullReload', () => {
        throw '(skipping full reload)';
    });
}
  1. Make a change to the Button.module.css file again (change the background-color back to red), and save the file.
  2. At this point, if you look at the browser console, you'll see Uncaught (in promise) (skipping full reload), as expected; the full page reload is prevented.
  3. Now, make a meaningful change (not just whitespace, change the button's label for example) to the Button.svelte file, and then save the file.
  4. Back to the browser, you can see that the new CSS change has been hot-reloaded as well. I think this showcases that HMR for these CSS files is perfectly possible, so this has to be a matter of misconfiguration or something similar on the part of SvelteKit.

Let me know. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingvite

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions