Next.js Discord

Discord Forum

NextJs Redirect - Not working?

Unanswered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
How can I force a redirect to work reliably? Adding random parameters to make it function isn't the right approach. I find the redirect behavior to be quite unpredictable. Any assistance would be appreciated. I just need to enforce a redirect while ignoring the cache and other factors. Thanks!

  // set nextUrl from query params in login.tsx
  useEffect(() => {
    setNextUrl(searchParams.get('nextUrl') || urls.site.home);
  }, [searchParams]);

  // redirect if already logged in
  useEffect(() => {
    if (state.isLoggedIn) {
      logger.info('Login successful', nextUrl);
      router.push(`${nextUrl}?protected=somerandomstring`);
    }
  }, [state, nextUrl, router]);

1 Reply