Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to control CSS and JS includes within HTML template #419

Closed
spacedawwwg opened this issue Aug 16, 2022 · 4 comments
Closed

Ability to control CSS and JS includes within HTML template #419

spacedawwwg opened this issue Aug 16, 2022 · 4 comments
Labels
enhancement ✨ New feature or request

Comments

@spacedawwwg
Copy link

spacedawwwg commented Aug 16, 2022

Description

It would be nice to fine tune the positioning of asset includes within the HTML.

As a front-end developer, I want to be in charge, in very fine detail, where specific tags are included. Currently all CSS is prepended to the <head> and scripts appended to <body>.

In the case of CSS, the CSS its standard practice to be at least included after a charset meta tag.

In the case of Javascript, there could be blocking scripts that need to be included after the vite compiled injections.

Currently, we currently doing this via a "post-build" script that loops over all the statically generated files - but this is not suitable for any pages using SSR.

Suggestion

A template tag (e.g. {{CSS_PLACEHOLDER}} {{JS_PLACEHOLDER}}) that can be utilised within the templates and if they do not exist, fall back to current implementation.

@spacedawwwg spacedawwwg added the enhancement ✨ New feature or request label Aug 16, 2022
@spacedawwwg spacedawwwg changed the title Ability to dictate CSS and JS includes with HTML template Ability to control CSS and JS includes within HTML template Aug 16, 2022
@brillout
Copy link
Member

How about this?

import { escapeInject, injectAssets, dangerouslySkipEscape } from 'vite-plugin-ssr'

export { render }

function render(pageContext) {
  return escapeInject`<!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        ${injectAssets(() => pageContext.pageAssets.filter(asset => asset.type==='style'))}
        <title>Vite App</title>
      </head>
      <body>
        <div id="page-view">${dangerouslySkipEscape(pageHtml)}</div>
        ${injectAssets(() => pageContext.pageAssets.filter(asset => asset.type==='script'))}
      </body>
    </html>`
}

@spacedawwwg
Copy link
Author

spacedawwwg commented Aug 19, 2022

This would be perfect!

@brillout
Copy link
Member

https://vite-plugin-ssr.com/preload

Released in 0.4.52.

In case your company is up for it: https://github.com/sponsors/brillout.

Some of what you said is still not possible though.

In the case of CSS, the CSS its standard practice to be at least included after a charset meta tag.

Why?

In the case of Javascript, there could be blocking scripts that need to be included after the vite compiled injections.

What's a use case for that?

I originally went for the design of my previous comment, but it didn't work out for various reasons.

@brillout
Copy link
Member

#695

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants