NextJS 12 results in less standalone setup for serverless environments than NextJS 11 #33275
Replies: 10 comments 19 replies
-
|
Hey, please let me know if you make any progress on this. The serverless team is working on getting their plugin supporting next 12, and they have some partial progress in place. I'm looking to see what they do and hacking it into my own app. |
Beta Was this translation helpful? Give feedback.
-
|
I'm currently not working on it, sticking with NextJS 11 ... I mean, if you're ok with storing a Anyways, I don't see where a page is actually rendered anywhere in the commit you pointed to, I guess that's the interesting part... whether that can be solved in a non-hacky way without all these extra stuff or not. Will post in this thread if I find something interesting :) |
Beta Was this translation helpful? Give feedback.
-
|
I really agree with you @fast-reflexes . Thanks for your discussion! |
Beta Was this translation helpful? Give feedback.
-
|
Hi @fast-reflexes / all, Any chance you found a solution. I spent a lot of time this weekend trying to get Nextjs 12 running in a Lambda... I might give in and run a web server within a lambda and if so I found this - https://github.com/aws-samples/aws-lambda-adapter/blob/main/README.md. Any other suggestions on how to do this? |
Beta Was this translation helpful? Give feedback.
-
|
The problem is not that we can't run NextJS 12 in a Lambda, the problem is that NextJS 12 makes it more cumbersome and less Lambda friendly. I have a NextJS lambda handler that I wrote some time ago and in that project, I tried to explore all the ways you could generate a NextJS response from a Lambda event. The following were the methods I tried in that project:
So, what NextJS 12 essentially does is that it removes the possibility to use either of the first two methods and now we BOTH have to start a server AND have a
Sounds straightforward, and even though it's doable, there are a lot of small things that can go wrong :) Seems the previous posts are about using some outside layer to turn the Lambda event into a |
Beta Was this translation helpful? Give feedback.
-
|
Hi everyone 👋 I see that the What's the suggested way to bundle for serverless? For previous versions, on Stormkit we are checking for a For newer versions however, I see that providing the |
Beta Was this translation helpful? Give feedback.
-
|
@jinqian1998 any update on using https://github.com/aws-samples/aws-lambda-adapter |
Beta Was this translation helpful? Give feedback.
-
|
For what it's worth, I succesfully used the AWS CDK to deploy a Next.js application to Cloudfront, Lambda@Edge and S3. It uses the new |
Beta Was this translation helpful? Give feedback.
-
|
Hello, I created an example to show how to deploy a simple Next.js application on AWS Lambda with Lambda Web Adapter. You can find the code here: https://github.com/awslabs/aws-lambda-web-adapter/tree/main/examples/nextjs Thanks @rohanshiva for raising awslabs/aws-lambda-web-adapter#58 in Lambda Web Adapter repo. |
Beta Was this translation helpful? Give feedback.
-
|
Hey all! |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Run
next info(available from version 12.0.8 and up)What version of Next.js are you using?
12.0.8
What version of Node.js are you using?
14.17.5
What browser are you using?
Chrome
What operating system are you using?
MacOS
How are you deploying your application?
Other platform
Describe the Bug
Version 11
NextJS 11 had the
serverlesstarget which, despite some more dependencies than advertised, was an attempt at making serverless functions from each dynamic page. The idea was that the serverless page could be deployed like a render function only without any resources. This was in fact almost right, even though some dependencies actually existed, which was reported here: #26297. Nonetheless, despite these extra resources, you could actually copy the render function along with about five or so dependency chunks and it all worked; you didn't even have to run a server to render the response.Note well that NextJS adds some middleware to both API requests and API responses:
An arbitrary API page written for NextJS will assume that these exist. In NextJS version 11, one of the dependency chunks that I talked about, that resides inside the
.next/serverless/pages/chunksfolder, enhances thereqandresobjects with this in functionapiResolver.To sum up, in version 11, all you needed was the following file system to serve a given page:
Version 12
Version 12 has ditched the
serverlesstarget at the benefit of outputting a file for each page containing information about the dependencies. This way we will clearly see the needed dependencies and may copy them to the location where they're needed. There is also the option to let NextJS do this for us, whereby astandalonefolder withnode_modulesand the whole shebang is included. The problem now, no matter if we use the standalone option or not, theapiResolvermethod where the extra middleware is added has been moved tonode_modules/next/server/api-utils.jswhich means we MUST includenode_modulesfolder to get the expected NextJS equippedreqandresobjects in our standalone pages. It also seems that theapiResolvermethod, which was in version 11 called from the standalone api function itself, is now called from thebase-serverin the server package, indicating that we need to start a server to ornament thereqandresobjects with the necessary api middleware / helpers.Conundrum
It seems to me that from a general serverless perspective, NextJS just took a step backwards and we are with version 12 required to both keep a
node_modulesfolder and also start a Node / Next server to be able to properly handle api requests whereas we could do the same in version 11 without both. Starting a server is more time consuming than just rendering the response from a function and keeping thenode_modulesfolder around takes up more space than necessary as well. Perhaps given some specific serverless platform, this is a step in the right direction but if so, then it is a shame that it should be at the cost of a general usability in serverless contexts.Please enlighten me if I have misunderstood something or if I'm wrong in any way, I would be really glad if I am :)
Expected Behavior
Improved architecture from version 11, perhaps with the rendering functions actually being standalone as advertised instead of depending on the
.next/serverless/chunksfolder.To Reproduce
Start a new Next project with
npx create-next-appRun
next buildCopy the contents of
.nextfolder to a new folderserverlessVerify the dependencies for default api
helloendpoint in.next/server/pages/api/hello.js.nft.jsonto be only the mainpackage.jsonand.next/server/webpack-api-runtime.jsDelete all files in the
serverlessfolder except.next/server/webpack-api-runtime.jsand.next/server/pages/api/hello.jsAt the root, create a serverless entrypoint
index.jswith contentRun
node index.jsVerify error
res.status is not a functionThe default api endpoint makes use of NextJS specific
res.status()adres.json()which doesn't work since thereqandresobjects have not been ornamented with these extra capabilities through theapiResolverfunction.Adding
to
next.config.jsand using the entire.next/standalonein place of the aboveserverlessfolder does not change the results, indicating that we must indeed spawn a server (perhaps the includedserver.js) to get the embellishedreqandresobjects that are expected, which in turn also suggests that we need to keep anode_modulesfolder around.Beta Was this translation helpful? Give feedback.
All reactions