I'm looking for solutions for better data fetching in a Next.js app. In this question I'm not just looking for a solution, I'm looking for multiple options so we can look at the pros and cons.
The problem I have
Right now I have a few pages that all include a component that displays som static content and a that have some dynamic content that is fetched from an API. Each page do a fetch()
in their getInitialProps()
to get their own page data, but also the footer data, which is the same for all pages.
This of course works, but there is a lot of duplicated data fetching. The footer data will always be displayed for all pages and always be the same. It will also rarely be changed in the API, so no need for revalidate the data.
The answers I'm looking for
I'm not just looking to solve this one problem, I'm looking for an overview to learn some new practice for future projects as well. I like writing "obvious" code, so not looking for too hacky solutions, like writing to the window
object etc. Simple solutions with less dependancies are preferred. The goal is a fast site. It's not that important to reduce network usage/API calls.
What I have thought so far
This is the possible solutions I've come up with, somewhat sorted from simple/obvious to more complex.
- Do a fetch inside the Footer component (client side)
- Do a fetch in getInitialProps (server side & client side) on all /pages
- Do a fetch in _app.js with a HOC and hooking into it's
getInitialProps()
and add it to props, so data is available for all pages
- Use zeit/swr and data prefetching to cache data
- Use redux to store a global state
All of these "work", but most of them will refetch the data unnecessarily, and/or adds a bit more complexity. Here are the pros/cons as I see it (numbers are the same as above):
- ?? Simple! Fetch code is only in one place, it's located where it's used. ?? Data is fetched after page is loaded, so the content "jumps" in to view. Data is refetched all the time.
- ?? Simple! Data is fetched on the server, so content is available before the page is rendered. ?? Data is refetched for each page. We have to remember to fetch the same footer data for each page in their
getInitialProps()
.
- ?? We can do the fetch in one place and add it to all the pages props, so footer data is automatically available for all pages' props. ?? Might be a bit more complex for some to easily understand what's going on, as it requires a bit more understanding of how Next.js/React works. Still refetches the data for all pages. We now do two
fetch()
calls after each other (first in _app.js to load footer content, then in each page to get custom content), so it's even slower.
- ?? Somewhat simple. We can use the prefetching to load data to cache even before the JS is loaded. After first page load, we will have fast data fetching. Can have fetch code directly in footer component. ?? The
rel="preload"
prefetching technique won't work with all types of fetching (for instance Sanity's client using groq). To not have "jumpy" content where the data is loaded after initial page load, we should provide useSWR()
with initialData
which still will require us to fetch data in getInitialProps()
, but it would be enough to just do this on the server side. Could use the new getServerSideProps()
.
- ?? We can load data once(?) and have it available throughout the application. Fast and less/no refetching. ?? Adds external dependency. More complex as you'll have to learn redux, even to just load one shared data object.
Current solution, using the solution described in bullet point number 2.
const HomePage = (props) => {
return (
<Layout data={props.footer}>
<Home data={props.page} />
</Layout>
)
}
// Not actual query, just sample
const query = `{
"page": *[_type == "page"][0],
"footer": *[_type == "footer"][0]
}`
HomePage.getInitialProps = async () => {
const data = await client.fetch(query)
return {
page: data.page
footer: data.footer
}
}
export default HomePage
Would love some more insight into this. I'm a missing something obvious?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…