Next.js Discord

Discord Forum

(question) Is it possible to move script tags from the head to the body?

Answered
Goldstripe sardinella posted this in #help-forum
Open in Discord
Goldstripe sardinellaOP
is there any way to move <script> tags from the <head> to the <body> without writing client side logic like using <Script/> or writing logic in a useEffect? like something in the next.config.ts file? Just curious.

project:
- Next 15
- App Router
- Typescript
Answered by Losti!
I haven't found anything about it. The Script component should work with afterInteractive or lazyOnLoad. Mhm, sorry I can't help you...

I suggest the following:

- Create a test project and implement a simple Next Script to see if it works in another project.
- Look for repositories that implement Next/Script.
View full answer

15 Replies

Goldstripe sardinellaOP
(ping me please)
@Losti! Docs: https://nextjs.org/docs/app/api-reference/components/script
Goldstripe sardinellaOP
without writing client side logic like using <Script/> or writing logic in a useEffect?
I can't use beforeInteractive because the document doesnt exist then and afterInteractive is basically running on the client
please read the full message
@Goldstripe sardinella please read the full message
I think I gave a hasty answer. Have you tried using strategy="lazyOnload"?
@Losti! I think I gave a hasty answer. Have you tried using strategy="lazyOnload"?
Goldstripe sardinellaOP
ive tried everything
I'm surprised theres no way to do it with the config file
About a month ago, someone else apparently posted the same thing you mentioned here on the forum, and I've tried to help them. As far as I can remember, they ended up using a client-side script tag. But let me investigate if there's a solution that meets your needs.
the reason i dont want it to be client side is because the scripts will get injected after everything else has loaded. ive seen some websites that do this on the server somehow (before initial load)
@Goldstripe sardinella the reason i dont want it to be client side is because the scripts will get injected after everything else has loaded. ive seen some websites that do this on the server somehow (before initial load)
I haven't found anything about it. The Script component should work with afterInteractive or lazyOnLoad. Mhm, sorry I can't help you...

I suggest the following:

- Create a test project and implement a simple Next Script to see if it works in another project.
- Look for repositories that implement Next/Script.
Answer
Goldstripe sardinellaOP
well since theres no other way i guess i will close this
thanks for your time
<3!