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

prevent loading global style entry css file #14953

Closed
danielroe opened this issue Sep 17, 2022 · 11 comments · Fixed by nuxt/framework#8278
Closed

prevent loading global style entry css file #14953

danielroe opened this issue Sep 17, 2022 · 11 comments · Fixed by nuxt/framework#8278

Comments

@danielroe
Copy link
Member

danielroe commented Sep 17, 2022

We now have styles inlined in the page (nuxt/framework#7160), and we've switched the global CSS file to a non-blocking prefetch. However, the vite preloader does still add these styles to the page, and we can probably avoid this (or have an option to avoid this).

Note: this may be unfeasible as entry.css may include more CSS than is inlined in the page, and be necessary for subsequent navigation. See #14943 for example.

@praveshishere
Copy link

praveshishere commented Sep 19, 2022

It would be nice to have a configuration option to disable prefetching entry.css (or delaying) on our end (if that's possible).
styles on subsequent pages could be loaded on demand.

@jgupta
Copy link

jgupta commented Sep 19, 2022

In favour of lighthouse score, since we already have critical css inlined we should defer non-critical css.

@jgupta
Copy link

jgupta commented Sep 20, 2022

Chrome developer tool performance insights shows entry.css as render blocking. For testing, we removed entry.css file using build:manifest hook and it improved lighthouse score significantly.

  hooks: {
    "build:manifest": (manifest) => {
      Object.values(manifest).forEach((entry) => {
        if (entry.isEntry) {
          entry.css = [];
        }
      });
    },
  },

@danielroe
Copy link
Member Author

danielroe commented Sep 20, 2022

@jgupta We should already do that:

https://github.com/nuxt/framework/blob/5855ba4684e36eb1e785f377d034277f008bc92a/packages/vite/src/server.ts#L132-L140

Would you provide a sandbox in which entry.css is render blocking?

@jgupta
Copy link

jgupta commented Sep 20, 2022

@danielroe below are two URLs.

Old URL (without our hook) and this new URL (with our hook). Both URLs have exact same code except the hook above.

The way entry.css is added is still a blocking request (at least in Chrome).
<link rel="prefetch stylesheet" href="/_nuxt/entry.c5c3adc4.css">

We may modify it as follows (source)
<link rel="stylesheet" href="/_nuxt/entry.c5c3adc4.css" media="print" onload="this.media='all'; this.onload=null;">

I can't find source but I have read somewhere that above might interfere with Content Security Policy (CSP). I am not sure about proper solution for this but if the css continues to be blocking, inline css is useless. Alternately, we might try adding css to early hints.

Copy link
Member Author

danielroe commented Sep 22, 2022

You're right, it seems blocking, and it shouldn't be. I've raised nuxt-contrib/vue-bundle-renderer#39 to address that issue.

@Anoesj
Copy link

Anoesj commented Nov 3, 2022

@danielroe below are two URLs.

Old URL (without our hook) and this new URL (with our hook). Both URLs have exact same code except the hook above.

The way entry.css is added is still a blocking request (at least in Chrome). <link rel="prefetch stylesheet" href="/_nuxt/entry.c5c3adc4.css">

We may modify it as follows (source) <link rel="stylesheet" href="/_nuxt/entry.c5c3adc4.css" media="print" onload="this.media='all'; this.onload=null;">

I can't find source but I have read somewhere that above might interfere with Content Security Policy (CSP). I am not sure about proper solution for this but if the css continues to be blocking, inline css is useless. Alternately, we might try adding css to early hints.

This change depends on JavaScript to load styles. Therefore, generated builds don't load this CSS file and styling breaks when JS is disabled. Can you take another look at another possible solution that doesn't require JavaScript to be enabled to make sites look like they should? 😄

Also, when JavaScript is enabled, there's a flash of unstyled elements with this solution. It doesn't look nor feel right!

Copy link
Member Author

Agreed. I think it is best to completely remove support for inlining entry CSS for now. ~> nuxt/framework#8666

@Anoesj
Copy link

Anoesj commented Nov 3, 2022

Thank you Daniel, that was fast! Will this be shipped with rc.13 or rc.14? Would be nice to include this in rc.13, since serving an unstyled website to users with JS disabled is a pretty big shortcoming 😬

@danielroe
Copy link
Member Author

This will be in rc13.

@manniL
Copy link
Member

manniL commented Nov 4, 2022

Thank you Daniel, that was fast! Will this be shipped with rc.13 or rc.14? Would be nice to include this in rc.13, since serving an unstyled website to users with JS disabled is a pretty big shortcoming 😬

Feel free to try it out on the edge channel though 😋

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants