Skip to main content

default.js

当 Next.js 在全页加载后无法恢复 插槽的 活动状态时,default.js 文件用于在 并行路由 内渲染后备。

¥The default.js file is used to render a fallback within Parallel Routes when Next.js cannot recover a slot's active state after a full-page load.

软导航 期间,Next.js 跟踪每个槽的活动状态(子页面)。但是,对于硬导航(全页加载),Next.js 无法恢复活动状态。在这种情况下,可以为与当前 URL 不匹配的子页面渲染 default.js 文件。

¥During soft navigation, Next.js keeps track of the active state (subpage) for each slot. However, for hard navigations (full-page load), Next.js cannot recover the active state. In this case, a default.js file can be rendered for subpages that don't match the current URL.

考虑以下文件夹结构。@team 插槽有 settings 页,但 @analytics 没有。

¥Consider the following folder structure. The @team slot has a settings page, but @analytics does not.

当导航到 /settings 时,@team 插槽将渲染 settings 页面,同时维护 @analytics 插槽的当前活动页面。

¥When navigating to /settings, the @team slot will render the settings page while maintaining the currently active page for the @analytics slot.

刷新时,Next.js 将为 @analytics 渲染 default.js。如果 default.js 不存在,则会渲染 404

¥On refresh, Next.js will render a default.js for @analytics. If default.js doesn't exist, a 404 is rendered instead.

此外,由于 children 是隐式插槽,因此你还需要创建 default.js 文件,以便在 Next.js 无法恢复父页面的活动状态时渲染 children 的后备。

¥Additionally, since children is an implicit slot, you also need to create a default.js file to render a fallback for children when Next.js cannot recover the active state of the parent page.

参考

¥Reference

params(可选)

¥params (optional)

从根段到插槽的子页面解析为包含 动态路由参数 的对象的 promise。例如:

¥A promise that resolves to an object containing the dynamic route parameters from the root segment down to the slot's subpages. For example:

export default async function Default({
params,
}: {
params: Promise<{ artist: string }>
}) {
const artist = (await params).artist
}
export default async function Default({ params }) {
const artist = (await params).artist
}
示例URLparams
app/[artist]/@sidebar/default.js/zackPromise<{ artist: 'zack' }>
app/[artist]/[album]/@sidebar/default.js/zack/nextPromise<{ artist: 'zack', album: 'next' }>
  • 由于 params prop 是一个 promise。你必须使用 async/await 或 React 的 use 函数来访问值。

    ¥Since the params prop is a promise. You must use async/await or React's use function to access the values.

    • 在版本 14 及更早版本中,params 是一个同步 prop。为了帮助实现向后兼容,你仍然可以在 Next.js 15 中同步访问它,但此行为将来会被弃用。

      ¥In version 14 and earlier, params was a synchronous prop. To help with backwards compatability, you can still access it synchronously in Next.js 15, but this behavior will be deprecated in the future.