Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update data-fetch example to SSG (#11017)
* Update to SSG * Change to getServerSideProps * Fix lint * Update
- Loading branch information
Showing
4 changed files
with
20 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
import React from 'react' | ||
import Link from 'next/link' | ||
import fetch from 'isomorphic-unfetch' | ||
import fetch from 'node-fetch' | ||
|
||
function Index(props) { | ||
function Index({ stars }) { | ||
return ( | ||
<div> | ||
<p>Next.js has {props.stars} ⭐️</p> | ||
<p>Next.js has {stars} ⭐️</p> | ||
<Link href="/preact"> | ||
<a>How about preact?</a> | ||
</Link> | ||
</div> | ||
) | ||
} | ||
|
||
Index.getInitialProps = async () => { | ||
export async function getStaticProps() { | ||
const res = await fetch('https://api.github.com/repos/zeit/next.js') | ||
const json = await res.json() // better use it inside try .. catch | ||
return { stars: json.stargazers_count } | ||
return { | ||
props: { | ||
stars: json.stargazers_count, | ||
}, | ||
} | ||
} | ||
|
||
export default Index |
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 |
---|---|---|
@@ -1,22 +1,26 @@ | ||
import React from 'react' | ||
import Link from 'next/link' | ||
import fetch from 'isomorphic-unfetch' | ||
import fetch from 'node-fetch' | ||
|
||
function Preact(props) { | ||
function Preact({ stars }) { | ||
return ( | ||
<div> | ||
<p>Preact has {props.stars} ⭐</p> | ||
<p>Preact has {stars} ⭐</p> | ||
<Link href="/"> | ||
<a>I bet Next.js has more stars (?)</a> | ||
</Link> | ||
</div> | ||
) | ||
} | ||
|
||
Preact.getInitialProps = async () => { | ||
export async function getStaticProps() { | ||
const res = await fetch('https://api.github.com/repos/developit/preact') | ||
const json = await res.json() // better use it inside try .. catch | ||
return { stars: json.stargazers_count } | ||
return { | ||
props: { | ||
stars: json.stargazers_count, | ||
}, | ||
} | ||
} | ||
|
||
export default Preact |