Next.js 12.1 正式发布,企业级 Web 应用框架
Next.js 12.1 已正式发布。
新特性概览
- 按需使用的 ISR (Beta):支持即时使用
getStaticProps重新验证页面 - 扩展对 SWC 的支持:引入
styled-components和 Relay 等功能 - 引入
next/jest插件:使用 SWC 实现零配置支持 Jest - 通过 SWC (RC) 提供更快的压缩:比 Terser 快 7 倍的压缩速度
- 优化自托管功能:减小 Docker 镜像体积约 80%
- React 18 & Server Components (Alpha):优化稳定性和支持情况
按需使用的 ISR (Beta)
ISR 是 Next.js 9.5 引入的功能,本次的更新提供了按需使用的特性,开发者可按需手动清除特定页面的 Next.js 缓存。
// pages/api/revalidate.js
export default async function handler(req, res) {
// Check for secret to confirm this is a valid request
if (req.query.secret !== process.env.MY_SECRET_TOKEN) {
return res.status(401).json({ message: 'Invalid token' })
}
try {
await res.unstable_revalidate('/path-to-revalidate')
return res.json({ revalidated: true })
} catch (err) {
// If there was an error, Next.js will continue
// to show the last successfully generated page
return res.status(500).send('Error revalidating')
}
}
零配置 Jest 插件
Next.js 现已支持自动配置 Jest,使用基于 Rust 的 Next.js 编译器来转换文件,包括:
- 自动模拟样式表(
.css,.module.css和.scss变体),以及图像导入 - 将
.env加载到process.env - 忽略测试解析和转换的
node_modules - 忽略测试解析的
.next - Loading
next.config.jsfor flags that enable Next.js compiler transforms
优化自托管功能
Next.js 现在支持自动创建standalone文件夹,该文件夹仅复制生产部署所需的文件。这使得自托管 Next.js 应用程序的Docker 映像缩小了约 80% 。