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

blog-with-comment issue mapping comments if undefined #61941

Closed
1 task done
aumbriac opened this issue Feb 12, 2024 · 2 comments · Fixed by #62041
Closed
1 task done

blog-with-comment issue mapping comments if undefined #61941

aumbriac opened this issue Feb 12, 2024 · 2 comments · Fixed by #62041
Labels
examples Issue/PR related to examples locked

Comments

@aumbriac
Copy link

Verify canary release

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

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
Binaries:
  Node: 18.17.1
  npm: N/A
  Yarn: N/A
  pnpm: 8.6.12
Relevant Packages:
  next: 14.0.3
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config:
  output: N/A

Which example does this report relate to?

blog-with-comment

What browser are you using? (if relevant)

Chrome/Opera GX

How are you deploying your application? (if relevant)

No response

Describe the Bug

The client application responds with Application error: a client-side exception has occurred (see the browser console for more information).
The bug happens when the comments array is undefined. This occurs locally as well as in production example:
image

Expected Behavior

When the user clicks on a blog post, the client application should not respond with Application error: a client-side exception has occurred (see the browser console for more information).

To Reproduce

Clicks on a blog post without comments or just go here: https://blog-with-comment.vercel.app/posts/prancing-pony

@aumbriac aumbriac added the examples Issue/PR related to examples label Feb 12, 2024
@icyJoseph
Copy link
Contributor

Hi,

The problem starts here:

We are hitting that line in the API route. Notably that returns a 400 with JSON payload.

Then when we SWR is used here:

We hit the second issue. fetch doesn't throw for 4xx range requests. A library such as axios does throw/error out for non 2xx status codes (let's not think of 3xx for now).

So when the fetcher does:

const fetcher = (url) => fetch(url).then((res) => res.json());

It'll simply decode the error message with error 400 we sent from the API route, and then pass that SWR, which puts it in the data state.

So comments is no longer an Array, but an Object, and you can see that if you debug the application:

Screenshot 2024-02-14 at 09 03 43

And we can confirm our assumptions by looking at the network tab, where we see a 400 Bad Request, from
https://blog-with-comment.vercel.app/api/comment?url=%2Fapi%2Fcomment

I'll make a PR to fix this, so that it throws when 400 comes in.

styfle pushed a commit that referenced this issue Feb 14, 2024
Fixes #61941 example blog-with-comment.

The root issue is that the SWR fetcher does not check the `ok` status of the response, and that way we end up putting the error message object into the SWR data. And then `.map` is invoked in the object.

Since this is a learning kind of example, I think perhaps the presented change is just about enough to understand what's going on. Whether or not the people using the example want to gain access to the message from the server is up to them.
Copy link
Contributor

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue/PR related to examples locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants