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

Closed
roman-may opened this issue Dec 21, 2020 · 12 comments · Fixed by #20572
Closed

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

roman-may opened this issue Dec 21, 2020 · 12 comments · Fixed by #20572
Labels
good first issue Easy to fix issues, good for newcomers kind: bug Confirmed bug that is on the backlog
Milestone

Comments

@roman-may
Copy link

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: {} };
};
@roman-may roman-may added the bug Issue was opened via the bug report template. label Dec 21, 2020
@timneutkens timneutkens added good first issue Easy to fix issues, good for newcomers kind: bug Confirmed bug that is on the backlog type: needs investigation and removed bug Issue was opened via the bug report template. labels Dec 22, 2020
@timneutkens timneutkens modified the milestones: 10.x.x, backlog Dec 22, 2020
@nghiepdev
Copy link
Contributor

Duplicate of #17113 and #17143

@roman-may
Copy link
Author

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
Contributor

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 :)

@roman-may
Copy link
Author

Thank you very much @kaykdm

@kaykdm
Copy link
Contributor

kaykdm commented Jan 23, 2021

I think this is fixed in this PR

@nghiepdev
Copy link
Contributor

nghiepdev commented Jan 23, 2021

@kaykdm PR just fix asPath for getServersideProps .
getInitialProps and const {asPath} = useRouter() still is incorrect 😭

@kaykdm
Copy link
Contributor

kaykdm commented Jan 23, 2021

@nghiepit
Have you tried with v10.0.6-canary.7 ?

@nghiepdev
Copy link
Contributor

@kaykdm
Reproduction v10.0.6-canary.7 https://codesandbox.io/s/sharp-cannon-kcsio?file=/pages/post.js

@kaykdm
Copy link
Contributor

kaykdm commented Jan 23, 2021

@nghiepit
Thanks for reproduction!
I just realized that I was testing with different branch.

You are right. This issue still exists, so I reopened my PR.
Thanks again!

@nghiepdev
Copy link
Contributor

@kaykdm Does your PR have a fix for useRouter()?

@kaykdm
Copy link
Contributor

kaykdm commented Jan 23, 2021

@nghiepit
I just tested, and it didn't have a fix for useRouter()
I will try to fix that when I have time.

@kodiakhq kodiakhq bot closed this as completed in #20572 Jan 25, 2021
kodiakhq bot pushed a commit that referenced this issue Jan 25, 2021
Fixes: #20370

> 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.

The same issue was used to happen on `getServersideProps`, but it was fixed in this PR (#17121).
`getInitialProps` needs same fix except when the target is serverless, which has correct `asPath` value.

Additional tests have been added in the `getInitialProps` suite to ensure correct asPath with rewrites.
@Timer Timer modified the milestones: backlog, iteration 16 Jan 25, 2021
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Easy to fix issues, good for newcomers kind: bug Confirmed bug that is on the backlog
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants