The Wayback Machine - https://web.archive.org/web/20210103231018/https://github.com/vercel/next.js/issues/20370
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

AsPath is incorrect on Server if you use rewrites and getInitialProps #20370

Open
roman-may opened this issue Dec 21, 2020 · 3 comments · May be fixed by #20572
Open

AsPath is incorrect on Server if you use rewrites and getInitialProps #20370

roman-may opened this issue Dec 21, 2020 · 3 comments · May be fixed by #20572

Comments

@roman-may
Copy link

@roman-may roman-may commented Dec 21, 2020

Bug report

Describe the bug

AsPath is incorrect on Server if you use rewrites and getInitialProps. On the server, asPath is the rewritten asPath while on the client asPath ist as given in the request URL.

To Reproduce

Start with a new next typescript project. And add:

Next.config.js

module.exports = {
  async rewrites() {
    return [
      {
        source: "/:sector(s1|s2)/product/:pid",
        destination: "/product/:pid",
      },
    ];
  },
};

/pages/product/[pid].tsx

import { NextPage } from 'next';
import { useRouter } from 'next/router';

const Product: NextPage = () => {
  const { asPath } = useRouter();
  console.log({ asPath });
  return <h1>Product</h1>;
};


Product.getInitialProps = ({ asPath, query }) => {
  // dataFetch
  return {};
};

export default Product;
  1. Go to /s1/product/42
  2. Compare logs on server and clientside.

Expected behavior

AsPath on server and client ist /s1/product/42

System information

  • OS: Windows
  • Version of Next.js: 10.0.3
  • Version of Node.js: 12.18.3

Additional Information

If you use getServerSideProps instead of getInitialProps it works as expected. You can test this by replacing

Product.getInitialProps = ({ asPath, query }) => {
  // dataFetch
  return {};
};

with

export const getServerSideProps: GetServerSideProps = async () => {
  // datafetch
  return { props: {} };
};
@nghiepit
Copy link
Contributor

@nghiepit nghiepit commented Dec 22, 2020

Duplicate of #17113 and #17143

@roman-may
Copy link
Author

@roman-may roman-may commented Dec 23, 2020

Im not sure if this is a duplicate as #17143 seems to be an issue with pages using static optimization / getStaticProps.
In this case, static optimization ist not used and the path including parameters is known on the server.
Since it works if you use getServerSideProps instead of getInitialProps i think it is of different origin, but I can close it if I'm mistaken and this is not the case.

@kaykdm
Copy link

@kaykdm kaykdm commented Dec 29, 2020

I think same issue was used to happen on getServerSideProps, but it was fixed in this PR (#17121).

getInitialProps needs same fix, and I am happy to work on this :)

@kaykdm kaykdm linked a pull request that will close this issue Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.