From 987bc3199c7790b5e6e0e2927cdd7cbbbb0a8203 Mon Sep 17 00:00:00 2001 From: Joost Date: Wed, 9 Sep 2020 22:56:05 +0200 Subject: [PATCH] Use SSR for incremental SSG --- lib/allNextJsPages.js | 40 ++++++++++++----- .../fixtures/pages/getStaticProps/dynamic.js | 32 ++++++++++++++ .../fixtures/pages/getStaticProps/ssg/[id].js | 44 +++++++++++++++++++ 3 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 tests/fixtures/pages/getStaticProps/dynamic.js create mode 100644 tests/fixtures/pages/getStaticProps/ssg/[id].js diff --git a/lib/allNextJsPages.js b/lib/allNextJsPages.js index 4f57d35..30a00df 100644 --- a/lib/allNextJsPages.js +++ b/lib/allNextJsPages.js @@ -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( diff --git a/tests/fixtures/pages/getStaticProps/dynamic.js b/tests/fixtures/pages/getStaticProps/dynamic.js new file mode 100644 index 0000000..dc26b24 --- /dev/null +++ b/tests/fixtures/pages/getStaticProps/dynamic.js @@ -0,0 +1,32 @@ +import Link from "next/link"; + +const Show = ({ show }) => ( +
+

This page uses getStaticProps() to pre-fetch a TV show.

+ +
+ +

Show #{show.id}

+

{show.name}

+ +
+ + + Go back home + +
+); + +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; diff --git a/tests/fixtures/pages/getStaticProps/ssg/[id].js b/tests/fixtures/pages/getStaticProps/ssg/[id].js new file mode 100644 index 0000000..9863b43 --- /dev/null +++ b/tests/fixtures/pages/getStaticProps/ssg/[id].js @@ -0,0 +1,44 @@ +import Link from "next/link"; + +const Show = ({ show }) => ( +
+

This page uses getStaticProps() to pre-fetch a TV show.

+ +
+ +

Show #{show.id}

+

{show.name}

+ +
+ + + Go back home + +
+); + +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;