Closed alexbujenita closed 1 year ago
A few things:
/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.
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"
@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.
Please verify that your issue can be recreated with next@canary
.
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.
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.
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.
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
.
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.
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.
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.
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.
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.
Expected Behavior
To not call getServerSideProps again with this weird behaviour
To Reproduce
pages/api/person/[id].js
pages/person/[id].js
/person/1 and /2 will cause no error, /3 is the one with the weird bug.