-
Notifications
You must be signed in to change notification settings - Fork 27.7k
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
Preview mode broken in Next v10.0.2+ for target: "serverless": TypeError: e[t] is not a function #20575
Comments
When compiling the code for jsonwebtoken with ncc, the compiled code defines __webpack_require_. This function conflicts with the same-named function at Next.js page-level and thus causes an unintentional fatal error when using jsonwebtoken in a Next.js app that has been built with target: "serverless". For example, in an API route that uses `setPreviewData` (which uses jsonwebtoken). Fixes vercel#20575.
I just created a PR that "un-ncc"s Tested it locally (with |
Thanks for the very clear replication info here @FinnWoelm. Finally managed to track this one down with a fix in vercel/ncc#633. As soon the ncc update is released we can reflect that build here which in my testing resolves the issue in your replication. |
Also facing this, very frustrating because the error is not intuitive at all in pointing out what's wrong. |
Hi @rdsedmundo, I've heard (through the grapevines from @lindsaylevine) that this is fixed in Next v10.0.6, which was released four days ago. Are you perhaps still on v10.0.5 or earlier? |
Per above comment it looks like this was fixed after ncc was updated so going to close this, if you're still encountering problems with this please reply with additional information and we can investigate further! |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
Given any API route with
setPreviewData()
, such as the following,that is built with
target: "serverless"
using Next v10.0.2 or later, will cause the following Node error when executing the page's functiondefault()
:The error occurs in the compiled API route, during
![image](https://user-images.githubusercontent.com/7606194/103293686-4e248f00-4a2b-11eb-8925-4818d1198d5b.png)
setPreviewData
, when the page attempts to__webpack_require__
jsonwebtoken
:Specifically, the error occurs when
![image](https://user-images.githubusercontent.com/7606194/103293829-96dc4800-4a2b-11eb-93ec-f2ceba107fd0.png)
jsonwebtoken
tries to requireBuffer
(NkYg
):This is because the code tries to load
![image](https://user-images.githubusercontent.com/7606194/103294007-f2a6d100-4a2b-11eb-8db8-c750992e01e9.png)
Buffer
with__webpack_require__
, which is a function that is redefined/overwritten by thejsonwebtoken
code:The root of this issue goes back to vercel/ncc. The compiled code for
jsonwebtoken
redefines__webpack_require__
, which takes priority over the__webpack_require__
defined at the page-level where Buffer/NkYg
is defined. A similar issue has been reported here: vercel/ncc#589This issue seems to have been introduced in
vercel/ncc
v0.24, which was introduced tonext
in v10.0.2 (#18873). In Next v10.0.1 and before, the jsonwebtoken code does not define__webpack_require__
in a way that overwrites the function at the page-level.To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
npm install
npm run build
(ornext build
)node request.js
, which simulates a HTTP request and calls the API route'sdefault()
functionnpm install next@10.0.1
npm run build
and re-runnode request.js
{success:"true"}
in console.Expected behavior
Running
setPreviewData()
in an API route should not cause a fatal error. Preview mode is currently not usable in Next v10.0.2 withtarget: "serverless"
.Screenshots
See screenshots included in description above.
System information
Additional context
It is much easier to trace the code and error when looking at a prettified version of the compiled API route and when looking at the prettified versions of the compiled jsonwebtoken code. I've included these in the
prettified
folder: https://github.com/FinnWoelm/next-serverless-preview-mode/tree/main/prettifiedsetPreviewData
: L5876jsonwebtoken
: L5899Buffer
: L15472__webpack_require__
: L15492Possible workarounds:
target: "experimental-serverless-trace"
patch-package
to replace the two instances ofnext/dist/compiled/jsonwebtoken
withjsonwebtoken
in https://github.com/vercel/next.js/blob/master/packages/next/next-server/server/api-utils.ts (patch here) and installingjsonwebtoken
as a dependency withnpm install jsonwebtoken@3.5.1
.Possible fix: Do not use
vercel/ncc
to precompilejsonwebtoken
until this issue (vercel/ncc#589) is fixed invercel/ncc
. This would follow the same approach that was taken withnode-fetch
,etag
,chalk
, andraw-body
, which were all "un-ncc-ed" whenvercel/ncc
was upgraded to v0.25 in #18873.PS: Huge fan of your work♥️ Let me know if I can provide any more information.
The text was updated successfully, but these errors were encountered: