app router
Next 13 ๋ฒ์ ์ ์ถ๊ฐ๋ App Router ๋ฐฉ์์ ๊ธฐ์กด์ ์ฌ์ฉํ๋ Page Router ๋ฐฉ์๊ณผ ์ด๋ป๊ฒ ๋ค๋ฅธ์ง ์์๋ณด์.
ํด๋ ๊ตฌ์กฐ
Page Router์์ page ํด๋์์์ ๋ผ์ฐํฐ๋ฅผ ๋ง๋ค์๋ค๋ฉด App Router ๋ฐฉ์์์๋ app ํด๋์์์ ๋ผ์ฐํฐ๋ฅผ ์์ฑํ๋ค.
-app
-dashboard
-settings
-page.tsx
root ํด๋์ธ app
ํด๋์์ ์์ฑ๋ ํด๋๋ ํ๋ก์ ํธ์ ๊ฒฝ๋ก๊ฐ ๋๋ค. ์์ ์๋ ํด๋ ๊ตฌ์กฐ์ url ๊ฒฝ๋ก๋ ๋ค์๊ณผ ๊ฐ๋ค. domain.com/dashboard/settings
App Router์์๋ ํน๋ณํ ๊ธฐ๋ฅ์ ํ๋ ๊ธฐ๋ณธ ํ์ผ๋ค์ ์ ๊ณตํ๋ค. layout
, page
, loading
, not-found
, error
ํด๋น ๋ด์ฉ๋ ์์ธํ ์ ๋ฆฌํ ์์ ์ด๋ค.
๋์ ๋ผ์ฐํ
๋์ ๋ผ์ฐํ ํด๋๋ฅผ ๋ง๋๋ ๋ฐฉ์์ Page Router์ ๋์ผํ๋ค. ๋๊ดํธ([])๋ก ๋ง๋ ํด๋๋ Next๊ฐ ๋์ ๋ผ์ฐํ ํด๋๋ก ์ธ์ํ๋ค.
-app
-product
-[id]
ํด๋น ํด๋ ๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์ url๋ก ์ ๊ทผํ ์ ์๋ค. domain.com/product/1
product ์ธ๊ทธ๋จผํธ ๋ค์๋ ์ด๋ค ๊ฐ์ด ์๋ url ๊ฒฝ๋ก๋ก ์ธ์๋๋ค. ๋์ ์ธ๊ทธ๋จผํธ๋ page
, layout
๋ฑ์์ props๋ก ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
// routing : app/product/[id]
// url : domain.com/product/1
export default function ProductDetailPage(props) {
console.log("props:", props)
return (
<ProductDetailComponent>
)
}
// ์ถ๋ ฅ : { params: { id: '1' }, searchParams: {} }
๊ทธ๋ฃน ๋ผ์ฐํฐ
app ํด๋์์ ์์ฑ๋ ํด๋๋ url ๊ฒฝ๋ก๋ก ์ธ์๋์ง๋ง ๋ผ์ฐํฐ๋ฅผ ๊ทธ๋ฃน์ผ๋ก ๋ฌถ์ด์ url ๊ฒฝ๋ก์ ํฌํจ๋์ง ์๊ฒ ํ ์ ์๋ค. Next ๋ฌธ์์์๋ ๊ทธ๋ฃน ๋ผ์ฐํฐ๋ฅผ ๋ค์๊ณผ ๊ฐ์ ์ํฉ์์ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์๋ค๊ณ ์๊ฐํ๋ค.
Organizing routes into groups e.g. by site section, intent, or team.
Enabling nested layouts in the same route segment level:
Creating multiple nested layouts in the same segment, including multiple root layouts
Adding a layout to a subset of routes in a common segment
๊ฐ์ ๋ ๋ฒจ์ ์ธ๊ทธ๋จผํธ๊ฐ ๋๊ฐ์ layout์ ๊ณต์ ํ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์์ ๊ฒ๊ฐ๋ค. (layout์ ๋ํด์๋ ๋ ์์ธํ ๋ค๋ฃฐ ์์ )
๊ดํธ๋ก ๋ง๋ ํด๋์์ ์๋ ํด๋๋ ๊ทธ๋ฃน ๋ผ์ฐํ ์ผ๋ก ์ธ์๋๋ค.
-app
-(shop)
-product
-page.js
-account
-page.js
product
์ account
๋ ๊ฐ์ ๋ ๋ฒจ์ ์ธ๊ทธ๋จผํธ๋ก ์ธ์๋๊ณ , shop์ url ๊ฒฝ๋ก์ ์๋ฌด ์ํฅ๋ ์ฃผ์ง ๋ชปํ๋ค. ๊ฐ์ domain.com/product
, domain.com/account
์ผ๋ก ๊ฒฝ๋ก๊ฐ ์ค์ ๋๋ค.
-app
-(shop)
-layout.js // product
-product
-page.js
-account
-page.js
product
์ account
๋ (shop)
์์ ์๋ layout.js๋ฅผ ๊ณต์ ํ ์ ์๋ค.
Page
Page Route ๋ฐฉ์์๋ url ๊ฒฝ๋ก๊ฐ ๋ ํด๋์์ index ํ์ผ์ ๋ง๋ค์ด ์คฌ๋ค๋ฉด App Router์์๋ page ํ์ผ์ ๋ง๋ค์ด ์ค๋ค.
-app
-product
-page.js
// `app/product/page.js`
export default function ProductListPage() {
return <h1>Product List page </h1>
}
Layout
Layout์ ๊ฐ์ route ์์์ ๊ณต์ ํ ์ ์๋ UI์ด๋ค. ์ฌ๋ฌ page๊ฐ ๊ณตํต์ผ๋ก ์ฌ์ฉํ๋ UI๊ฐ ์๋ค๋ฉด layout.js ํ์ผ์์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
export default function RootLayout({children}:{children: React.ReactNode}) {
return (
<html lang="en">
<body>
<main>{children}</main>
</body>
</html>
)
}
Next๋ layout์ ๋จผ์ ๋๋๋ง ํ ํ, ํด๋น ๊ฒฝ๋ก์ ๋ง๋ ์ปดํฌ๋ํธ๋ฅผ ๋๋๋งํ๋ค. /product ๊ฒฝ๋ก๋ก ์ด๋ํ๋ฉด ํ๋ฉด์ ๋ค์๊ณผ ๊ฐ์ด ๋๋๋ง๋๋ค.
<RootLayout>
<ProductList />
</RootLayout>
layout์ ์ค์ฒฉ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค. route ํด๋์์ layout ํ์ผ์ ์์ฑํ๋ฉด ๊ฐ์ ๊ฒฝ๋ก์ ์๋ ํ์ด์ง๋ค์ ๊ฐ์ layout์ ๊ณต์ ํ๊ฒ ๋๋ค. ๋ค์ ํด๋๊ตฌ์กฐ์์ product/about ๊ฒฝ๋ก๋ก ์ด๋ํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋๋๋ง๋๋ค.
-app
-page.js
-layout.js
-product
-page.js
-layout.js
-about
-page.js
<RootLayout>
<ProductLayout>
<Product>
<About />
</Product>
</ProductLayout>
</RootLayout>
Last updated