can't use the data inside getStaticProps if navigated from another page
Unanswered
LaPerm posted this in #help-forum
LaPermOP
Hello everyone, possibly I'm missing something stupid but I'm working on this for 3 hours and still couldn't find the issue. Here is my code:
The thing is, If I refresh the page, I can make use of the data and it works.
However If I start browsing the website from another webpage and later on if I redirect to this page, even tho I can see the data at bottom of the code, the component above just thinks its undefined. Why? What could be the issue?
Its also weird because I have an exact copy of this file, but just for another language (ik duplicate code is bad, its just the previous dev) and it works fine. But for this one it does not work.
const Home: React.FC<HomeProps> = ({ home }) => {
return (
<IntlProvider locale="en" messages={en}>
{home && <HomeIndex home={home} defaultLanguage={Language.English} />}
{!home && <div>Home not found</div>}
// If I do F5 : I see the data and <HomeIndex> renders (first line)
// If I navigate here from another page, it always renders Home not found, even tho the log is working fine below (inside ......getStaticProps)
</IntlProvider>
);
};
export async function getStaticProps() {
const [locations, comments, campaigns, locationsData] = await Promise.all([
sendRequest("GET", locationsUrl, headers),
sendRequest("GET", commentsUrl, headers),
sendRequest("GET", campaignsurl, headers),
sendRequest("GET", allLocationsUrl, headers),
]);
// this log works perfectly - i can see the data.
console.log(campaigns, comments, locations, allLocations)
return {
props: {
home: {
campaigns,
comments,
locations,
allLocations: searchLocations,
},
},
revalidate: 1800,
};
}
export default Home;
The thing is, If I refresh the page, I can make use of the data and it works.
However If I start browsing the website from another webpage and later on if I redirect to this page, even tho I can see the data at bottom of the code, the component above just thinks its undefined. Why? What could be the issue?
Its also weird because I have an exact copy of this file, but just for another language (ik duplicate code is bad, its just the previous dev) and it works fine. But for this one it does not work.
2 Replies
LaPermOP
I'm not very experienced nextjs and I'm also hearing it could be related to not using build command. But I still had the issue after running the build command. Do you think its about that?
LaPermOP
Ok more info, I'm having this problem when I do
npm run dev
However when I do:
npm start
I don't have this issue. What does this mean? Does it mean I will only see this problem when running in local/dev mode , and not in prod version?
npm run dev
However when I do:
npm start
I don't have this issue. What does this mean? Does it mean I will only see this problem when running in local/dev mode , and not in prod version?