Hey y'all!
I’m currently developing this blog site using Prismic and Nuxt 3 and have encountered an issue where the blog article content doesn’t update correctly when navigating between articles (see next/prev links at bottom of this page). It works fine in my local dev environment, but you already knew that.
Ok, here's a whole lotta context:
-
Repository: here's a link to my repo over on GitHub.
-
Tech Stack: Prismic CMS, Nuxt 3, GSAP (for animations); deployed via Netlify with
npx nuxi generate
build command. -
Issue: When I click the “Next” or “Previous” links to navigate between articles, the URL updates correctly, but the page content doesn’t change. The new article data is not being fetched/displayed until I manually refresh the page.
-
Observations:
-
I’ve added a
:key="router.currentRoute.value.fullPath"
to the layout and article template to force a re-render on route change. This made the URL update correctly, and it triggered re-renders, but the content still doesn’t update without a refresh. -
The
NewsletterSignup
component (a popup which appears based on a cookie) shows up each time I navigate between articles. This seems to suggest that the route is updating, but article data is not being properly re-fetched/rendered.
-
Steps I’ve Taken So Far:
- Refetching Data on Route Change:
- I’m using
useAsyncData
to fetch the article data with awatch
option onroute.params.uid
to trigger re-fetching of article content. - I’ve also implemented a manual refresh using
watch
to triggerrefreshArticle
andrefreshAllArticles
functions when the route changes.
watch(() => route.params.uid, async () => {
await refreshArticle()
await refreshAllArticles()
})
However, this approach hasn’t resolved the issue, and the article content is still not refreshing when the route changes.
- Key Prop for Forcing Re-render:
- I’ve added
:key="router.currentRoute.value.fullPath"
to the layout and article page templates to ensure the component re-renders on route change. This did force re-rendering, but the new article data is not loading correctly.
- Attempted ScrollSmoother/GSAP Debugging:
- Initially, I suspected that GSAP’s ScrollSmoother might be causing conflicts with the route change behavior. However, after temporarily removing all GSAP plugins, the issue persisted. So, I’ve since added GSAP back in.
Here’s an Overview of My Setup:
- Layout (
/layouts/default.vue
):
- I’m using
gsap
withScrollSmoother
to handle smooth scrolling. The key for the layout is set as:key="router.currentRoute.value.fullPath"
.
- Article Page Template (
/pages/articles/[uid].vue
):
- Here's the full template on GitHub.
- The page fetches article data using
useAsyncData
:
const { data: article, refresh: refreshArticle } = useAsyncData('article', () =>
prismic.client.getByUID<BlogArticleDocument>('blog_article', route.params.uid as string),
{ watch: [() => route.params.uid] }
)
- I also fetch all articles to calculate the previous/next articles using:
const { data: allArticles, refresh: refreshAllArticles } = useAsyncData('allArticles', () =>
prismic.client.getAllByType<BlogArticleDocument>('blog_article', {
orderings: { field: 'my.blog_article.publication_date', direction: 'asc' },
fetch: ['blog_article.title', 'blog_article.featured_image'],
}),
{ watch: [() => route.params.uid] }
)
- The
watch
block is used to manually refresh the article and all articles when the route changes.
Despite all these efforts, the article content still doesn’t update on navigation without a page refresh.
Questions:
- Is there a better way to ensure Prismic data is refetched properly on route changes in Nuxt 3?
- Should I be handling route changes differently to force the content to update?
- Could this issue be related to how I’ve implemented
useAsyncData
or how the component re-renders? - Are the
watch
block and:key
attributes overkill?
Any insights or suggestions would be greatly appreciated!
Many thanks!