Skip to content

Proposal: +initial-data.ts hook (Usecase: is it possible to call +data ONLY on first load)? #1904

Discussion options

You must be logged in to vote

Basically:

// +data.shared.ts (Will make +data run on first-load, and on client-navigations, but won't fetch `pageContext.json` anymore)

export async function data(pageContext) {
  if (typeof window !== "undefined") return; // Don't run on the client.
  
  return {
     dehydratedState: {...}
  }
}

// +Page.tsx
export default function MyPage() {
   const data = useData();
   useHydrate(data.dehydratedState);

  const myQuery = createQuery(...);
}

Here's a sample repo: https://github.com/Blankeos/vike-vs-next-ssr-experiment/tree/main/vike.

Personally works great for me.

cached.data.shared.ts.mp4

Alternative solution (I think would be easier), is just caching the pageContext.json.

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@Blankeos
Comment options

Answer selected by Blankeos
Comment options

You must be logged in to vote
1 reply
@brillout
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants