Critical CSS for Nuxt
Paint the banner on the first frame by inlining its rules in the HTML.
What it does
The Nuxt module does all of this for you; there is nothing to set up. The rest of the page explains what it does.
The normal stylesheet import puts the stylesheet on the critical path: the browser waits for it before painting anything. On a slow connection that delay is longer than everything else the SDK does. critical.css is the small part of the stylesheet that paints the banner; put it inline in <head> and load the full stylesheet without blocking, and the banner is styled on the first frame.
| File | Contents | Size |
|---|---|---|
styles.css | Everything: banner, dialogs, toggles, recall button, reload notice | about 3.8 KB gzipped |
critical.css | Only what the banner needs to paint | about 1.8 KB gzipped |
Set it up
Nothing to do. On every page the module writes critical.css inline right after <meta charset>, ahead of every other tag in <head>, and links the full stylesheet so it loads without blocking; the SDK switches it on once it starts. A route the module adds serves that stylesheet at /cookieyes/styles.css, under your app's base URL (app.baseURL, or NUXT_APP_BASE_URL at run time), compressed and cached for a year under a versioned address. To serve it from another path, set stylesHref:
export default defineNuxtConfig({
modules: ["@cookieyes/nuxt"],
cookieyes: {
stylesHref: "/assets/cookieyes.css",
},
});The tags are written into the HTML Nuxt sends, so they are also there with ssr: false, on prerendered pages and on routes with swr, isr or cache route rules. nuxt generate writes the stylesheet as a static file next to your pages, since there is no server to answer the route.
If you send a Content-Security-Policy, the inline block needs its hash in style-src. See CSP.
Common mistakes
The preferences dialog is unstyled, the banner is fine.
The full stylesheet did not load. In the Network tab, /cookieyes/styles.css, or your stylesHref, must return CSS.
Next steps
- CSP: the
style-srchash for the inline block
- Configuration:
stylesHrefand the other module options