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

Routing error in dev mode: TypeError: Cannot read property 'includes' of undefined (take two) #38047

Closed
1 task done
JoshuaKGoldberg opened this issue Jun 27, 2022 · 1 comment · Fixed by #38046
Closed
1 task done
Labels
bug Issue was opened via the bug report template.

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Jun 27, 2022

Verify canary release

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

Provide environment information

  Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP Fri Apr 2 22:23:49 UTC 2021
    Binaries:
      Node: 18.2.0
      npm: 8.9.0
      Yarn: 1.22.19
      pnpm: 6.11.0
    Relevant packages:
      next: 12.1.7-canary.48
      react: 18.2.0
      react-dom: 18.2.0

What browser are you using? (if relevant)

Chrome, but n/a

How are you deploying your application? (if relevant)

n/a

Describe the Bug

#16874 is still impacting users. I have a repro! If the network request to /_next/static/development/_devPagesManifest.json fails for any reason, the Next.js dev application crashes with:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'includes')
    at resolveDynamicRoute (router.js?8684:1314:16)
    at _callee$ (router.js?8684:969:31)
    at tryCatch (runtime.js?ecd4:45:16)
    at Generator.invoke [as _invoke] (runtime.js?ecd4:274:1)
    at prototype.<computed> [as next] (runtime.js?ecd4:97:1)
    at asyncGeneratorStep (_async_to_generator.js?0e30:23:1)
    at _next (_async_to_generator.js?0e30:12:1)

Expected Behavior

Next.js should be resilient to that manifest failing to load.

I took a dive into the source code and there are some spicy TypeScript shenanigans happening 😄. And some TODOs about handling this correctly! #38046 is a draft PR showing what I'd suggest doing to resolve this issue.

  1. The crash is coming from inside the framework, on if (pages.includes(fsPathname))
  2. pages is a parameter of resolveRewrites
  3. In calls to resolveRewrites, pages comes from this.pageLoader.getPageList()
  4. getPageList doesn't properly handle the case of fetch(.../_next/static/development/_devPagesManifest.json) failing: return this.promisedDevPagesManifest! has a // TODO Remove this assertion as this could be undefined comment

Link to reproduction

https://stackblitz.com/edit/vercel-next-js-flnphe?file=pages%2Findex.tsx

(really, any Next.js app with a Link can demonstrate this issue)

To Reproduce

  1. Start any Next.js app in development mode
  2. Add a <Link> somewhere on the page
  3. Visit localhost:3000 (or equivalent) in Chrome (or equivalent)
  4. In the network tab, right click on the _devPagesManifest.json request and select Block request url
  5. Refresh the page

Step 3 is the easiest way to simulate any of the reasons why that URL wouldn't be accessible that are mentioned in #16874. Apparently nginx configurations are more common culprits. Anecdotally I think I've seen this on CI runs that have flaky network layers (both GitHub Actions and CircleCI).

@JoshuaKGoldberg JoshuaKGoldberg added the bug Issue was opened via the bug report template. label Jun 27, 2022
@kodiakhq kodiakhq bot closed this as completed in #38046 Aug 7, 2022
kodiakhq bot pushed a commit that referenced this issue Aug 7, 2022
)

## Bug

- [x] Fixes #38047
- ~[ ] Integration tests added: do you want them added for such an unusual edge case?~ [Comment below: skipping](#38046 (comment))
- [x] Errors have helpful link attached, see `contributing.md`: ~I'll wait until this approach & error message are confirmed before thinking more deeply on how to explain the error~

Sending a draft PR as reference ahead of time. 🙂 

Doesn't resolve the root issue of why `_devPagesManifest.json` might fail to load. But does improve the log for when it happens. I'd suggest applying this same fix to `_devMiddlewareManifest.json` too.

<table>
<thead>
<tr>
<th></th>
<th>Before</th>
<th>After</th>
</tr>
</thead>
<tbody>
<tr>
<th>Console</th>
<td>
<pre>
<code>
Failed to fetch devPagesManifest TypeError: Failed to fetch
    at PageLoader.getPageList (page-loader.js?e87a:30:53)
    at _callee$ (router.js?8684:955:45)
    at ...
</code>
</pre>
<pre>
<code>
router.js?8684:1319 Uncaught (in promise) TypeError: Cannot \
read properties of undefined (reading 'includes')
    at resolveDynamicRoute (router.js?8684:1319:16)
    at _callee$
    at ...
</code>
</pre>
</td>
<td>
<pre>
<code>
Failed to fetch devPagesManifest: TypeError: Failed to fetch
    at PageLoader.getPageList (page-loader.js?e87a:30:53)
    at _callee$ (router.js?8684:955:45)
    at ...
</code>
</pre>
</td>
</tr>
<tr>
<th>Visual</th>
<td><img alt="Screenshot of a Next.js runtime error: TypeError: Cannot read properties of undefined (reading 'includes')" src="https://user-images.githubusercontent.com/3335181/175854728-8d5c2051-1229-4da7-8af1-fc95236befae.png" /></td>
<td><img alt="Screenshot of a Next.js runtime error: Error: Failed to fetch _devPagesManifest.json. Is something blocking that network request?" src="https://user-images.githubusercontent.com/3335181/175854774-07895846-1a1c-4bb8-bf57-fa696b8c6ba4.png" /></td>
</tr>
</tbody>
</table>
@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant