content: The fetch() hook is no longer called on the client-side in production/universal mode
Hey!
First of all thanks for the nice work đ
I encountered something really strange and Iâm not too sure about it but it looks like that the new fetch() hook of nuxt is not called on the client-side when in production mode:
Version
@nuxt/content: ^1.6.0 nuxt: 2.14.0
Steps to reproduce
Create a dynamic page component with a fetch() hook (example: pages/projects/_slug.vue).
export default {
fetch() {
console.log('fetch');
}
}
What is Expected?
Fetch to be called when navigation occurs and a project is shown on the client side.
What is actually happening?
Fetch is only called on the client side when in development mode. In production mode, it is never called (only on the server).
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 4
- Comments: 16 (5 by maintainers)
After stepping through production code, I found the issue (for me at least) is not that fetch isnât called. Itâs that the componentâs
fetchKeyis misaligned with the cached fetch data ($nuxt._pagePayload.fetch). After setting a customfetchKey(thanks to #8466), the issue went away.If this is indeed the correct solution, the documentation should give some guidance on when to use the new
fetchKeyoptionwhen using Nuxt Content.If the content would show up on client-side navigation, I would agree with you. Since it doesnât we have to call
$fetch()by hand again.We use a component in
pages/index.vuethat fetches stories written in *.md-files with Nuxt-content (<slug>-<locale>.md). Each md-file has yaml vars up top for title, description etc. and locale.$ npm run generateand$ npm run startshows the stories on initial load, which makes sense, because the stories are hardcoded intodist/index.html./again, the stories are not there.That must mean
fetch()-hook is not called or the âcachingâ that Nuxt uses under the hood doesnât really work with Nuxt-Content.Here is our component:
Maybe Iâll find time to recreate the issue in a sandbox, but for now I hope this information helps getting to the bottom of this.
Would you mind opening a PR to the nuxtjs.org repo? This would be really kind âşď¸
I think under the Data Fetching topic in Nuxtâs docs, there should be a warning that if the fetch hook is used in a component in a layout, a custom fetchKey (or a component name) should be specified.
Actually reopening the issue!
I managed to recreate it:
It appears that calling again
fetch()in another component will override thefetch()in my first component.Is @nuxt/content overriding the nuxt fetch hook?