+data hook cannot access context data which was created by +onPrerenderStart #1759
Replies: 2 comments 3 replies
-
It's bit difficult to get a clear picture of your issue (e.g. some of what you say doesn't match how Vike actually works). So how about this: create a minimal reproduction with what you expect and what happens instead. We can then take it from there, in a step-by-step fashion. (FYI some users have a similar use case, so it's likely possible to achieve what you want.) |
Beta Was this translation helpful? Give feedback.
-
As I delved deeper into the framework last week, I realized that modifying the /pages/@id/+data.ts import { PageContext } from 'vike/types'
import { getAndCacheNavigation, getAndCacheWpPage } from '#lib/api'
const data = async (pageContext: PageContext) => {
const { id } = pageContext.routeParams
// Fetch the page by slug (vike: id) and store the result in a "page cache" (simple Map)
// Next time this route is requested (client route), the data is taken from this cache without a new fetch
const wpPageData = await getAndCacheWpPage(id)
// Fetch, cache, and provide navs for the data hook
const wpNavigationData = await getAndCacheNavigation(['main', 'footer'])
return {
pageData: wpPageData, // serialized page data
navigationData: wpNavigationData, // serialized nav data
}
}
export { data } such simple cache fnc could look like this export const WpPageCache = new Map<string, WPPageData['pageData'] | undefined>()
// also a nice spot to handle dispatch events for some state managers (redux ie)
export const getAndCacheWpPage = async (slug: string) => {
// bypass if cached
if (WpPageCache.has(slug)) return WpPageCache.get(slug)
// call: some-wp.com/api/page/{slug}
const response = await fetch(`${APP_CONFIG.apiUrl}/page/${slug}`)
if (response.status >= 400) throw render(404, response.statusText)
const pageResponse: WPPageResponse = await response.json()
const pageData = {
slug,
title: pageResponse.title,
acf: pageResponse.acf,
}
// set client cache
WpPageCache.set(slug, pageData)
return pageData
} In this project almost everything is dynamically routed to a respective WP page, I had to implement this in just two +data.ts hooks: since for the very first page user visits is no client-side cache provided, I wrote small hooks to pass this first page/nav (pre-rendered) also to the caches. This workflow allows Vike to autonomously build upon the data provided via the WP API. This is fantastic! ps: was the last piece of the puzzle and I'm happy to announce that I am building a headless WP theme with a minimal REST setup, together with a separate pre-rendered Vike frontend, which automatically pre-renders via a GitHub workflow upon saving a page in WordPress. For me this is a strong alternative to the current gatsby integrations, since on the wordpress-end we can work with the native rest api, designing the endpoint directly with PHP in access to the wordpress data. Surely coming up with something on github in the next months, for those who are interested. Thank you so much! |
Beta Was this translation helpful? Give feedback.
-
Hey guys, and first thanks to the contributors and @brillout for the fantastic vike. Using it a lot and currently deep-diving into prerendering and the pageContext.
While I`m trying to manage to connect a wordpress-rest-api with my prerendered vike I stumbled upon a problem, which let's me a little stuck at this point. Maybe you already saw the problem or can give me a little hint. Hope this just a configuration problem by a confused me.
the main idea is to fetch a set of navigation items from rest-endpoint on the prerender process (build / prod) and store it in the pageContext to get access to the set in my application later on, especially in the data-hook. I saw the hook
onPrerenderStart
is capable of putting custom stuff into the context when prerendering, so I came up with this code to storedata.navigation
/pages/+onPrerenderStart.ts
The result looks promising, for example this is a generated file after the prerendering:
(Also the prerendered HTML-files in dist dir containing the new data.)
dist/client/testpage/index.pageContext.json
Now I wanna access this prerendered
navigation
from the context in the data() hook, mainly to map the vikepageContext.routeParams.id
to the related navigation item found in thenavigation
to use it's related ID to query a wordpress endpoint directly in the same data hookBut navigation appears to be returned
undefined
by the data hook and also the rendered html in the started preview (run prod) does not contain the context data anymore.the data's
console.log(navigation)
was returned with values, when pre-rendering page on build in console./pages/@id/+data.ts
Surely I don't get the full picture - any idea what I'm missing here or knowledge if is this even possible?
Environment:
Beta Was this translation helpful? Give feedback.
All reactions