export default function Layout({ children }) {
return (
<>
<Sidebar />
{children}
<Modal />
</>
)
}
export default function Layout({ children, sidebar, modal }) {
return (
<>
{sidebar}
{children}
{modal}
</>
)
}
後者では app/@sidebar/page.tsx や app/@modal/page.tsx が独立してレンダリングされ、URLに応じて切り替わります
以下は @sidebar と @modal スロットを使い、URL によって切り替える最小構成の例です。
app/
layout.tsx
page.tsx
@sidebar/
layout.tsx
page.tsx
default.tsx
@modal/
default.tsx
photo/
[id]/
page.tsx
dashboard/
layout.tsx
page.tsx
photo/
[id]/
page.tsx
export default function Layout({ children, sidebar, modal }: {
children: React.ReactNode
sidebar: React.ReactNode
modal: React.ReactNode
}) {
return (
<>
{sidebar}
<main>{children}</main>
{modal}
</>
)
}
export default function HomePage() {
return <p>Home</p>
}
export default function SidebarLayout({ children }: { children: React.ReactNode }) {
return <aside>{children}</aside>
}
export default function SidebarPage() {
return <nav>Sidebar Nav</nav>
}
export default function SidebarDefault() {
return <nav>Sidebar Default</nav>
}
export default function ModalDefault() {
return null
}
export default function PhotoModalPage({ params }: { params: { id: string } }) {
return <div>Photo Modal: {params.id}</div>
}
export default function DashboardPage() {
return <p>Dashboard</p>
}
export default function PhotoPage({ params }: { params: { id: string } }) {
return <p>Photo Page: {params.id}</p>
}
| URL | children | sidebar | modal |
|---|---|---|---|
/ | Home | Sidebar Nav | null |
/dashboard | Dashboard | Sidebar Nav | null |
/photo/123 | Photo Page: 123 | Sidebar Nav | null |
/dashboard(Intercepted) | Dashboard | Sidebar Nav | Photo Modal: 123(Intercepted) |
app/dashboard/@modal/(..)photo/[id]/page.tsx を配置すると、/dashboard で /photo/123 をインターセプトしてモーダル表示できます 1 。
default.tsx はスロットに対応するページがないときのフォールバックです 2 。sidebar prop を受け取る Layout が実装されています 3 。File: docs/01-app/03-api-reference/03-file-conventions/intercepting-routes.mdx (L58-66)
### Modals
Intercepting Routes can be used together with [Parallel Routes](/docs/app/api-reference/file-conventions/parallel-routes) to create modals. This allows you to solve common challenges when building modals, such as:
- Making the modal content **shareable through a URL**.
- **Preserving context** when the page is refreshed, instead of closing the modal.
- **Closing the modal on backwards navigation** rather than going to the previous route.
- **Reopening the modal on forwards navigation**.
File: test/e2e/app-dir/parallel-routes-and-interception/app/parallel-side-bar/layout.tsx (L4-9)
export default function Layout({
children,
sidebar,
}: {
children: React.ReactNode
sidebar: React.ReactNode