next.js: calling `redirect()` inside a server component causes flashing UI

Link to the code that reproduces this issue

https://github.com/Fredkiss3/bug-next-redirect-flashing-ui

To Reproduce

  1. click on the link
  2. you will see a blank page while the page is being redirected

Current vs. Expected behavior

You can see in the video below that for server actions it works as expected, the page don’t flash while being redirected (this is the desired behavior), while for link it doesn’t :

https://github.com/vercel/next.js/assets/38298743/415037db-31a9-4cde-9b13-4f7c6eb99c3a

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:43:05 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6020
Binaries:
  Node: 18.17.1
  npm: 9.6.7
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 13.5.5
  eslint-config-next: 13.4.6
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 4.9.5
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router

Additional context

No response

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 20
  • Comments: 22 (4 by maintainers)

Most upvoted comments

@timneutkens Sorry for pinging you, but you mentioned in #49424 that specifically this feature is needed: https://github.com/facebook/react/pull/26854, which has been on idle for over than one year. Are we still waiting for this to be merged or is there something else planned moving forward? Is there an update on this? As far as I can see this is still a problem for v15.

I have the same issue. I am loading some data in a layout component and then redirecting to a nested route if the data is not empty.

Eg. accounts/[accountId]/[bankId]

Each page is dependent on the data from its parent, but accountId or bankId could be empty based on the user. The loading screen shows but once the redirect is called, the screen is blank until the nested route is loaded.

Current strategy is to try preload the data and directly route to the nested route, avoiding a possible redirect.

I guess current solution is to not use redirect function from NextJS and instead do our own useRouter redirection and pass the URL to page, so we can show nie UI with it. Hopefully this can be fixed soon.

@4lejandrito thanks for looking into this. For me, unfortunately, the workaround won’t work. I have a server component that does a redirect based on some logic and I see the flash even if I type the URL into the browser address bar and navigate directly. For now, I just minimized the number of cases when redirect happens by eagerly putting together the correct URL where the page is linked so redirect won’t even be necessary. This at least prevents flashing when the user navigates through the links on the UI.

Having the same issue here. I am using redirect to add a missing query parameter or replace it if it’s invalid. And I have a loading.tsx. When I navigate directly to a URL that doesn’t require any redirects the loading is seamlessly replaced by the rendered page. But when there’s a redirect, it flashes a white screen between the loading content and the rendered page. It should keep displaying the loading until the rendered page is ready even if there’s a redirect.