inject <script type="module"> into <head> instead of <body>
See original GitHub issueIs your feature request related to a problem? Please describe.
vite buildHtmlPlugin injects <script type="module">
tags at the end of <body>
This has 2 potential issues with generic SSR (eg. headless chrome with puppeteer crawling a regular vite build)
- If the initial html is large, it may take longer for the browser to start downloading the scripts.
- If you run code that completely replaces the content of body, SSR result will no longer contain the scripts
Describe the solution you’d like
Inject module scripts in <head>
they are only executed after parse per spec: https://html.spec.whatwg.org/multipage/scripting.html#attr-script-defer
Describe alternatives you’ve considered rewrite index.html after vite is done, before SSR
Additional context Ran into this when i tried to build a simple export feature in svite, where the svelte app is mounted to document.body by default. Not an issue when you don’t use sveltes hydratable compiler option. But when you do, 2) above happens
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (9 by maintainers)
Top GitHub Comments
@underfin According to the spec link in the issue,
<script type="module">
s aredefer
red by default.According to the resource hints spec (https://github.com/w3c/resource-hints/issues/13, https://www.w3.org/TR/preload/#early-fetch-and-application-defined-execution), if I understand correctly,
<link rel="preload" as="script">
or<link rel="modulepreload">
work essentially the same as<script defer>
.See comment in #882