NNext.js 中文网 GitHub

devIndicators

经过优化的页面会包含一个指示器,用于提示该页面是否进行了静态优化。你可以在此处选择退出。

devIndicators 允许你配置屏幕指示器,该指示器会在开发期间提供有关你当前所查看路由的上下文信息。

打开 next.config.ts 并设置 position 以选择指示器的显示位置。默认值为 bottom-left。

next.config.ts
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
  devIndicators: {
    position: 'bottom-right', // 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right'
  },
}

export default nextConfig

要完全隐藏指示器,请将 devIndicators 设置为 false。Next.js 仍会显示遇到的任何编译或运行时错误。

next.config.ts
const nextConfig: NextConfig = {
  devIndicators: false,
}

export default nextConfig

故障排除#

指示器未将路由标记为静态#

如果你期望某个路由是静态的,但指示器将其标记为动态,那么很可能是该路由选择退出了预渲染。

你可以通过使用 next build --debug 构建应用并查看终端输出,来确认某个路由是预渲染还是动态渲染。静态(或预渲染)路由会显示 ○ 符号,而动态路由会显示 ƒ 符号。例如:

Build Output
Route (app)
┌ ○ /_not-found
└ ƒ /products/[id]

○  (Static)   预渲染为静态内容
ƒ  (Dynamic)  按需进行服务端渲染

路由选择退出预渲染的原因有两个:

检查你的路由是否存在上述任一情况,如果无法静态渲染该路由,请考虑使用 loading.js 或 <Suspense /> 来利用流式渲染。

当从页面导出 getServerSideProps 或 getInitialProps 时,该路由会被标记为动态。

版本历史#

版本变更
v16.0.0appIsrStatus、buildActivity 和 buildActivityPosition 选项已移除。
v15.2.0改进了屏幕指示器,新增 position 选项。appIsrStatus、buildActivity 和 buildActivityPosition 选项已弃用。
v15.0.0新增静态屏幕指示器,带有 appIsrStatus 选项。
Next.js 中文网