Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

293
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!