Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

router.asPath different between server and front on 404 page #40617

Closed
1 task done
Poyoman39 opened this issue Sep 16, 2022 · 6 comments
Closed
1 task done

router.asPath different between server and front on 404 page #40617

Poyoman39 opened this issue Sep 16, 2022 · 6 comments
Labels
bug Issue was opened via the bug report template. stale The issue has not seen recent activity.

Comments

@Poyoman39
Copy link

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT Mon Sep 5 10:15:47 UTC 2022
Binaries:
  Node: 16.16.0
  npm: 8.19.1
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 12.3.1-canary.2
  eslint-config-next: 12.3.0
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

Firefox developer edition 105.0b4 (64 bits)

How are you deploying your application? (if relevant)

No response

Describe the Bug

When accessing /someUnknownUrl

const router = useRouter();
console.log(router.asPath); // '/404' on server '/someUnknownUrl' on browser

useRouter does not work consistently between browser and server on 404 page.

Expected Behavior

router.asPath should be '/someUnknownUrl' on server too

Link to reproduction

https://stackblitz.com/edit/vercel-next-js-ycc47c?file=pages/404.tsx

To Reproduce

Go to an unknownUrl
(https://vercel-next-js-ycc47c--3000.local-corp.webcontainer.io/unknownUrl)

You can see "Error: Text content does not match server-rendered HTML" caused by different rendering

@Poyoman39 Poyoman39 added the bug Issue was opened via the bug report template. label Sep 16, 2022
@ShaneMaglangit
Copy link

ShaneMaglangit commented Sep 16, 2022

Isn't this an expected behavior as stated here?

While rendering your application, there was a difference between the React tree that was pre-rendered (SSR/SSG) and the React tree that rendered during the first render in the Browser.

It was pre-rendered with 404 on the server but it changed to the current slug on the first render. The error is easily resolved when you apply the stated fix.

import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';

const Custom404 = () => {
  const router = useRouter();
  const [slug, setSlug] = useState<string>();

  useEffect(() => {
    setSlug(router.asPath.substr(1).replace(/([A-Z])/g, ' $1'));
  }, [router]);

  return <main>{slug}</main>;
};

export default Custom404;

@Poyoman39
Copy link
Author

But router.asPath should return the same thing on server and client no ?

Thank you for your quickfix anyway ;)

@Poyoman39
Copy link
Author

If router.asPath is intended to return different content on server and on client this is not a bug then ... ;)

@ShaneMaglangit
Copy link

ShaneMaglangit commented Sep 24, 2022

If router.asPath is intended to return different content on server and on client this is not a bug then ... ;)

I believe it is more of a side effect of pre-rendering rather than an intended effect. During pre-rendering inside the server, the router is unaware of the domain where it will be sent. It assumes its at /404 based on the filename, but it doesn't actually exists in a browser yet (its in the server, a server does not have a URL/web address).

@vercel-release-bot
Copy link
Collaborator

This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

@vercel-release-bot vercel-release-bot added the stale The issue has not seen recent activity. label Jan 10, 2025
@nextjs-bot
Copy link
Collaborator

This issue has been automatically closed due to two years of inactivity. If you’re still experiencing a similar problem or have additional details to share, please open a new issue following our current issue template. Your updated report helps us investigate and address concerns more efficiently. Thank you for your understanding!

@nextjs-bot nextjs-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. stale The issue has not seen recent activity.
Projects
None yet
Development

No branches or pull requests

4 participants