Serverless functions assume they are executing in a node.js environment, and as such can not run in non-node based JavaScript platforms such as Cloudflare Workers.
Compiled serverless functions are completely self-contained and can execute without a node.js runtime.
It looks like output functions call require
in order to use the url
, stream
, path
, crypto
, and fs
built-in modules, and they also use process
which may not have the intended value. It would be nice to see this fixed (or discussion and documentation), as node.js may not be a viable runtime for some platforms. The use of fs
is particularly concerning as some platforms don't have any sort of filesystem, so providing a replacement for that could be difficult.
TBH running next.js in Cloudflare Workers is bad idea because it's very easy to hit CPU limit
@sheerun Depends what plan you're using. I've had no CPU issues executing Next.JS on Cloudflare Workers. Haven't got rendering working tho, but the CPU runtime shouldn't be a issue.
I've managed to get a proof of concept running: https://terribleplan.com/nextjstest
Currently the solution involves using webpack to re-bundle everything with mode: 'webworker'
and a bunch of node polyfills. The roughest bit other than converting fetch Request
and Response
into node req
and res
was figuring out to use a custom polyfill for path
since next uses path.posix
which isn't polyfilled by path-browserify
. I'm working on getting it all thrown into a module right now. I don't think that all of the issues are on the shoulders of next, but some things could be made better by not relying on node builtins (or dependencies that use node builtins).
There may also be trouble for larger apps since my simple hello world + styled components page weighs in at ~627KB, with the limit for workers being 1MB. I assume double-webpacking and polyfilling add a bunch since the serverless output is only ~420KB.
@terribleplan That's awesome! Do you have a repo available with your progress?
@StefanFransen It only works for 1 route right now, as I just discovered that workers have a peculiar limit of 1 script per domain for non-enterprise accounts. I am working on multiple possible workarounds, but won't release anything until the problem is solved somehow.
@StefanFransen I've got something working and published, but it comes with a few caveats: https://github.com/terribleplan/next-cloudflare
Ultimately, the simplest solution to this depends on the addition of a new target for next. I'm going to look into what actually getting this proposed and accepted looks like.
Closing this as we're not considering it at the current time. This is going to become even more problematic as we add support for Automatic Pre-rendering and API routes, so it's not likely we'll ever support this. Sorry!
@terribleplan Why not just build + export the NextJS application. Then upload all of the static assets in the out
dir to S3 upon exporting, and render the HTML files w/ Cloudflare Workers? Could probably inject API/GraphQL data into the page within the Worker aswell..
Better yet.. use Cloudflare Workers KV as your static asset hosting.
Most helpful comment
I've managed to get a proof of concept running: https://terribleplan.com/nextjstest
Currently the solution involves using webpack to re-bundle everything with
mode: 'webworker'
and a bunch of node polyfills. The roughest bit other than converting fetchRequest
andResponse
into nodereq
andres
was figuring out to use a custom polyfill forpath
since next usespath.posix
which isn't polyfilled bypath-browserify
. I'm working on getting it all thrown into a module right now. I don't think that all of the issues are on the shoulders of next, but some things could be made better by not relying on node builtins (or dependencies that use node builtins).There may also be trouble for larger apps since my simple hello world + styled components page weighs in at ~627KB, with the limit for workers being 1MB. I assume double-webpacking and polyfilling add a bunch since the serverless output is only ~420KB.