formidablelabs / nextjs-serverless-demo Goto Github PK
View Code? Open in Web Editor NEWSimple Next.js on Lambda via Serverless demo
Home Page: https://nextjs-sls-sandbox.formidable.dev/blog/
License: MIT License
Simple Next.js on Lambda via Serverless demo
Home Page: https://nextjs-sls-sandbox.formidable.dev/blog/
License: MIT License
Use case is that we want to get Next.js configured for the server-side as follows:
assignDefaults
in https://unpkg.com/browse/[email protected]/dist/server/config.js wherein we pass a config object from reading next.config.js
and get it all fixed up per that function.assignDefaults
is not exported, and (2) we don't want to require
anything that has Next's webpack stuff involved (for loadConfig
s webpack hooks).For our current code, we have a pure object (with object values but no arrays), which we can get away with a naive Object.assign()
on, but this lacks:
Like the real assignDefaults
and loadConfig
do in Next.js proper.
dist/server/config.js
to a new file that exports assignDefaults
assignDefaults
and write it out to disk that gets included in the zip bundle.serverles-jetpack
to sls 3.remark 13.0.0 13.0.0 14.0.2 dependencies https://remark.js.org
remark-html 13.0.2 13.0.2 15.0.1 dependencies https://github.com/remarkjs/remark-html#readme
target: "serverless"
build.serverless-http
Use the lambda-at-edge
library to break up next into two lambdas:
default-lambda
that contains all the pagesapi-lambda
that contains just the api routesPre-rendered and other static assets are extracted from the .next
build and are uploaded to S3.
Cloudfront cache behaviours are then configured to send the requests along to either these lambdas or to fetch static assets that were uploaded to S3 (no lambda hit necessary).
referrer
)The TF IAM permissions module isn't needed for this demo. Let's remove all the terraform.
formidops
server
vs serverless
src
to clarify files. Leave server
as-isTODO
s in app and either fix or ticket.express
, with the Next.js router doing it all. Try out removing express
and using http
built-in module instead. (Fix or ticket)express.static()
in root handler.server
output. Maybe even compare against serverless
output or Next.js internals in how it decides deps for serverless
mode?allowMissing
/ dynamic.resolutions
config.trace-deps
https://github.com/FormidableLabs/trace-deps/blob/main/docs/configuration.mdPresumably lots of various changes to update to most current Next.js 12.
dist
file import a non-existent file in the packagehttps://unpkg.com/browse/[email protected]/dist/compiled/webpack/bundle5.js at line 141344 has in relevant part:
"use strict";
module.exports = require("next/dist/build/webpack/plugins/terser-webpack-plugin");
/***/ }),
However, https://unpkg.com/browse/[email protected]/dist/build/webpack/plugins/terser-webpack-plugin is just a directory with a sub src
directory in it.
We're seeing 5+ second initial load time for code.
It looks like a monkey-patch for node internals for webpack to decide between 4 and 5 around this code: https://www.runpkg.com/[email protected]/dist/next-server/server/config.js#397 (after prettifying)
await (0, _configUtils.loadWebpackHook)(phase, dir);
produces this error because of missing files, where awkwardly it looks like we have to include webpack even though we don't want to use it at runtime:
"Error: Cannot find module 'next/dist/compiled/webpack/webpack-lib'",
"Require stack:",
"- /var/task/node_modules/next/dist/build/webpack/require-hook.js",
"- /var/task/node_modules/next/dist/next-server/server/config-utils.js",
"- /var/task/node_modules/next/dist/next-server/server/config.js",
"- /var/task/node_modules/next/dist/next-server/server/next-server.js",
"- /var/task/node_modules/next/dist/server/next.js",
"- /var/task/server/index.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)",
" at Function.resolve (internal/modules/cjs/helpers.js:80:19)",
" at /var/task/node_modules/next/dist/build/webpack/require-hook.js:4:1520",
" at Array.map (<anonymous>)",
" at Object.<anonymous> (/var/task/node_modules/next/dist/build/webpack/require-hook.js:4:1474)",
" at Module._compile (internal/modules/cjs/loader.js:999:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
" at Module.load (internal/modules/cjs/loader.js:863:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
" at Module.require (internal/modules/cjs/loader.js:887:19)"
We could just trace and package those files, but it would be nice to not bloat the package.
We've bandaided this with a tracing configuration like this: afc7d2e#diff-8cf16232f3b7094ea431234555d38ad71e6e62764c3e29a5841e25172aea5049R127-R130
Task:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.