Skip to content

Commit cb4fbde

Browse files
committed
add e2e
1 parent 7f48e53 commit cb4fbde

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
});

0 commit comments

Comments
 (0)