Skip to content

A way for plugins to inject tags to the rendered HTML #15

Open

Description

@sapphi-red

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/htmlTags virtual 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

Activity

  1. teemingc commented on Aug 21, 2026

    @teemingc

    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.

    It might even be that the client environment is never built if the app requires no JavaScript 🤔

  2. moved this from Backlog to In progress in ecosystemon Aug 24, 2026
  3. sapphi-red commented on Aug 24, 2026

    @sapphi-red
    MemberAuthor

    In today's metting we decided to try out the transformIndexHtml + manifest approach, which would work well for backend integration as well:

    • change transformIndexHtml's input parameter to string | undefined
    • If build.manifest is enabled, call transformIndexHtml hooks and collect the returned objects (if the hook returned a string, we warn and ignore). Put the list of returned objects in the manifest.
      • Note: we need to figure out how to do breaking changes for the manifest. The top level object is a key-value map so we cannot add new properties there without a breaking change.

    Also @sapphi-red mentioned about considering deprecating / removing the function-style (string in string out) transformIndexHtml after vitejs/vite#20374. After that PR, HTML will go through the plugin pipeline, so the same can be done using transform hook. If we avoid passing input to transformIndexHtml, it would be clear that transformIndexHtml would work for SSR cases as well, while transform for HTML files would only work if the HTML is part of the bundle.

  4. harlan-zw commented on Aug 25, 2026

    @harlan-zw

    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.

  5. sapphi-red commented on Oct 19, 2026

    @sapphi-red
    MemberAuthor

    @harlan-zw I've made a PR here: vitejs/vite#23837
    It would be great if you can try it 🙏

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions