Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples to ssg #11023

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/data-fetch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Next.js was conceived to make it easy to create universal apps. That's why fetching data
on the server and the client when necessary is so easy with Next.

Using `getInitialProps` fetches data on the server for SSR and then on the client when the component is re-mounted (not on the first paint).
Using `getStaticProps` fetches data on the server for SSG.

## Deploy your own

Expand Down
4 changes: 2 additions & 2 deletions examples/data-fetch/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function Index(props) {
)
}

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
4 changes: 2 additions & 2 deletions examples/data-fetch/pages/preact.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function Preact(props) {
)
}

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
29 changes: 13 additions & 16 deletions examples/ssr-caching/pages/blog.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import React from 'react'

export default class extends React.Component {
static getInitialProps({ query: { id } }) {
return { id }
}

render() {
return (
<div>
<h1>My {this.props.id} blog post</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
)
}
export function getServerSideProps({ query: { id } }) {
return { props: { id } }
}
export default function(props) {
return (
<div>
<h1>My {props.id} blog post</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
)
}