question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom server with TypeScript: error with spreading Set

See original GitHub issue

Bug report

Describe the bug

When having set up Next.js with a custom server and TypeScript, after having started up the custom server and going to localhost:3000 in the browser, there happens this error RangeError: Invalid array length on the server.

To Reproduce

Repository to reproduce the issue can be found here: https://github.com/tuomokar/simple-next-express-typescript

Clone the repo, run npm i and npm run dev and go to localhost:3000. In the command line, you should see the error mentioned above.

Expected behavior

The error shouldn’t happen.

System information

  • OS: Windows, but should happen on any OS
  • Version of Next.js: 9.5.3
  • Version of Node.js: 12.18.3

Additional context

Full stack trace:

RangeError: Invalid array length
    at __spreadArrays (C:\test\simple-next-express-typescript\.next\server\pages\_document.js:52:18)
    at getDocumentFiles (C:\test\simple-next-express-typescript\.next\server\pages\_document.js:277:13)
    at Head.module.exports../node_modules/next/dist/pages/_document.js.Head.render (C:\test\simple-next-express-typescript\.next\server\pages\_document.js:553:19)
    at processChild (C:\test\simple-next-express-typescript\node_modules\react-dom\cjs\react-dom-server.node.development.js:3293:18)    at resolve (C:\test\simple-next-express-typescript\node_modules\react-dom\cjs\react-dom-server.node.development.js:3124:5)      
    at ReactDOMServerRenderer.render (C:\test\simple-next-express-typescript\node_modules\react-dom\cjs\react-dom-server.node.development.js:3598:22)
    at ReactDOMServerRenderer.read (C:\test\simple-next-express-typescript\node_modules\react-dom\cjs\react-dom-server.node.development.js:3536:29)
    at renderToStaticMarkup (C:\test\simple-next-express-typescript\node_modules\react-dom\cjs\react-dom-server.node.development.js:4261:27)
    at renderDocument (C:\test\simple-next-express-typescript\node_modules\next\next-server\server\render.tsx:216:5)
    at renderToHTML (C:\test\simple-next-express-typescript\node_modules\next\next-server\server\render.tsx:768:14)

The error seems to be caused by the spread done here (introduced here), specifically by the attempted spread of the Set. This breaks because by default TypeScript does not support iterables on arrays.

A couple of ways to fix this would be:

  • Add downlevelIteration: true to the tsconfig generated by Next.js
  • Change that line to Array.from(new Set([...sharedFiles, ...pageFiles])) (funnily this works as the Array.from is transpiled a little differently than the spread)

I can make a PR to fix it but first I’d like to hear any opinions on the preferred way to fix it.

The first one of those, the downlevelIteration flag, introduces some other things to take into account regarding the emitted code (see for example https://mariusschulz.com/articles/downlevel-iteration-for-es3-es5-in-typescript)

The second one is a very simple but might be again very error prone in the future, because [...new Set()] is very concise and simple to use.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:27
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
rjeruecommented, Oct 22, 2020

Any updates? I have this occurring, but only when dev is false in the next function. Setting the following in my tsconfig does fix.

{
  "compilerOptions": {
    ...
    "downlevelIteration": true
  },
  ...
}
3reactions
tgrasslcommented, Jun 10, 2021

We had the same problem with a Custom Server + Typescript + Revalidate. As soon as the page was rebuilt the CSS+JS files were missing in the head tag. There was no error or warning message so we had to debug into the compiled next code. The reason was that in getDocumentFiles the spreaded Set for allFiles always returned an empty array.

@rjerue “downlevelIteration”: true fixed it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript errors and how to fix them
A list of common TypeScript errors and how to fix them.
Read more >
Set Up Next.js with a Custom Express Server + Typescript
In this post, I will walk you through how to make a Next.js application handled by a custom Express server with Typescript.
Read more >
Using spread syntax and new Set() with typescript
However, typescript report following error: Type 'Set' is not an array type. I am not typescript ninja, could someone tell me what is...
Read more >
TSConfig Reference - Docs on every TSConfig option
Setting the value to undefined will allow most JavaScript runtime checks for ... Turning on noImplicitAny however TypeScript will issue an error whenever...
Read more >
How To Implement Custom Error Responses in Express - Auth0
The 5xx class of HTTP status code refers to errors encountered by the server while processing the request. For security reasons, you should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found