NextJS keeps redirecting after the page is dynamically created

Hi there!

We have a NextJS website with some [slug].js files that take the content from a Headless CMS.

We added this to getStaticPaths:

  return ({
    paths,
    fallback: 'blocking',
  });

And this to getStaticProps:

  if (contentNotGood) {
    return ({
      redirect: {
        destination: '/path/to/redirect',
        permanent: false,
      },
    });
  }

It seemed to work, but then we had this problem:

  • We built the website when a blog post was published
  • Then we removed the blog post (switched to draft), and Next used the “redirect” part
  • Then we re-published the blog post, but Next kept redirecting (the blog post worked on local env)
  • To solve this, we had to re-build the website again

Also, if we add a new blog post, we can see it on the website without rebuilding it.

The question is, is this related to some Netlify limitation (like the On-Demand ISR path revalidation, written in the docs), or is there something wrong in the code?

Thanks in advance.

Hey there, @247X_Dev :wave:

Thanks for reaching out! Can you please share your site url as well as your most recent deploy log? This will help us look into why this is happening!

Hi @hillary, thanks for your reply

We had more problems related to this issue,
so we investigated further.

We’ve found that the problem is related to 301/302 cached responses, and what’s worse is that the problem shows up either if there’s a “trailing slash redirect”; if we request a URL like domain.com/slug/ NextJS’s response will be a 301 to domain.com/slug and from now on Netlify will always give a 301 from domain.com/slug/ OR domain.com/slug to domain.com/slug, generating an infinite loop of redirects.

We still can’t find a way to handle this problem; we tried to change the way NextJS handles trailing slash and Netlify’s Pretty URLs feature, but nothing worked.

Is this some “incompatibility” between Netlify and NextJS?

Thanks in advance.

@247X_Dev Thanks for that additional information, that helps! I’ve updated the issue that we have open with engineering with what you’ve found, and we’ll let you know as soon as we have an update from engineering.

Hi @amelia !

Any news on this?

Hey @247X_Dev , we believe this should be fixed if you re-deploy your site. Let us know if that doesn’t help!

Hi @amelia !

Yes, re-deploying our site clears the cache, and the routes work again,
but it’s not a definitive solution because if a user goes to a URL with a trailing slash after the re-deploy then the problem occurs again.

Hey there, @247X_Dev :wave:

Sorry to hear you are still encountering this. Can you please send us your actual URL so that we can investigate this further? We will need to see this happening in order to re-escalate the issue to the appropriate team.

Thanks for your patience!