7

I'm trying to code an app using TailwindCSS and SvelteKit, which uses ViteJS under the hood, and while coding I realized that my Header component that is inside ./src/components/common/Header.svelte was not hot reloading on changes. No matter how big or small the change to the component, Svelte would not display them until I terminated script in the console and re-ran npm run dev.

The normal behaviour would be that the whole page updated WITH changes to the components other than pages.

Note that adding and removing changes to any routes the changes are instantly visible but the components stay the same.

This issue got quite annoying after some time and I tried finding the fix in TailwindCSS and in the svelte.config.js (Not a .cjs file in Svelte-Kit) file.

After searching for a ton of answers I could not find anything that worked.

This behaviour is quite weird since in the other projects that I work on that use this same architecture of TailwindCSS and Svelte-kit the HMR works like a charm.

Here is the code for my Header.svelte file and the __layout.svelte

Header.svelte

<script>
</script>

<header>
    <!-- TEST HEADER -->
    <nav>
        <ul class="flex gap-5 bg-red-500">
            <!--These classes are tests and won't change the appearance of the Header unless I restart the script-->
            <li><a href="/">Home</a></li>
            <li><a href="/about">About</a></li>
            <li><a href="/contact">Contact</a></li>
            <!--Other random change that won't update-->
            <li>Random Change</li>
        </ul>
    </nav>
</header>

__layout.svelte

<script>
    import '../css/tailwind.css';
    import Header from '../components/common/Header.svelte';
</script>

<Header />
<slot />

also my config files:

tailwind.config.cjs

module.exports = {
    content: ['./src/**/*.svelte', './src/app.html'],
    plugins: []
};

svelte.config.js

import preprocess from 'svelte-preprocess';
import path from 'path';


// @type {import('@sveltejs/kit').Config
const config = {
    // Consult https://github.com/sveltejs/svelte-preprocess
    // for more information about preprocessors
    preprocess: preprocess(),

    kit: {
        vite: {
            resolve: {
                alias: {
                    '@components': path.resolve('./src/components'),
                    '@routes': path.resolve('./src/routes'),
                    '@utils': path.resolve('./src/utils'),
                    '@data': path.resolve('./src/data')
                }
            }
        }
    }
};

export default config;
7
  • 2
    I know this is unhelpful, but I put all this code into a SvelteKit project with Tailwind configured and HMR worked fine in Header.svelte. Perhaps you could isolate the offending code by starting with a fresh SvelteKit project and seeing if HMR works, then install Tailwind, then make the changes in svelte.config.js—something is not right but I don't think it is included in what you posted here. Commented Feb 4, 2022 at 1:32
  • Yeah seems to work. That's odd. I'll have to take a look at my dependencies. Commented Feb 4, 2022 at 2:18
  • 1
    I have the same problem. I work with Svelte (not SvelteKit), Tailwind and Vite. Right now I force a restart with "vite-plugin-restart" Commented Feb 20, 2022 at 20:11
  • 2
    Do you have any updates on that problem? I was not able to solve this. Somewhere else here on Stackoverflow I saw that movin the svelte plugin in the vite config to end helps and it does, but not fully. Commented Mar 1, 2022 at 21:58
  • @Woww Unfortunately I still haven't found the cause of this problem. I solved it by simply creating a new SvelteKit skeleton project and starting from there. Commented Mar 3, 2022 at 12:31

1 Answer 1

7

This is how I solved the problem in my case:

  1. I double checked and saw that I used a lowercase letter instead of a uppercase letter in the import path of my component. The component was strangely still working but the hot module reloading (HMR) was not working. I might have renamed a parent folder with an uppercase while developing, it might be the cause.

  2. I stopped the terminal, ran 'npm run dev' or 'yarn dev' again

  3. I set the component outside the layout file to check if HMR was working. It worked fine. Then put it back in the layout file, and it worked again.

Not really a satisfactory answer as I could not pinpoint the cause of the problem but anyone could try these steps if it happens for them. It might just solve it.

Sign up to request clarification or add additional context in comments.

1 Comment

Amazingly, I can also reproduce this with SvelteKit v1.0.0-next.350. If the case is not correct in the import path, the module will load and display but the hot reloading does not work. Good catch!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.