Next.js update: Middleware stable, experimental APIs include plugins for Rust-based SWC

Vercel has released version 12.2 of its Next.js React-based framework, with key features now stable  and new experimental APIs.

Next.js is open source and can be self-hosted or deployed to Vercel’s edge network (hosted by AWS). Last October, the team previewed a feature called Middleware, explaining that “Middleware enables you to use code over configuration.”

It added: “This gives you full flexibility in Next.js because you can run code before a request is completed,” reversing the common assumption that configuration is more efficient than code.

Middleware runs on the Edge Runtime, a lightweight runtime based on the V8 engine which uses a cut-down API and only works with ES (ECMAScript) modules, not with native Node.js APIs. Vercel’s chief architect Malte Ubl said on Hacker News that Edge Functions has “startup-time so fast that if you hit a cold start it is still fast enough from a human perception (often <30ms attributable to startup).” The trade-off, he said, is the restricted API set, smaller maximum binary size, lower CPU quota and lower maximum RAM usage.

Middleware is now generally available, and Vercel’s CEO Guillermo Rauch said yesterday that “almost half of our enterprise customers” had made use of the beta.

The GA version of Middleware has breaking changes from the beta. Middleware functions can no longer be nested, but must now be in a single root Middleware, one code file written in either TypeScript or JavaScript. The docs say that this is both faster and cheaper. It remains possible to execute code based on request paths or other conditions. Another change is that Middleware can no longer generate a response body, but instead must use rewrite or redirect to modify an incoming request. There are also changes to the cookie API.

Another feature now declared stable is on-demand incremental static regeneration, a feature added in beta in Next.js 12.1. Incremental means that static page regeneration can be done on a page by page basis, with no need to rebuild an entire site. On demand means that a revalidation API can be called when content changes.

New in this version is Edge API routes, for building APIs using the same Next.js Edge Runtime used by Middleware. Edge API routes are in beta. Another new feature, designated as experimental, is an option to use Edge server rendering with the Edge Runtime instead of Node.js for server-side rendering of pages. The documentation states that the Edge Runtime has “lower latency as well as better scalability.”

SWC is a tool for compilation and JavaScript or TypeScript bundling, written in Rust, and claimed [https://swc.rs/ ] to be 20 times faster than Babel on a single thread, or 70 times faster on four cores. SWC was developed by DongYoon Kang who was hired by Vercel in August last year. Build performance is critical for Next.js because of the requirement for fast server-side regeneration. New in Next.js 12.2 is experimental support for plugins written in WebAssembly that customize SWC transformation.

A full description of what is new in 12.2 can be found here.

There are echoes here of the work being done by Vercel’s competitor Netlify and by the Deno team, building an alternative to Node.js based on TypeScript and web standard ES modules.

It is a modern serverless approach to web development: push the code, let the provider worry about where it runs, and use web standard APIs. Scalability is easier and there are security benefits, but as noted by Hacker News commenters, not all the code is open source and the hosting can be expensive.