This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for dynamically routed SSG pages with fallback
Copy pre-rendered, dynamically routed pages with SSG to Netlify publish directory and copy SSG page JSON data to _next/data/ directory. Create a Netlify Function to handle the paths not defined in the page's getStaticPaths. When requesting a page that has not been pre-rendered, it will be SSRed by this function. The function also returns the page data for paths that have not been pre-defined. See: #7
- Loading branch information
Showing
11 changed files
with
315 additions
and
55 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
cypress/fixtures/pages/getStaticProps/withFallback/[id].js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useRouter } from 'next/router' | ||
import Link from 'next/link' | ||
|
||
const Show = ({ show }) => { | ||
const router = useRouter() | ||
|
||
// This is never shown on Netlify. We just need it for NextJS to be happy, | ||
// because NextJS will render a fallback HTML page. | ||
if (router.isFallback) { | ||
return <div>Loading...</div> | ||
} | ||
|
||
return ( | ||
<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: '3' } }, | ||
{ params: { id: '4' } } | ||
] | ||
|
||
// We'll pre-render these paths at build time. | ||
// { fallback: true } means other routes will be rendered at runtime. | ||
return { paths, fallback: true } | ||
} | ||
|
||
|
||
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 | ||
} | ||
} | ||
} | ||
|
||
export default Show |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.