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

286
Visualizações
Dynamic redirects with Gatsby

Given a Gatsby application with dynamically generated product urls on build:

  • /product/x/
  • /product/x/variant1/
  • /product/x/variant2/
  • /product/y/
  • /product/y/variant1/
  • ...

Is there a way to automatically cause access to non-existing sub-urls of each product to redirect to the root of that product? Preferably on the server-side (gatsby-ssr.js).

Like this:

  • /product/x/variantdoesnotexist/ => /product/x/
  • /product/x/* => /product/x/ (When * is not a valid url part)
  • /product/y/variantdoesnotexist/ => /product/y/
  • ...

Only found a client-side solution so far, which includes checking path in the 404 page template and doing a conditional window.location.assign. This is not very optimal as it flashes the 404 page before redirecting and does not perform a 302/301 redirect.

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

0

You can use createRedirect action in the gatsby-node.js:

exports.createPages = async ({ graphql, actions }) => {
    const { createRedirect } = actions;
    
    createRedirect({
    fromPath: `/product/*/foo`,
    toPath: `/product/*`,
  });
}

Tweak it as you wish, you can use your current redirects rules within createRedirect.

Keep in mind that in createPages is where you create all dynamic pages hence you have all the slugs and paths available to make your dynamic redirects.

More details: https://support.gatsbyjs.com/hc/en-us/articles/1500003051241-Working-with-Redirects-and-Rewrites

about 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