allow adding scripts within `<body>`
See original GitHub issueEnvironment
Nuxt CLI v3.0.0-27319101.3e82f0f
Nuxt project info:
- Operating System:
Windows_NT
- Node Version:
v16.13.0
- Nuxt Version:
3.0.0-27313139.1c88580
- Package Manager:
npm@8.1.0
- Bundler:
Vite
- User Config:
head
,meta
,buildModules
- Runtime Modules:
-
- Build Modules:
nuxt-windicss@2.0.15
Reproduction
First Bug/Issue Link:
Second Bug/Issue Link:
linking script globally not working
Describe the bug
Using “body: true” when linking a script will not insert the script into the body
I have a project in which I have a script that runs to get various items from the DOM in order to add an event listener that triggers audio to play. The only problem is that the script loads before the DOM loads because it is located in the head. In Nuxt 2, the body: true part when linking a script positioned the script in the body. However, Nuxt 3 does not seem to be allowing this. My project still works… as of this point, but when I was creating the Codesandbox project that I made to reproduce the issue, it wouldn’t even trigger the function. It returns the item as being “null” because the item is nonexistent when the script loads. Am I linking it correctly? I would think not because it worked with Nuxt 2. Is it different in Nuxt 3 now?
I cannot link the script globally
I’ve tried doing multiple things globally, as this, which may just be the fact that Nuxt 3 is still undergoing development, but I just wanted to bring this up to make sure that it was known about. I have the script linked in the nuxt.config.ts
file. However, when I open the dev tools to check the head to see if the script is linked, it’s not there and obviously, nothing works then. I’ve also tried globally declaring the title (would be useful for the project that I am working on now, but generally, probably not) and it won’t work. Declaring it per page works fine (in between the Vue document <script> tags), just not globally per the nuxt.config.ts
.
Additional context
No response
Logs
//Logs from body:true bug
Uncaught TypeError: el is null
<anonymous> https://2gpsk.sse.codesandbox.io/script.js:10
script.js:10:1
[vite] connecting... client.ts:22:8
<Suspense> is an experimental feature and its API will likely change. runtime-core.esm-bundler.js:897:47
Uncaught TypeError: newEl.textContent is not a function
myFunction https://2gpsk.sse.codesandbox.io/script.js:3
EventListener.handleEvent* https://2gpsk.sse.codesandbox.io/script.js:10
script.js:3:9
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
This is now supported in the latest nuxt v3 fyi, https://github.com/nuxt/framework/issues/5014#issuecomment-1200140609 & https://github.com/vueuse/head/pull/67
In case anyone else is having trouble with this, there’s an example in the official documentation at
https://v3.nuxtjs.org/guide/features/head-management/
Scroll down to the “Body Meta Tags” section.
I added this to my AppLayout component, but I imagine it could be used in many different locations.