File tree Expand file tree Collapse file tree 3 files changed +71
-0
lines changed
examples/app-pages-router
packages/tests-e2e/tests/appPagesRouter Expand file tree Collapse file tree 3 files changed +71
-0
lines changed File renamed without changes.
Original file line number Diff line number Diff line change 1+ import Home from "@/app/home" ;
2+ import type {
3+ GetStaticPathsResult ,
4+ GetStaticPropsContext ,
5+ InferGetStaticPropsType ,
6+ } from "next" ;
7+
8+ const validRootPages = [ "conico974" , "kheuzy" , "sommeeer" ] ;
9+
10+ export async function getStaticPaths ( ) : Promise < GetStaticPathsResult > {
11+ const rootPaths = validRootPages . map ( ( page ) => ( {
12+ params : { page : [ page ] } ,
13+ } ) ) ;
14+
15+ const paths = [ { params : { page : [ ] } } , ...rootPaths ] ;
16+
17+ return {
18+ paths,
19+ fallback : false ,
20+ } ;
21+ }
22+
23+ export async function getStaticProps ( context : GetStaticPropsContext ) {
24+ const page = ( context . params ?. page as string [ ] ) || [ ] ;
25+
26+ if ( page . length === 0 ) {
27+ return {
28+ props : {
29+ subpage : [ ] ,
30+ pageType : "home" ,
31+ } ,
32+ } ;
33+ }
34+ if ( page . length === 1 && validRootPages . includes ( page [ 0 ] ) ) {
35+ return {
36+ props : {
37+ subpage : page ,
38+ pageType : "root" ,
39+ } ,
40+ } ;
41+ }
42+
43+ return { notFound : true } ;
44+ }
45+
46+ export default function Page ( {
47+ subpage,
48+ pageType,
49+ } : InferGetStaticPropsType < typeof getStaticProps > ) {
50+ if ( subpage . length === 0 && pageType === "home" ) {
51+ return < Home /> ;
52+ }
53+ return (
54+ < div >
55+ < h1 > { pageType === "home" ? "Homepage" : `Root page: ${ subpage } ` } </ h1 >
56+ < p > Path: { subpage . join ( "/" ) } </ p >
57+ </ div >
58+ ) ;
59+ }
Original file line number Diff line number Diff line change 1+ import { test } from "@playwright/test" ;
2+
3+ // Going to `/`, `/conico974`, `/kheuzy` and `/sommeeer` should be catched by our `[[...page]]` route.
4+ test ( "Optional Catch all route in root should work" , async ( { page } ) => {
5+ await page . goto ( "/" ) ;
6+ await page . locator ( "h1" ) . getByText ( "App Router" ) . isVisible ( ) ;
7+ await page . locator ( "h1" ) . getByText ( "Pages Router" ) . isVisible ( ) ;
8+
9+ await page . goto ( "/conico974" ) ;
10+ const pElement = page . getByText ( "Path: conico974" , { exact : true } ) ;
11+ await pElement . isVisible ( ) ;
12+ } ) ;
You can’t perform that action at this time.
0 commit comments