-
Hi, @yyx990803 , the speed of vite was impressive. But I got a little problem when I was trying to show a loading DOM in the index.html as soon as possible, the entry js was injected to the head by default, and I found that it's not configurable yet. Source code is here: vite/packages/vite/src/node/plugins/html.ts Line 362 in 8c51bc0 After execute <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
<script type="module" crossorigin src="/assets/index.0f5d1ba9.js"></script>
<link rel="stylesheet" href="/assets/index.2338dd34.css">
</head>
<body>
<div id="app">
<div>Loading........</div>
</div>
</body>
</html> And what I expected was like this: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
<link rel="stylesheet" href="/assets/index.2338dd34.css">
</head>
<body>
<div id="app">
<div>Loading........</div>
</div>
<script type="module" crossorigin src="/assets/index.0f5d1ba9.js"></script>
</body>
</html> If it's possible to use code below: // inject chunk asset links
if (chunk) {
const assetTags = [
// preload for imports
...getPreloadLinksForChunk(chunk),
...getCssTagsForChunk(chunk)
]
result = injectToHead(result, assetTags)
result = injectToBody(result, [
// js entry chunk for this page
{
tag: 'script',
attrs: {
type: 'module',
crossorigin: true,
src: toPublicPath(chunk.fileName, config)
}
}
])
} instead of: // inject chunk asset links
if (chunk) {
const assetTags = [
// js entry chunk for this page
{
tag: 'script',
attrs: {
type: 'module',
crossorigin: true,
src: toPublicPath(chunk.fileName, config)
}
},
// preload for imports
...getPreloadLinksForChunk(chunk),
...getCssTagsForChunk(chunk)
]
result = injectToHead(result, assetTags)
} |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 5 replies
-
I found that a script element with |
Beta Was this translation helpful? Give feedback.
-
I think, in better way it should be configurable, instead of use another plugins to fix this behave |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
Vite is great, but it is true that it is necessary for the project to be configurable in which places the developers want which assets to be injected (in the head, or at the beginning or end of body, etc.). If the loading/rendering speed is to be precisely optimized, it is necessary for the developers to be able to determine in their project where they want to inject what. Related to this is the work with the critical CSS that we want to have in the , but the rest can be inserted later. We are starting to develop some of our projects in Astro, which uses Vite. The fact that it is not possible to determine where JS/CSS is injected, it is not possible to use some of super functionalities of libraries like a HTMX/Unpoly/HotWire. An example can be the fact that some of these libraries are not yet able to merge (in case of movement between pages), while interpreting CSS/JS in the replaced HTML content is without any problems. |
Beta Was this translation helpful? Give feedback.
-
I have another interesting use case where it would be really handy if Vite did not change the location of the I have a server-rendered Python web app that uses Jinja2 templates, most of which don't have I'm using Vite to bundle JS/CSS and to get hashes in asset file names for production. This almost works well, BUT whenever a file doesn't have a My hacky workaround was to write a plugin that runs after Vite has done its bundling, looks at the manifest, and then goes and transforms the original HTML from scratch again, using regular expressions to find the |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
-
This worked for us:
The plugin looks for the script tag in the built html, removes it and then pastes it where the "
Maybe not the cleanest solution, but it works! |
Beta Was this translation helpful? Give feedback.
-
I can not get it to run, i am supposed to put it inside the vite.config.js i think, but nothing happens, anyone help please ? |
Beta Was this translation helpful? Give feedback.
I found that a script element with
type=module
defer by default , so its position has no effect on the dom loading, however, the position of<link rel="stylesheet" href="/assets/index.2338dd34.css">
will.