Add an option to inline generated CSS in Nuxt
Clear and concise description of the problem
When using the @unocss/nuxt module, the bundled css is currently being inserted as a link (Something like <link rel="stylesheet" href="/_nuxt/entry.1e37fde4.css">). This works fine when running a solution in development mode, but after building the solution, this causes a FOUC issue (Flash of unstyled content), since the css is being fetched after the html.
If I find the time at some point, I might do a PR (My first one here, wohoo ). It will probably not be soon though, so if someone else would pick it up, it would be great!
Suggested solution
It would be amazing if we had the option to embed the css directly in the head, so that it's being fetched with the html. It could be added as module option, just like autoImport, so it would look something like this in nuxt.config.js:
defineNuxtConfig({
modules: [
['@unocss/nuxt', { embed: true }]
]
});And the result would then look like this:
<head>
<!-- ... -->
<style>
.bottom-0,.bottom-0px {
bottom: 0
}
.bottom-100\% {
bottom: 100%
}
.bottom-auto {
bottom: auto
}
/* And so on .. */
</style>
</head>Alternative
Before switching to Uno we used Tailwind, where it were possible to do the following in our index.css file:
@tailwind base;
@tailwind components;
@tailwind utilities;
@tailwind screens;Which would then insert the css directly into the file, which we could then embed through the css portion of nuxt.config.js.
There's already something similar in @unocss/postcss, unfortunately this package is marked as experimental, and fails for us when using the Wind preset. If this is preferred more than my primary suggestion, it would be nice to also have this functionality directly in the @unocss/nuxt module.
@unocss/postcss example from documentation:
/* style.css */
@unocss preflights;
@unocss default;
/*
Fallback layer. It's always recommended to include.
Only unused layers will be injected here.
*/
@unocss;Additional context
We've also tried disabling autoImport and then importing it manually from the css portion of nuxt.config.js:
defineNuxtConfig({
modules: [
['@unocss/nuxt', { autoImport: false }]
],
css: ['virtual:uno.css'],
});After looking closer at the source code however, we realised how little sense this makes
Validations
- Read the Contributing Guidelines.
- Read the
README.mdof using the package. - Already used the Interactive Docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Source: unocss/unocss