Skip to content

Commit d8c706d

Browse files
committed
feat: routing with dynamic imports for bundling
1 parent 8259629 commit d8c706d

File tree

8 files changed

+83
-11
lines changed

8 files changed

+83
-11
lines changed

examples/next/template-hierarchy/example-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12-
"next": "15.3.3",
12+
"next": "^15.3.4",
1313
"react": "^19.0.0",
1414
"react-dom": "^19.0.0",
1515
"urql": "^4.2.2"

examples/next/template-hierarchy/example-app/src/components/Layout.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ export default function Layout({ children }) {
1010
<TemplateHierarchyInfo template={templateData} uri={uri} />
1111
<header>
1212
<h1>Template Hierarchy Example</h1>
13+
<nav>
14+
<ul>
15+
<li>
16+
<a href="/">Home</a>
17+
</li>
18+
<li>
19+
<a href="/hello-world">Sample Post</a>
20+
</li>
21+
<li>
22+
<a href="/sample-page">Sample Page</a>
23+
</li>
24+
</ul>
25+
</nav>
1326
</header>
1427
<main>{children}</main>
1528
</div>

examples/next/template-hierarchy/example-app/src/lib/templates.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,14 @@ export async function getAvailableTemplates() {
147147
const templates = [];
148148

149149
for (const file of files) {
150+
if (file === "index.js") {
151+
continue; // Skip the index file
152+
}
153+
150154
const slug = file.replace(".js", "");
151155

152156
templates.push({
153-
id: slug,
157+
id: slug === "default" ? "index" : slug,
154158
path: join("/", TEMPLATE_PATH, file),
155159
});
156160
}

examples/next/template-hierarchy/example-app/src/pages/[[...uri]].js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { uriToTemplate } from "@/lib/templateHierarchy";
22
import { RouteDataContext } from "@/lib/context";
3-
import Layout from "@/components/Layout";
3+
import availableTemplates from "@/wp-templates";
4+
5+
export default function Page(props) {
6+
const { templateData } = props;
7+
8+
const PageTemplate = availableTemplates[templateData.template?.id];
49

5-
export default function Page({ params, uri, templateData }) {
610
return (
7-
<RouteDataContext.Provider value={{ params, uri, templateData }}>
8-
<Layout>
9-
<p>
10-
You shouldn't see this page if the template hierarchy is working
11-
correctly.
12-
</p>
13-
</Layout>
11+
<RouteDataContext.Provider value={props}>
12+
<PageTemplate {...props} />
1413
</RouteDataContext.Provider>
1514
);
1615
}
@@ -22,6 +21,15 @@ export async function getServerSideProps({ params }) {
2221

2322
const templateData = await uriToTemplate({ uri });
2423

24+
if (
25+
!templateData?.template?.id ||
26+
templateData?.template?.id === "404 Not Found"
27+
) {
28+
return {
29+
notFound: true,
30+
};
31+
}
32+
2533
return {
2634
props: {
2735
params,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Layout from "@/components/Layout";
2+
3+
export default function DefaultTemplate() {
4+
return (
5+
<Layout>
6+
<h2>Default Template</h2>
7+
<p>
8+
This is the default template for the template hierarchy example app.
9+
</p>
10+
</Layout>
11+
);
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Layout from "@/components/Layout";
2+
3+
export default function Home() {
4+
return (
5+
<Layout>
6+
<h2>Home Template</h2>
7+
<p>This is the home page of the template hierarchy example app.</p>
8+
</Layout>
9+
);
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import dynamic from "next/dynamic";
2+
3+
const home = dynamic(() => import("./home.js"), {
4+
loading: () => <p>Loading Home Template...</p>,
5+
});
6+
7+
const index = dynamic(() => import("./default.js"), {
8+
loading: () => <p>Loading Index Template...</p>,
9+
});
10+
11+
const single = dynamic(() => import("./single.js"), {
12+
loading: () => <p>Loading Single Template...</p>,
13+
});
14+
15+
export default { home, index, single };
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Layout from "@/components/Layout";
2+
3+
export default function SingleTemplate() {
4+
return (
5+
<Layout>
6+
<h2>Single Template</h2>
7+
<p>This is the single template for the template hierarchy example app.</p>
8+
</Layout>
9+
);
10+
}

0 commit comments

Comments
 (0)