Next.js 12.1 — On-demand ISR, expanded SWC, next/jest, and self-hosting improvements
Key Points
- On-demand ISR (Beta) for instant page revalidation
- Expanded SWC transforms + RC minifier (7x faster)
- next/jest: zero-config Jest using SWC
Summary
Next.js 12.1 (2022-02-17) introduces on-demand Incremental Static Regeneration (Beta), expanded SWC transforms and SWC-based minification (RC), a zero-configuration Jest plugin (next/jest), faster image optimization (stale-while-revalidate), and self-hosting improvements that produce much smaller Docker images. It also includes early React 18 / Server Components support (Alpha) and other developer ergonomics and performance improvements. Update with npm i next@latest.
Key Points
-
On-demand ISR (Beta)
- New API: call
res.unstable_revalidate('/path-to-revalidate')from an API route to purge and re-generate a specificgetStaticPropspage on-demand. - Example pattern: protect the revalidation endpoint with a secret token, call
unstable_revalidate();getStaticPropsdoes not needrevalidateset to use on-demand. - On Vercel, on-demand revalidation propagates globally in ~300ms.
- New API: call
-
Expanded SWC support and faster minification (RC)
- SWC transforms added:
styled-components,importSource,legacy-decorators,relay,remove-react-properties,remove-console. - SWC minifier is Release Candidate and reported ~7x faster than Terser. Opt in via
module.exports = { swcMinify: true }.
- SWC transforms added:
-
Zero-configuration Jest (
next/jest)- Automatically configures Jest using the Next.js compiler (SWC): auto-mocks styles/images, loads
.env, ignores.nextandnode_modulestransforms, and respectsnext.config.jsflags.
- Automatically configures Jest using the Next.js compiler (SWC): auto-mocks styles/images, loads
-
Self-hosting / Docker
- Enable
experimental.outputStandalone: trueinnext.config.jsto produce.next/standalonefor smaller production bundles (~80% smaller Docker images) and deploy withoutnode_modules.
- Enable
-
Image optimization
- Image cache now supports stale-while-revalidate (serve stale image and revalidate in background) for better performance.
-
React 18, Server Components, and Edge runtime (Alpha)
- Improvements for Server Components, SSR streaming,
useIdsupport, partial hydration and ability to choose Node.js or Edge runtime per page (experimental).
- Improvements for Server Components, SSR streaming,
-
Additional practical notes
- Many developer-experience fixes: Fast Refresh speedups, ESLint 8 support, styled-jsx 5.0, various accessibility and routing bug fixes.
Actionable items for engineers
- Upgrade:
npm i next@latest. - To use on-demand ISR: implement a secure API route that calls
res.unstable_revalidate('/path'). - To try faster minification: set
swcMinify: trueinnext.config.js. - To reduce self-hosted image size: set
experimental.outputStandalone: trueand use.next/standalonein your Docker image. - Evaluate
next/jestto simplify Jest setup and get SWC-based transforms.