An implementation from getStaticProps from Next.js to Gatsby via Plugin.
$ npm i gatsby-plugin-static-props
or
$ yarn addgatsby-plugin-static-props
Add the plugin to your gatsby-config.js
.
module.exports = {
plugins: [
`gatsby-plugin-static-props`
]
}
const Home = ({ pageContext }) => {
const dog = pageContext.dog
return (
<div>
<img src={dog.message} />
</div>
)
}
export default Home
//execute in server-side only
export const getStaticProps = async () => {
const res = await fetch(`https://dog.ceo/api/breeds/image/random`)
const dog = await res.json()
return {
dog,
}
}
The code is available under the MIT License.