Artista
Artista

Reputation: 1

I get a "Unhandled Runtime Error TypeError: network error" if i try to redirect (router.push) from the client

I am trying to redirect to the welcome page after successfully submitting the form in my handleFinish function. Since no errors are raised in the try/catch block, I mark the form as 'complete' and expect the router to redirect. However, I encounter an "Unhandled Runtime Error: TypeError: Network Error" for a few (long) seconds before it eventually redirects to "/welcome".

`

const handleFinish = async () => {
setIsLoading(true);

if (!formState.data.privateMetadata.agreement) {
setAlertMessage('Please confirm the terms and conditions');
setShowAlert(true);
setIsLoading(false);
return;
}

const user = constructUser(formState);

try {
  console.log('Benutzerdaten werden aktualisiert...');
  await updateUserData(locale, user);

  console.log('Vor- und Nachname werden aktualisiert...');
  await updateFirstAndLastName(locale, user);

  console.log('Compliance-Status wird aktualisiert...');
  await updateCompilance(user.id);

  setAlertMessage('');
  setShowAlert(false);
  setIsLoading(false);

  console.log('Weiterleitung zu:', `/${locale}/welcome`);
  router.push(`/${locale}/welcome`);
} catch (error) {
  console.error('Fehler während der Anmeldung:', error);
  setAlertMessage('Bei der Anmeldung ist ein Fehler aufgetreten');
  setShowAlert(true);
  setIsLoading(false);
}

setAlertMessage('');
setShowAlert(false);
setIsLoading(false);
setIsComplete(true);
};`

I tried using router.push('/welcome') after marking the form as complete in my handleFinish function. I expected it to redirect immediately to the welcome page without any issues. Instead, I encountered an "Unhandled Runtime Error: TypeError: Network Error" for several seconds before the redirect finally occurred. I have already wrapped my API calls in a try/catch block, and no errors are being logged there. I'm not sure why this delay and error are happening before the successful redirect.

Upvotes: -1

Views: 31

Answers (1)

Artista
Artista

Reputation: 1

I was running Spotlight by Sentry in the background on PORT 8969 for error tracking. However, this setup caused errors when handling redirects in Next.js. I didn’t investigate the issue in depth, but everything works fine now after turning it off.

Upvotes: 0

Related Questions