SSR CSS injection in development
See original GitHub issueRelevant previous issues / discussion:
- https://github.com/vitejs/vite/issues/2013
- https://discord.com/channels/804011606160703521/804061937029218334/814909610372366346
User-land vue-specific patch that (partially, see NOTE below) works for me (haven’t seen how it hydrates yet) –
/* relevant snippet from server.js */
const cssUrls = new Set(), cssJsUrls = new Set()
function collectCssUrls(mod) {
mod.importedModules.forEach(submod => {
if (submod.id.match(/\?vue.*&lang\.css/)) return cssJsUrls.add(submod.url)
if (submod.file.endsWith(".css")) return cssUrls.add(submod.url)
if (submod.file.endsWith(".vue")) return collectCssUrls(submod)
/* TODO include js files like routes that include other components */
if (submod.file.match(/route/)) return collectCssUrls(submod)
})
}
let render
if (!isProd) {
render = (await vite.ssrLoadModule("/src/entry-server.js")).render
const mod = await vite.moduleGraph.getModuleByUrl('/src/app.js') /* TODO replace with your entry */
cssUrls = mod.ssrTransformResult.deps.filter(d => d.endsWith(".css"))
} else {
render = require("./dist/server/entry-server.js").render
}
const [appHtml] = await render(url, manifest)
const devCss = [...cssUrls].map(url => {
return `<link rel="stylesheet" type="text/css" href="${url}">`
}).join("") + [...cssJsUrls].map(url => {
return `<script type="module" src="${url}"></script>`
}).join("")
const html = template.replace(`<!--app-html-->`, appHtml).replace(`<!--dev-css-->`, devCss)
Curious about latest thoughts on this!
NOTE: It looks like there is some non-determinism to the above… sometimes css modules are missing…
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:12 (10 by maintainers)
Top Results From Across the Web
Advanced (LEGACY) - MUI System
By default, the style tags are injected last in the <head> element of the page. They gain more specificity than any other style...
Read more >Server-Side Rendering (SSR) - Vue.js
Overview #. What is SSR? #. Vue.js is a framework for building client-side applications. By default, Vue components produce and manipulate DOM in...
Read more >How I made it easy to develop on Vue.js with server-side ...
In my new project I decided to use Vue.js. I needed server-side rendering (SSR), CSS modules, code-splitting and more cool things.
Read more >Why insert style again on client-side when using ssr? (from ...
why should we inject CSS again on client-side if it's already injected server-side? Is there any difference between client-side cache and server ...
Read more >esbuild CSS Modules Plugin for Server-Side Rendering
I found this quite challenging when developing my Serverless React SSR template. I am using the esbuild JS bundling tool quite extensively ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I just inlined the css as
<style>
tags in the HTML:Some changes need to be made in updateStyle in order to make hydration work, or at least expose
sheetsMap
and leave that to users:https://github.com/vitejs/vite/blob/89e0e33b66ecf199fedce18628f0c382e45f93a6/packages/vite/src/client/client.ts#L195
I posted a working implementation on the other issue. Both the attempts from this issue and the alternative I proposed use artifacts of the internal build process, which leads to instability.
Here is a way to test
After that open localhost:3000, the html will have the styles embedded.
It is a small change from ssr-vue from the playground of this very repository, the minimal test case to show how to do it, here are the changes from the vanilla ssr-vue: https://github.com/ferdinando-ferreira/vite/commit/e70bd50575bdf8413d77fbd0d10c0e669f1b3dcb