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

Dynamic CSS import fails because it tries to load JavaScript instead #4718

Closed
7 tasks done
vwkd opened this issue Aug 24, 2021 · 3 comments
Closed
7 tasks done

Dynamic CSS import fails because it tries to load JavaScript instead #4718

vwkd opened this issue Aug 24, 2021 · 3 comments

Comments

@vwkd
Copy link
Contributor

vwkd commented Aug 24, 2021

Describe the bug

(This bug was initially filed at sveltejs/kit#2275)

When building a SvelteKit with a page that dynamically imports CSS, the import will fail. Dynamically importing JavaScript works fine.

This issue occurs only when building for production (npm run build), and not when running the development server (npm run dev).

It seems like instead of a .css file it's trying to load a .js file that doesn't exist. The server then correctly responds with a 404 which makes the import throw because it expected JS and not the HTML of the error page.

For example, a page that has dynamic imports like this (within some async function)

await import("mod.css");

will generate a build with a [page].svelte-[hash].js with an import of a .js file that doesn't exist

async function h() {
    await a((() => import("../chunks/mod-b7268ec5.js")), [])
}

instead of the .css file that does exist ../assets/mod-ebd5bba8.css.

A workaround is to not import CSS dynamically but at the top-level using

import "some.css"

The drawback is that this is always run when opening the page because it's top-level, while the dynamic imports can sit in some function that runs only when desired (e.g. after a click on a button).

Reproduction

See https://github.com/vwkd/sveltekit-bugs/tree/dynamic-import

  1. Run npm run build && npm run preview
  2. Click the button
  3. Observe no text color change and a console error (import failed)

You see the second erroneous import failing in the network tab

net

Note, this issue occurs only when building for production, not when running the dev server.

  1. Run npm run dev
  2. Click button
  3. Observe text color green (import successful)

System Info

System:
    OS: macOS 11.5
  Binaries:
    Node: 16.6.0 - ~/.nvm/versions/node/v16.6.0/bin/node
    npm: 7.19.1 - ~/.nvm/versions/node/v16.6.0/bin/npm
  Browsers:
    Chrome: 92.0.4515.131
    Firefox: 90.0.2
    Safari: 14.1.2
  npmPackages:
    @sveltejs/adapter-static: ^1.0.0-next.11 => 1.0.0-next.16 
    @sveltejs/kit: next => 1.0.0-next.144 
    svelte: ^3.34.0 => 3.42.1

Used Package Manager

npm

Logs

No response

Validations

@vwkd vwkd changed the title Dynamic import of JS fails if other dynamic import of CSS is present Dynamic CSS import fails because it tries to load a JS file Aug 25, 2021
@vwkd
Copy link
Contributor Author

vwkd commented Aug 25, 2021

(Note, I edited the issue to simplify it because it doesn't actually have to do with other JavaScript imports.)

Meanwhile I tried to replicate it in a vanilla Vite project but I failed. Instead I encountered this seemingly different bug #4731 and I'm not sure if they are connected.

@hex-ci
Copy link
Contributor

hex-ci commented Aug 26, 2021

This problem has been fixed in #3333 (Vite 2.5.1), but a #4731 issue you submitted was introduced, and then I tried to fix it #4740

@vwkd vwkd changed the title Dynamic CSS import fails because it tries to load a JS file Dynamic CSS import fails because it tries to load JavaScript instead Aug 26, 2021
@vwkd
Copy link
Contributor Author

vwkd commented Aug 26, 2021

Thanks. Duplicate of #3307 then, so closing.

@vwkd vwkd closed this as completed Aug 26, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Sep 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants