Blog - Next - Js 12 - Next - Js
Blog - Next - Js 12 - Next - Js
js in seconds →
Next.js 12
Tuesday, October 26th 2021 (7 days ago)
Results from using the new Rust compiler with large Next.js codebases.
- Further speed improvements for large codebases: We've validated the Rust
compiler with some of the largest Next.js codebases in the world.
- Improved observability into performance: Next.js now outputs Fast Refresh timing
in the console for both client and server compilation, including the number of modules
and files compiled.
- Underlying webpack improvements: We've made numerous improvements to
webpack, including optimizing Fast Refresh and making on-demand entries more
reliable.
Compilation using Rust is 17x faster than Babel and enabled by default using Next.js 12,
replacing transforming JavaScript and TypeScript files. This meant we had to port the Babel
transformations in Next.js to Rust, including a brand new CSS parser in Rust used to
implement the `styled-jsx` transform.
The new Rust compiler is backwards compatible. If you have an existing Babel
configuration, you wil automatically be opted out. We have plans to port parsing for popular
libraries like `styled-components`, `emotion`, and `relay` soon. If you're using a custom
Babel setup, please share your configuration.
You can also opt-in to using the Rust compiler for minification. This is 7x faster than
Terser. Minification is opt-in until it’s thoroughly validated as it replaces multi-year
infrastructure.
// next.config.js
module.exports = {
swcMinify: true
On top of hiring DongYoon Kang, the creator of SWC, and Maia Teegarden, a contributor to
Parcel, we are continuing to invest in the Rust ecosystem. If you have experience working
with Rust, please apply to join our team.
For more information, watch our demo from Next.js Conf.
Introducing Middleware
Middleware enables you to use code over configuration. This gives you full flexibility in
Next.js because you can run code before a request is completed. Based on the user's
incoming request, you can modify the response by rewriting, redirecting, adding headers, or
even streaming HTML.
Middleware gives you complete flexibility inside Next.js.
Middleware can be used for anything that shares logic for a set of pages, including:
- Authentication
- Bot protection
- Redirects and rewrites
- Handling unsupported browsers
- Feature flags and A/B tests
- Server-side analytics
- Advanced i18n routing requirements
- Logging
- And more!
Middleware uses a strict runtime that supports standard Web APIs like `fetch`. This works
out of the box using `next start`, as well as on Edge platforms like Vercel, which use Edge
Functions.
To use Middleware in Next.js, you can create a file `pages/_middleware.js`. In this
example, we use the standard Web API Response (MDN):
// pages/_middleware.js
For more information, watch our demo from Next.js Conf and check out the documentation.
`React.lazy`.
We've been working closely with the React team at Facebook to prepare Next.js for React
18 as it moves towards a stable release. We're making these features available to try today
in Next.js 12 under an experimental flag.
Server-Side Streaming
Concurrent features in React 18 include built-in support for server-side Suspense and SSR
streaming support. This allows you to server-render pages using HTTP streaming. This is an
experimental feature in Next.js 12, but once enabled, SSR wil use the same strict runtime as
Middleware.
To enable, use the experimental flag `concurrentFeatures: true`:
// next.config.js
module.exports = {
experimental: {
concurrentFeatures: true
// next.config.js
module.exports = {
experimental: {
concurrentFeatures: true,
serverComponents: true
Next.js now enables you to do data fetching at the component level, all expressed as JSX.
By using React Server components, we can simplify things. Special functions like
`getServerSideProps` or `getStaticProps` are no lo nger needed. This aligns with the
URL Imports
Next.js 12 includes experimental support for importing ES Modules through URLs, no install
or separate build step is required.
URL imports allow you to use any package directly through a URL. This enables Next.js to
process remote HTTP(S) resources exactly like local dependencies.
If a URL import is detected, Next.js wil generate a `next.lock` file to track remote
resources. URL imports are cached locally to ensure you can stil work offline. Next.js
supports both client and server URL imports.
To opt-in, add the allowed URL prefixes inside `next.config.js`:
module.exports = {
experimental: {
urlImports: ['https://cdn.skypack.dev']
Any CDN that serves ES modules wil work, including no-code and design tools like Framer:
- Skypack
- esm.sh
- jsDelivr
- JSPM
- unpkg
For more information, watch our demo from Next.js Conf and check out the documentation.
In Next.js 12, web crawlers (e.g. search bots) wil automatically server-render ISR pages
using `fallback: true`, while stil serving the previous behavior of the fallback state to
non-crawler User-Agents. This prevents crawlers from indexing loading states.
images: {
This list of formats is used to determine the optimized image format on-demand using the
request's `Accept` header. Since AVIF is first, it wil be served if the browser supports AVIF.
If not, WebP wil be served if the browser supports WebP. If neither format is supported, the
original image format wil be served.
Now, we're bringing these improvements directly into the Next.js framework by default, for
all deployment platforms, providing a significantly improved approach over the `target`
option.
Next.js 12 automatically traces which files are needed by each page and API route using
`@vercel/nft`, and outputs these trace results next to the `next build` output, allowing
Breaking Changes
- After making webpack 5 the default in Next.js 11, we've now official y removed
webpack 4. We've worked closely with the community to ensure a smooth transition
to webpack 5.
- `target` in `next.config.js` is no longer needed.
- `next/image` now uses a `span` as the wrapping element instead of a `div`.
- The minimum Node.js version has been bumped from `12.0.0` to `12.22.0`, which is
the first version of Node.js with native ES modules support.
To learn more, check out the upgrade guide.
Community
Five years ago, we released Next.js to the public. We set out to build a zero-configuration
React framework that simplifies your developer experience. Looking back, it's incredible to
see how the community has grown, and what we've been able to ship together. Let's keep
going.
Next.js is the result of the combined work of over 1,800 individual developers, industry
partners like Google and Facebook, and our core team.
This release was brought to you by the contributions of: @ka2n, @housseindjirdeh,
@rojserbest, @lobsterkatie, @thibautsabot, @javivelasco, @sokra, @rishabhpoddar, @kdy1,
@huozhi, @georgegach, @ionut-botizan, @paul-creates, @TimBarley, @kimizuy, @devknoll,
@matamatanot, @christianvuerings, @pgrodrigues, @mohamedbhy, @AlfonzAlfonz, @kara,
@molebox, @angelopoole, @oste, @genetschneider, @jantimon, @kyliau, @mxschmitt,
@PhattOZ, @finn-orsini, @kriswuollett, @harryheman, @GustavoEdinger, @AryanBeezadhur,
@Blevs, @colevscode, @atcastle, @ij k, @velocity23, @jonowu, @timneutkens,
@whitep4nth3r, @micro-chipset, @TyMick, @padmaia, @arthurdenner, @vitorbal, @zNeb,
@jacksonhardaker, @shuding, @kylemh, @Bundy-Mundi, @ctjlewis, @thien-do, @leerob,
@Dev-CasperTheGhost, @janicklas-ralph, @rezathematic, @KonstHardy, @fracture91,
@lorensr, @Sheraff, @HaNdTriX, @emilio, @oluan, @ddzieduch, @colinclerk, @x4th,
@volcareso, @oiva, @sinchang, @scottrepreneur, @smakosh, @catnose99, @adrienharnay,
@donsn, @andersonleite, @msp5382, @tim-hanssen, @appsplash99, @alexvilchis,
@RobEasthope, @royal, @Perry-Olsson, @well-balanced, @mrmckeb, @buraksakalli,
@espipj, @prateekbh, @AleksaC, @eungyeole, and @rgabs
← Back to Blog