Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
How to precisely control where script tag is inserted using Next Script

I'm currently trying to add a third party script to my Next.js application. The script inserts an iframe directly below the script tag. So I need precise control over where the script tag is located on the page.

I'm currently using next/script because regular script tags are preventing the iframe from rendering. However, the next/script tag seems to relocate the script based on the strategy used: Using beforeInteractive strategy inserts the script into the head. afterInteractive and lazyOnload insert it somewhere near the bottom of the page.

If I have something like the following:

<div id="script-container">
  <Script id="script" async /* other attributes */ />
</div>

How would I ensure that the script remains inside the script-container div when the page loads?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Here is the somewhat hacky solution I came up with.

import React, { useRef } from 'react'
import Script from 'next/script'

export const ScriptComponent = (props) => {
  const containerRef = useRef(null)

  function moveScript() {
    containerRef.current.appendChild(this)
  }

  return (
    <div ref={containerRef} id="script-container">
      <Script
        id="script"
        type="text/javascript"
        src="#url"
        async
        onLoad={moveScript}
      />
    </div>
  )
}

Basically add an onLoad function that moves the script back to the container. I'm unsure how consistent this solution is overall since it depends on the script being moved before the iframe gets loaded, but it appears to be working with the script I'm using.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda