Next.js 12: Middleware, ECMAScript modules, and lessening use of Node.js

Interview Next.js 12 is out with a range of new features including built-in middleware and support for ECMAScript (ES) modules.

"Next.js 12 reimagines the infrastructure of how we build for the web," says Guillermo Rauch, CEO of Vercel, the company maintaining the Next.js MIT-licensed open-source project, and owner of the copyright. Rauch is not one to understate his case, but there is plenty new in this update, launched at the virtual Next.js Conf yesterday.

"It's on two levels," Rauch tells The Register. "One is developer experience, and the other is the global execution model for compute."

The basis for these claims is, first, improvements to the Next.js tools including a new Rust compiler, and second, the addition of middleware to the framework, which Vercel hopes developers will run on the company's Edge Functions (now in beta), a content delivery network.

Next.js is based on React with the addition of options for both server-side rendering with Node.js and static site generation. Here is what is new in version 12 - though much is still in preview:

Why the new compiler? "It's primarily motivated by the pace at which the size of front-end codebases have been growing," says Rauch. "It's not unusual to hear of companies with hundreds of Next.js developers with codebases with tens of thousands of components, if not hundreds of thousands of components."

When Next.js was first developed, "the syntax of JavaScript was changing rapidly, TypeScript wasn't the de-facto standard that it is today. So a lot of the tools were written in JavaScript itself. Now things are becoming more stable, the React JSX syntax is also a de-facto standard, so we're at the point where it makes sense to take advantage of native code. It makes some of the processes developers rely on every day 100 times faster."

Next.js Middleware does not run on Node but more directly on the V8 JavaScript engine itself (which is also used by Node). "Node as the primitive for the cloud is too bulky. In the context of serverless it's created the cold start problem, so by simplifying the code that developers can use, both on the edge side and on the dev side, we can get a lot of performance and security wins, by constraining the API surface."

Rauch is referring to the Edge Runtime used by Middleware, which does not support Node.js APIs. Node modules can be used "as long as they implement ES Modules and do not use any native Node.js APIs," the docs say.

According to Rauch, developers can "eliminate entire categories of issues" by not using Node.js for these serverless functions. "Node has served us well until now, but it's starting to show its age when it comes to the performance needs of today."

The potential uses for Middleware are extensive, including authentication, bot protection, feature flags and A/B testing, server side analytics, logging, and any case where redirects and rewrites are required. At Next.js Conf an example showed how using a region-specific database server could improve latency.

Rauch observes that JAMStack, using static websites with dynamic data from microservices, has pros and cons. "The good part was the focus on pre-rendering. The bad part is that it was so static that sometimes we couldn't pre-render anything.

"What we're seeing now, especially with the introduction of React server components that we're announcing in beta form, we can move the vast majority of the code back to the server, but instead of that server being monolithic at a particular place in the world, we're making the code globally distributed. With that comes greater performance, and no single point of failure."

How is this different from just using a CDN with a conventional web application? "When CDNs looked at speeding up, their hammer was the cache," Rauch tells us. "They go to the origin, take the page, cache it. At that point they've already made it too static, especially for what we're seeing in media, ecommerce... we're seeing that you want to personalize everything, from the language to the currency to variations of components. In that world, the top-level cache of the legacy CDN vendors doesn't work any more."

Rauch also believes that the web-centric development model of Next.js is a benefit. "There's nothing about Next.js that is not native to a web browser," he says. "When you look at designers, how they work today, they just open the web browser and point it to Figma. We asked ourselves, can we do the same, open the browser, point it to a Next.js project, and run the entire compiler and machinery inside the browser?"

This idea ended up as Next.js Live, a web-based development platform. "In our private beta we've been able to load production-grade applications directly in the web browser," Rauch says. The concept is similar to GitHub Codespaces or Gitpod, except that with Next.js Live everything is in the browser, there is no container back-end.

"Next.js is fundamentally a front-end framework," says Rauch. "The Next.js runtime is fundamentally serverless, so we don't need all that additional machinery to run and render a project. The web was designed from the outset to allow both reading and editing. We want to bring more people into the world of contributing to the web."

The editor in Next.js Live is based on Monaco, as in Microsoft's Visual Studio Code project.

Next.js Live uses WebAssembly in the browser for the compiler. "WebAssembly is now the new universal way of distributing applications," Rauch says. "We can take code written in Rust and make it entirely universal." However, he believes that it is "early days for WebAssembly in production" so this is currently only used for development - though it is something the team is looking at for the future.

Next.js 12 also improves the performance and stability of Hot Module Reload (HMR), Rauch tells us. This is where "you type, you save your file, and you see your change instantly without refreshing the web browser... It used to be the case that developers had to second guess and sometimes just hit refresh out of muscle memory as they wouldn't fully trust the technology. We've refined it a lot... we're seeing up to five times faster repaints on the screen, from what you're typing."

The technology behind the faster refresh is in part a benefit of ES modules because "we can become smart about only giving the web browser the specific change. We only load the part of the page that you are working on."

Why not just use React rather than Next.js? "I like to think of [React] as the engine," says Rauch. "What this engine does is on the one hand power rendering, and on the other hand supply the primitive abstractions that the developers are going to use. The component model is provided by React and the rendering engine is provided by React. But with Next.js folks are building websites and applications, and that's the layer of abstraction that Next.js introduced on top. We've raised the abstraction from component to page."

Is TypeScript taking over from JavaScript? "The ecosystem is maturing. The everyday developer wants fewer bugs, a higher level of correctness, more and better documentation, types and not just code. That has driven TypeScript to be the default for Next.js projects, especially for teams in companies." ®

Search
About Us
Website HardCracked provides softwares, patches, cracks and keygens. If you have software or keygens to share, feel free to submit it to us here. Also you may contact us if you have software that needs to be removed from our website. Thanks for use our service!
IT News
Nov 27
How Kubernetes simplifies the operation of fintech apps

From data security to automatic, safe scaling, we've got it covered

Nov 27
Nov 26
Nov 26
Bad news for Tencent: Chinese companies steer employees away from Weixin or WeChat

Middle Kingdom's internet giant: It's a switch to enterprise apps. Try ours?

Nov 25
Alpine Linux 3.15 bids a fond farewell to MIPS64 support

LTS Linux kernel - check. Once proud RISC contender? Nope

Nov 25
Robotaxis freed to charge across 60km2 of Beijing

Baidu's Apollo tech exits testing phase, so punters must now pay the machine for a ride. Would you?