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

Proposal to use SSR for incremental static regeneration #37

Merged
merged 1 commit into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions lib/allNextJsPages.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,42 @@ const getAllPages = () => {
pages.push(new Page({ route, type, filePath, alternativeRoutes }));
});

const renderedDynamicSsgPages = {};

// Parse SSG pages
Object.entries(staticSsgPages).forEach(([route, { dataRoute }]) => {
pages.push(
new Page({
route,
type: "ssg",
dataRoute,
alternativeRoutes: route === "/" ? ["/index"] : [],
})
);
Object.entries(staticSsgPages).forEach(([route, { srcRoute, dataRoute, initialRevalidateSeconds }]) => {
if (initialRevalidateSeconds && initialRevalidateSeconds != false) {
// Use SSR for SSG pages with revalidate
debugger;
if (renderedDynamicSsgPages[srcRoute])
return;

if (srcRoute) {
const dynamicPage = dynamicSsgPages[srcRoute];
if (dynamicPage) {
dataRoute = dynamicPage.dataRoute;
route = srcRoute;
renderedDynamicSsgPages[route] = true;
}
}
const filePath = join("pages", `${route}.js`);
pages.push(new Page({ route, type: "ssr", filePath, alternativeRoutes: [dataRoute] }));
} else {
pages.push(
new Page({
route,
type: "ssg",
dataRoute,
alternativeRoutes: route === "/" ? ["/index"] : [],
})
);
}
});
Object.entries(dynamicSsgPages).forEach(
([route, { dataRoute, fallback }]) => {
// Ignore pages without fallback, these are already handled by the
// static SSG page block above
if (fallback === false) return;
if (fallback === false || renderedDynamicSsgPages[route]) return;

const filePath = join("pages", `${route}.js`);
pages.push(
Expand Down
32 changes: 32 additions & 0 deletions tests/fixtures/pages/getStaticProps/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Link from "next/link";

const Show = ({ show }) => (
<div>
<p>This page uses getStaticProps() to pre-fetch a TV show.</p>

<hr />

<h1>Show #{show.id}</h1>
<p>{show.name}</p>

<hr />

<Link href="/">
<a>Go back home</a>
</Link>
</div>
);

export async function getStaticProps(context) {
const res = await fetch(`https://api.tvmaze.com/shows/71`);
const data = await res.json();

return {
props: {
show: data,
},
revalidate: 1,
};
}

export default Show;
44 changes: 44 additions & 0 deletions tests/fixtures/pages/getStaticProps/ssg/[id].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Link from "next/link";

const Show = ({ show }) => (
<div>
<p>This page uses getStaticProps() to pre-fetch a TV show.</p>

<hr />

<h1>Show #{show.id}</h1>
<p>{show.name}</p>

<hr />

<Link href="/">
<a>Go back home</a>
</Link>
</div>
);

export async function getStaticPaths() {
// Set the paths we want to pre-render
const paths = [{ params: { id: "1" } }, { params: { id: "2" } }];

// We'll pre-render only these paths at build time.
// { fallback: false } means other routes should 404.
return { paths, fallback: false };
}

export async function getStaticProps({ params }) {
// The ID to render
const { id } = params;

const res = await fetch(`https://api.tvmaze.com/shows/${id}`);
const data = await res.json();

return {
props: {
show: data,
},
revalidate: 1
};
}

export default Show;