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

178
Visualizações
How to load JavaScript chunks from webpage relative to origin URL using React Router?

I have a React client built with webpack. It adds every file to a URL/version/ folder.

However, once the HTML page is loaded, it loads a view making the URL URL/App/Home. After this point, every chunk is loaded from URL/App/Home/version/script.js instead of the intended URL/version/script.js.

I understand this can be related to publicPath in webpack. However, I can only add more folders to the URL, I cannot remove the App/Home or whatever is the current full URL.

This is the webpack config:

output: {
            filename: "[name].js",
            path: path.join(dir, build),
            publicPath: path.join(version, "/"),
        }

My router is a <BrowserRouter> at 5.1.2 without setting any properties.

Another thing that is interesting is that we have hardcoded in the HTML a script such as:

<script defer src="<%= htmlWebpackPlugin.options.prefix%>/script.js"></script>

This script loads fine, even if accessing through URL/App/Home.

I tried many different things, and the only thing that worked is manually setting a base tag in the HTML. However, this doesn't sound like a particularly clean solution especially since we have multiple environments, requiring different tags.

Another solution was using Hash Router, but we don't want the hashes in the URL.

Is there any clean solution to this I am missing or is it a constraint due to how the browser works?

about 4 years ago · Juan Pablo Isaza
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