1

So it seem like in next.config.js our previous devs coded some pernament redirects like so:

{
  source: '/some-path',
  destination: 'https://storage.googleapis.com/company/some-path.html',
  permanent: true
}

Here's doc: https://nextjs.org/docs/api-reference/next.config.js/redirects

Now we would like to disable that redirect, and use normal page thats generated within next.js app.

I've removed the redirect from next.config.js, but since it used "permament" param it seems like the redirect is cached in browser (if user visited that redirect previously they'll have to clear cache to actually see the new page, since it uses response code 308 Permament Redirect).

I've found two possible solutions:

  • change the url we're using so the cache wont trigger redirect
  • set redirect on that external site back to our page (seems like dirty solution)

Is there other way to fix this? Or maybe default next.js redirect cache has some validation time and it will be fine in few days or something?

  • 1
    Changing the url so that cache won't trigger redirect can cause SEO inefficiency, because the old url has already been indexed by the search engines. – Hritik Sharma Feb 14 '23 at 12:37

1 Answers1

0

You can add the Cache-Control and Expires headers to delete cache. Example from docs:

async redirects() {
    return [
      // if the header `x-redirect-me` is present,
      // this redirect will be applied
      {
        source: '/:path((?!another-page$).*)',
        has: [
          {
            type: 'header',
            key: 'x-redirect-me',
          },
        ],
        permanent: false,
        destination: '/another-page',
      },
      // ....
rycha
  • 679
  • 2
  • 9
  • But if the cache is cleared from the user's browser, then he has to login again which might increase one more step for a user to access the website, just a thought from user's perspective. – Hritik Sharma Feb 14 '23 at 12:33
  • another way - hard reset in browser)) Yes, its bad for user, but there aren't many ways to reset the cache – rycha Feb 14 '23 at 12:46
  • Yeah, I agree with you. – Hritik Sharma Feb 14 '23 at 12:47
  • @rycha From what I've understand this wont. If the used already has redirect cached, overriding the config wont work. Editing the redirect to: { source: '/originalPath', destination: 'https://google.com', }, wont affect the catched redirecting. – stackustack Feb 14 '23 at 13:16
  • 1
    i thought about it after sending response)) And if this is true, only one way - hard reset in browser. And for user comfortability may be more simple will be change route. Invalidate cache its problem every time. But next time when use permanent redirects - adding headers with expire date not bad practice) – rycha Feb 14 '23 at 13:22
  • ... Adding cache-control / expires headers will not help as well, as redirect cache is already stored (and from what i understand it can't be overridden as its was marked as "permanent"... or am I wrong?). – stackustack Feb 14 '23 at 13:22
  • > But next time when use permanent redirects - adding headers with expire date not bad practice) yea, luckily I was not responsible for that implementation, but sadly I have to deal with that right now. Thanks for help anyways! – stackustack Feb 14 '23 at 13:24