vercel / next.js

The React Framework
https://nextjs.org
MIT License
123.06k stars 26.29k forks source link

Wrong and weird behaviour of context in getServerSideProps with incorrect src on img #29806

Closed alexbujenita closed 1 year ago

alexbujenita commented 2 years ago

What version of Next.js are you using?

11.1.2

What version of Node.js are you using?

14.17.6

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

local, both with dev and build-start

Describe the Bug

When using getServerSideProps with a param and then returning the data, if I'm rendering an with a wrong URL getServerSideProps gets called again but this time with the image URL as params!

GET http://localhost/person/1 Here here id is 1 GET http://localhost/person/3 Here is 3 and the again "image not found"

The ID 3 is with a wrong img src.

Screenshot 2021-10-11 at 12 06 51

Expected Behavior

To not call getServerSideProps again with this weird behaviour

To Reproduce

pages/api/person/[id].js

export default function handler(req, res) {

  const data = [
    { id: 1, name: "John Doe", image: null },
    { id: 2, name: "Jane Doe", image: "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/Nextjs-logo.svg/1200px-Nextjs-logo.svg.png" },
    { id: 3, name: "Blah Doe", image: "image not found" },
  ];

  const person = data.find((p) => parseInt(req?.query?.id) === p.id)

  res.status(200).json(person);
}

pages/person/[id].js

export default function Person({ name, image }) {
  return (
    <div>
      <p>{name}</p>
      {image && <img alt="person" src={image} />}
    </div>
  );
}

export async function getServerSideProps(ctx) {
    console.log(ctx.params)
    const res = await fetch("http://localhost:3000/api/person/" + ctx.params.id);
    const data = await res.json();
    return {
      props: data,
  }

/person/1 and /2 will cause no error, /3 is the one with the weird bug.

balazsorban44 commented 2 years ago

A few things:

  1. If you have access to the logic that runs in /api/person/[id].js to fetch the image, you should import that in getServerSideProps, instead of using a fetch call:

Note: You should not use fetch() to call an API route in getServerSideProps. Instead, directly import the logic used inside your API route. You may need to slightly refactor your code for this approach. Fetching from an external API is fine!

https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering

This will avoid an unnecessary trip to get some data.

  1. If your API resolves to return undefined as JSON, you won't be able to pass it as props, and Next.js is supposed to throw this error message:
Error: Error serializing `foo` returned from `getServerSideProps` in "/foo".
Reason: `undefined` cannot be serialized as JSON. Please use `null` or omit this value.

You should return null as a fallback prop, if there was no match.

In any case, with the code you provided, I could not reproduce the issue. The page renders "correctly" image

alexbujenita commented 2 years ago

@balazsorban44 That's just a small reproduction, I don't care about wrong data being returned.

It's mostly to show a weird behaviour, getServerSideProps getting called with a param taken from somewhere else, in this case the url from the image, but why?!

EDIT: yes the page does render, but take a look at the console, you'll have another weird call to the backend this time though instead of having the ID (like 3 in this case), you'll see one call with the ID and one with the URL of the image!

That's the bug that I'm reporting

EDIT_2: sorry, just realized now about your comment about not calling the next API from there, this is just an example, I had this issue where I was calling a different external API.

github-actions[bot] commented 1 year ago

Please verify that your issue can be recreated with next@canary.

Why was this issue marked with the please verify canary label?

We noticed the provided reproduction was using an older version of Next.js, instead of canary.

The canary version of Next.js ships daily and includes all features and fixes that have not been released to the stable version yet. You can think of canary as a public beta. Some issues may already be fixed in the canary version, so please verify that your issue reproduces by running npm install next@canary and test it in your project, using your reproduction steps.

If the issue does not reproduce with the canary version, then it has already been fixed and this issue can be closed.

How can I quickly verify if my issue has been fixed in canary?

The safest way is to install next@canary in your project and test it, but you can also search through closed Next.js issues for duplicates or check the Next.js releases.

My issue has been open for a long time, why do I need to verify canary now?

Next.js does not backport bug fixes to older versions of Next.js. Instead, we are trying to introduce only a minimal amount of breaking changes between major releases.

What happens if I don't verify against the canary version of Next.js?

An issue with the please verify canary that receives no meaningful activity (e.g. new comments that acknowledge verification against canary) will be automatically closed and locked after 30 days.

If your issue has not been resolved in that time and it has been closed/locked, please open a new issue, with the required reproduction, using next@canary.

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the :+1: reaction on the topmost comment (please do not comment "I have the same issue" without repro steps). Then, we can sort issues by votes to prioritize.

I think my reproduction is good enough, why aren't you looking into it quicker?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

balazsorban44 commented 1 year ago

This issue has been automatically closed because it wasn't verified against next@canary. If you think it was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you.

github-actions[bot] commented 1 year ago

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.