Next.js Discord

Discord Forum

Fetch an api and use that data on build time for ALL pages

Unanswered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
Hi, is there a way I could use getStaticProps for all pages without having to put it on every page? I need to fetch an api at build time and make the data fetched available in entire app using React.useContext

10 Replies

@Masai Lion Hi, is there a way I could use getStaticProps for all pages without having to put it on every page? I need to fetch an api at build time and make the data fetched available in entire app using React.useContext
I think you could create it in another file and export it from the page
//lib/get-static-props.ts
export const getStaticProps: GetStaticProps = async () => {}

// pages/index.tsx
...

export { getStaticProps } from 'lib/get-static-props'
Masai LionOP
Is there a better way without having to do this? @Ray
Masai LionOP
:this_is_fine:
Masai LionOP
Im using next export
not sure if it will work
without server
@Masai Lion not sure if it will work
yeah im not sure if it work too
but you could try it and see if it can build