Next.js Discord

Discord Forum
When it's placed anywhere inside your app, nextjs will automatically place it at the end of your head","url":"https://nextjs-forum.com/post/1324631696045244416#message-1324642561381367898","dateCreated":"2025-01-03T07:36:37.302Z","author":{"@type":"Person","name":"B33fb0n3"},"upvoteCount":1}}}

How to add custom inline script into head element?

Answered
Eared Grebe posted this in #help-forum
Open in Discord
Eared GrebeOP
In my project, there is a requirement of putting custom scripts, both from external source and inline script into the head element dynamically.

Script tag doesn't help because it only supports external source.

Is there a way to do that?
Answered by B33fb0n3
You can use the <Script /> tag from nextjs itself. As you said: it supports external source and you can add inline script like:
<Script id="my-script">{`console.log('Hello world!');`}</Script>

When it's placed anywhere inside your app, nextjs will automatically place it at the end of your head
View full answer

4 Replies

@Eared Grebe In my project, there is a requirement of putting custom scripts, both from external source and inline script into the head element dynamically. Script tag doesn't help because it only supports external source. Is there a way to do that?
You can use the <Script /> tag from nextjs itself. As you said: it supports external source and you can add inline script like:
<Script id="my-script">{`console.log('Hello world!');`}</Script>

When it's placed anywhere inside your app, nextjs will automatically place it at the end of your head
Answer
Eared GrebeOP
Thank you. Let me try.
happy to help