Next.js Discord

Discord Forum
","url":"https://nextjs-forum.com/post/1143885476919521363#message-1143888940886138910","dateCreated":"2023-08-23T12:46:16.000Z","author":{"@type":"Person","name":"Indian Spitz"},"upvoteCount":1}}}

Add script tag to <head> from within a sub route

Answered
Indian Spitz posted this in #help-forum
Open in Discord
Indian SpitzOP
Hey guys, anyone know how to add a script tag to head with other properties than those provided by the <Script> tag from a page within a sub route like /blog/[id]?

I've looked at https://nextjs.org/docs/app/building-your-application/optimizing/scripts but I can't figure out if it's possible to add it to <head>, it only ends up in <body>.

Is it even possible?

Many thanks in advance 🙇
Answered by Indian Spitz
Managed to solve it by adding the <Script> with strategy="beforeInteractive". It works but I get a warning which says:

next/script's beforeInteractive strategy should not be used outside of pages/_document.js. See: https://nextjs.org/docs/messages/no-before-interactive-script-outside-documenteslint@next/next/no-before-interactive-script-outside-document

I can live with it 🙂

  <Script id="schema.org" type="application/ld+json" strategy="beforeInteractive">
    {content}
  </Script>
View full answer

1 Reply

Indian SpitzOP
Managed to solve it by adding the <Script> with strategy="beforeInteractive". It works but I get a warning which says:

next/script's beforeInteractive strategy should not be used outside of pages/_document.js. See: https://nextjs.org/docs/messages/no-before-interactive-script-outside-documenteslint@next/next/no-before-interactive-script-outside-document

I can live with it 🙂

  <Script id="schema.org" type="application/ld+json" strategy="beforeInteractive">
    {content}
  </Script>
Answer