Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit e3ce87f

Browse files
committed
Merge branch 'incremental_ssg' (#37)
Merge branch 'incremental_ssg' of git://github.com/joostmeijles/next-on-netlify into joostmeijles-incremental_ssg
2 parents be4a95e + 987bc31 commit e3ce87f

File tree

3 files changed

+106
-10
lines changed

3 files changed

+106
-10
lines changed

lib/allNextJsPages.js

+30-10
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,42 @@ const getAllPages = () => {
6060
pages.push(new Page({ route, type, filePath, alternativeRoutes }));
6161
});
6262

63+
const renderedDynamicSsgPages = {};
64+
6365
// Parse SSG pages
64-
Object.entries(staticSsgPages).forEach(([route, { dataRoute }]) => {
65-
pages.push(
66-
new Page({
67-
route,
68-
type: "ssg",
69-
dataRoute,
70-
alternativeRoutes: route === "/" ? ["/index"] : [],
71-
})
72-
);
66+
Object.entries(staticSsgPages).forEach(([route, { srcRoute, dataRoute, initialRevalidateSeconds }]) => {
67+
if (initialRevalidateSeconds && initialRevalidateSeconds != false) {
68+
// Use SSR for SSG pages with revalidate
69+
debugger;
70+
if (renderedDynamicSsgPages[srcRoute])
71+
return;
72+
73+
if (srcRoute) {
74+
const dynamicPage = dynamicSsgPages[srcRoute];
75+
if (dynamicPage) {
76+
dataRoute = dynamicPage.dataRoute;
77+
route = srcRoute;
78+
renderedDynamicSsgPages[route] = true;
79+
}
80+
}
81+
const filePath = join("pages", `${route}.js`);
82+
pages.push(new Page({ route, type: "ssr", filePath, alternativeRoutes: [dataRoute] }));
83+
} else {
84+
pages.push(
85+
new Page({
86+
route,
87+
type: "ssg",
88+
dataRoute,
89+
alternativeRoutes: route === "/" ? ["/index"] : [],
90+
})
91+
);
92+
}
7393
});
7494
Object.entries(dynamicSsgPages).forEach(
7595
([route, { dataRoute, fallback }]) => {
7696
// Ignore pages without fallback, these are already handled by the
7797
// static SSG page block above
78-
if (fallback === false) return;
98+
if (fallback === false || renderedDynamicSsgPages[route]) return;
7999

80100
const filePath = join("pages", `${route}.js`);
81101
pages.push(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Link from "next/link";
2+
3+
const Show = ({ show }) => (
4+
<div>
5+
<p>This page uses getStaticProps() to pre-fetch a TV show.</p>
6+
7+
<hr />
8+
9+
<h1>Show #{show.id}</h1>
10+
<p>{show.name}</p>
11+
12+
<hr />
13+
14+
<Link href="/">
15+
<a>Go back home</a>
16+
</Link>
17+
</div>
18+
);
19+
20+
export async function getStaticProps(context) {
21+
const res = await fetch(`https://api.tvmaze.com/shows/71`);
22+
const data = await res.json();
23+
24+
return {
25+
props: {
26+
show: data,
27+
},
28+
revalidate: 1,
29+
};
30+
}
31+
32+
export default Show;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Link from "next/link";
2+
3+
const Show = ({ show }) => (
4+
<div>
5+
<p>This page uses getStaticProps() to pre-fetch a TV show.</p>
6+
7+
<hr />
8+
9+
<h1>Show #{show.id}</h1>
10+
<p>{show.name}</p>
11+
12+
<hr />
13+
14+
<Link href="/">
15+
<a>Go back home</a>
16+
</Link>
17+
</div>
18+
);
19+
20+
export async function getStaticPaths() {
21+
// Set the paths we want to pre-render
22+
const paths = [{ params: { id: "1" } }, { params: { id: "2" } }];
23+
24+
// We'll pre-render only these paths at build time.
25+
// { fallback: false } means other routes should 404.
26+
return { paths, fallback: false };
27+
}
28+
29+
export async function getStaticProps({ params }) {
30+
// The ID to render
31+
const { id } = params;
32+
33+
const res = await fetch(`https://api.tvmaze.com/shows/${id}`);
34+
const data = await res.json();
35+
36+
return {
37+
props: {
38+
show: data,
39+
},
40+
revalidate: 1
41+
};
42+
}
43+
44+
export default Show;

0 commit comments

Comments
 (0)