Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How i can change inject script tag #14116

Answered by sapphi-red
jerryk1925 asked this question in Q&A
Discussion options

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

You must be logged in to vote

Because it makes the page faster than injecting it to the body.
See https://flaviocopes.com/javascript-async-defer/

Replies: 1 comment 7 replies

Comment options

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.

You must be logged in to vote
7 replies
Comment options

why vite injected script tag to head by default ? and for change this we need create new plugin for this)

Comment options

Because it makes the page faster than injecting it to the body.
See https://flaviocopes.com/javascript-async-defer/

Answer selected by sapphi-red
Comment options

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?

Comment options

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

Comment options

thank you so much)) sorry for stupid questions)

Comment options

thanks for your question,also thanks for everyone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
documentation Improvements or additions to documentation
Converted from issue

This discussion was converted from issue #14112 on August 16, 2023 06:30.

AltStyle によって変換されたページ (->オリジナル) /