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

Support the direct query param #341

Closed
matthewp opened this issue May 16, 2022 · 4 comments · Fixed by #513
Closed

Support the direct query param #341

matthewp opened this issue May 16, 2022 · 4 comments · Fixed by #513
Labels
enhancement New feature or request

Comments

@matthewp
Copy link

Describe the problem

In Astro we prefer to add <link> tags per component for styles in development mode. This works because Vite will detect a request that has been added via link tag through the Accept header and then injects the direct query param.

For Astro and Vue styles this works because those Vite plugins do not modify the id. Since Svelte creates an id the direct param gets lost.

This means that we cannot use <link> in dev for Svelte, but rather must use a module script which results in a FOUC (flash of unstyled content).

Describe the proposed solution

When generating the cssId, preserve direct query param, if it's included in the importee.

const cssId = createVirtualImportId(filename, root, 'style');

Alternatives considered

No alternatives that I am aware of, other than Astro continuing to use <script> and live with the FOUC.


Note that I am willing to submit a PR for this.

Importance

would make my life easier

@matthewp matthewp added enhancement New feature or request triage Awaiting triage by a project member labels May 16, 2022
@dominikg
Copy link
Member

dominikg commented May 16, 2022

👍
It gets a bit more complicated than just preserving the direct param. Right now vite-plugin-svelte compiles the .svelte files and stores the css in a cache + returns a js module that contains an import to the css id.
The cached css is then returned by the subsequent request to that module.

So requesting the css via direct link before the .svelte file was imported won't work right now.
3 possible solutions to that

  1. if the cache is empty, compile in load, only take the css output do not cache anything, the import of .svelte will populate the cache afterwards
  2. refactor the compile+cache logic so that the first request primes the cache, regardless of js or css imported first
  3. a long term plan is to reorganize how these sub modules are connected separate es modules for <script context="module"> and <script> during dev / hmr #136 so if we add the css module there to that could also work

Right now i prefer solution 1) as it is the least amount of work.

@dominikg dominikg removed the triage Awaiting triage by a project member label May 16, 2022
@matthewp
Copy link
Author

Thanks, I think this makes sense to me. For what it's worth, Astro does not add <link> before the Svelte module has already been loaded, so I don't think we'd run into the scenario you are describing, if I'm understanding it correctly.

@dominikg
Copy link
Member

If you always import Foo.svelte first before the browser sends a request to Foo.svelte?svelte&type=style&lang.css it'll work without additional compiling. Others may not be so cautious so it would still make sense to support it.

Can be done in separate PRs to keep the diffs small.

@dominikg
Copy link
Member

dominikg commented Oct 2, 2022

@matthewp is this still something you need/miss?

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

Successfully merging a pull request may close this issue.

2 participants