-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
How i can change inject script tag #14116
-
Documentation is
- Missing
- Outdated
- Confusing
- Not sure?
Explain in Detail
I have some problem. I want to move injected script tag from head to body. How do this?
Your Suggestion for Changes
I have some problem. I want to move injected script tag from head to body. How do this?
Reproduction
No response
Steps to reproduce
No response
Beta Was this translation helpful? Give feedback.
All reactions
Because it makes the page faster than injecting it to the body.
See https://flaviocopes.com/javascript-async-defer/
Replies: 1 comment 7 replies
-
You can do that by creating a plugin that uses the transformIndexHtml hook.
https://vitejs.dev/guide/api-plugin.html#transformindexhtml
In the hook, find the script tag and then move that to the body.
Beta Was this translation helpful? Give feedback.
All reactions
-
👎 4
-
why vite injected script tag to head by default ? and for change this we need create new plugin for this)
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 5
-
Because it makes the page faster than injecting it to the body.
See https://flaviocopes.com/javascript-async-defer/
Beta Was this translation helpful? Give feedback.
All reactions
-
thank you so much for this article. Best way injected script to head with defer, vite dont do this. If i want add this attribute, i need create new plugin for this?
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
We don't need to add defer because type='module' scripts are defer by default.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#defer
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 13 -
❤️ 1
-
thank you so much)) sorry for stupid questions)
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2 -
❤️ 3
-
thanks for your question,also thanks for everyone
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2