Repository navigation
Description
Plugins using transformIndexHtml hook does not work well in build for meta-frameworks that handle HTML on their own, especially when the HTML is generated in the app at runtime. transformIndexHtml hook takes the HTML as an input and that is known at build time if the HTML is generated at runtime.
Suggested solution
Introduce a Vite built-in virtual module (vite/app/htmlTags) that aggregates head/body tags declared by plugins so the application can inject them manually during SSR.
- Plugin side (e.g., Fontaine): Plugins specify a virtual module mapping (e.g.,
plugin.appVirtualModule.htmlTags) that exports the head/body tags they intend to inject. - Meta-framework / Apps side: Import the unified
vite/app/htmlTagsvirtual module and inject those tags into their SSR output pipeline.import tags from 'vite/app/htmlTags' export function onRequest(requestHtml) { const processedHtml = processHtml(requestHtml, tags) return processedHtml }
Alternatives considered
Running transformIndexHtml in the app
This requires all plugins to be bundled in the production app build, which bloats the app bundle. It is also not known whether the plugin runs in any runtime.
Adding the list of results of transformIndexHtml in the build somewhere
This needs to call transformIndexHtml hooks at build time. However, the input is unknown, so the hook will be called with an empty string. The plugins may not expect that.
Adding builder.transformIndexHtml as a build equivalent of ViteDevServer.transformIndexHtml
This will only work for the case where the input HTML is known like CSR or SSG. It can be used for running against the template of SSR. But it feels more like a workaround.
Allowing functions in the virtual module of plugin.appVirtualModule.htmlTags
We can consider it if it's important. The tricky point is that the HTML might be a stream and not a simple string. Also the caller might want to inject tags as JSX (I assume RSC would want that). Allowing functions means the plugins (and the interface) would need to support all of these.
Open questions
What does this mean for the transformIndexHtml?
We can keep it, or remove it. Maybe we should use the same way for Vite's internal HTML processing.
Is it fine to assume that the client environment is built before the server environment?
Currently, it assumes that. If the environments are built in the opposite order, this won't work because the plugin does not have the information to put in the virtual module.
original discussion: https://discord.com/channels/804011606160703521/1539275125659279441/1539964804821688400
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn progress
Activity
teemingc commentedon Aug 21, 2026
It might even be that the client environment is never built if the app requires no JavaScript 🤔
sapphi-red commentedon Aug 24, 2026
In today's metting we decided to try out the
transformIndexHtml+ manifest approach, which would work well for backend integration as well:transformIndexHtml'sinputparameter tostring | undefinedbuild.manifestis enabled, calltransformIndexHtmlhooks and collect the returned objects (if the hook returned a string, we warn and ignore). Put the list of returned objects in the manifest.Also @sapphi-red mentioned about considering deprecating / removing the function-style (string in string out)
transformIndexHtmlafter vitejs/vite#20374. After that PR, HTML will go through the plugin pipeline, so the same can be done usingtransformhook. If we avoid passinginputtotransformIndexHtml, it would be clear thattransformIndexHtmlwould work for SSR cases as well, whiletransformfor HTML files would only work if the HTML is part of the bundle.harlan-zw commentedon Aug 25, 2026
Hi, Unhead maintainer here (Nuxt and wider ecosystem head layer).
The manifest approach looks good to me.
A big problem I have at the moment is getting the head tags that Vite is emitting into an object form so that Unhead can process them with the end-users'
useHead()calls - this is important so tag order is optimized for the preload scanner. See the Vue install docs as an example.At the moment, Unhead ships a bespoke HTML parser just to pull these out, and it is expensive, as you can imagine.
I'm also working on a compiled-mode which would be a good stress test for the manifest.
Otherwise, my only feedback at the moment is it may be useful to stamp which plugin added what meta in dev for devtool tracking purposes.
Happy to test on Unhead / Nuxt once there's a PR up.
fix(vite,stream): support manifest-mode `transformIndexHtml()` (#970)
sapphi-red commentedon Oct 19, 2026
@harlan-zw I've made a PR here: vitejs/vite#23837
It would be great if you can try it 🙏