Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
32
rated 0 times [  33] [ 1]  / answers: 1 / hits: 13840  / 4 Years ago, thu, july 2, 2020, 12:00:00

I'm basically developing a blog on Next.js. Because it is another team which is in charge of the back-end, I'm currently making fetch API calls from getStaticProps to get my articles even though it's better practice to make database queries directly:


export async function getStaticProps({ params, res }) {
try {
const result = await fetch(`${API.baseURL}/get_article/${params.id}`);
const article = await result.json();

return {
props: { article },
};
} catch (error) {
res.statusCode = 404;
return { props: {} };
}
}

While this works perfectly in development mode, getting the article, editing it, and then accessing it again does not work in production (even locally, with the built version).


I guess it has something to do with Next.js handling cache somehow... What am I doing wrong? Thank you!


More From » reactjs

 Answers
4

First of all the argument of function getStaticProps i.e the context object doesn't have any property named res. So res.statusCode = 404; doesn't do anything here.


And getStaticProps is meant be used for static site generation, additionally for dynamic routes, you can export another function getStaticPaths which should generate and return an array of paths with the dynamic route params for which getStaticProps will be called at build time for pre-rendering the pages.


In development mode, data-fetching methods will be called per-request basis so your code works. But in production mode, it will show the pre-rendered static pages which means the page will show the content as it was rendered and if you edit and update the content it will not reflect on the pages.


If you decide to go with static-site-generation either you have to rebuild the entire site after an update to a blog or you have to have some kind of client-side data-fetching logic that will update the blog when you update its content.


For client-side data-fetching you can use something like swr or react-query


Here is some psuedo-code which might help with pre-rendering the pages,


for route /article/[articleId]


export async function getStaticPaths() {
const articles = await /* db query to get the list of articles or fetch from remote API*/

// generate a list of paths with route params
const paths = articles.map(article => ({ params: { articleId: article.id }}))

return {
paths,
fallback: false
// fallback can be true if you want to show a fallback version of page
// and serve JSON for unknown articles
}

}


export async function getStaticProps(ctx) {
try {
const result = await fetch(`${API.baseURL}/get_article/${params.id}`);
const article = await result.json();

return {
props: { article },
};
} catch (error) {
return {
props: null
}
}
}

Learn more about how fallback works in returned value of function getStaticPaths docs.


Another alternative is to use getServerSideProps as the data-fetching method which will be called on each request for the page, but TTFB(time to first byte) will be higher. So for a blog-post site, I will not suggest using getServerSideProps.


[#3305] Tuesday, June 30, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
freddiem

Total Points: 456
Total Questions: 116
Total Answers: 101

Location: Dominica
Member since Mon, Jan 4, 2021
3 Years ago
freddiem questions
Tue, Oct 26, 21, 00:00, 3 Years ago
Thu, Jun 17, 21, 00:00, 3 Years ago
Fri, Sep 27, 19, 00:00, 5 Years ago
;