Call an API function during the Nextjs build process in getStaticProps
#16826
-
I want to run a build here is the code import data from "data/data.json";
export default function handler(req, res) {
return res.status(200).json({ data });
}
import axios from "axios";
import Home from "components/Home";
export default function HomePage() {
return <Home />;
}
export const getStaticProps = async () => {
const { data } = await axios("http://localhost:3000/api/data");
return {
revalidate: 1,
props: data,
};
}; The app works fine on dev mode but if I try to run a build it gives me this error
|
Beta Was this translation helpful? Give feedback.
Answered by
jamesmosier
Sep 3, 2020
Replies: 1 comment 1 reply
-
You should not call Next API routes from within |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
smootok
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should not call Next API routes from within
getStaticProps
(orgetStaticPaths
) because those API routes do not exist at the time of build (because they haven't been deployed anywhere).Instead, write your code directly in
getStaticProps