Skip to content

How can we prefetch on the server and resume on the client? #79

Answered by eglove
eglove asked this question in Q&A
Discussion options

You must be logged in to vote

It looks like there's no way to set the cache before the page is initialized, but I can read from NEXT_DATA directly. I'm not super-confident in this, but it is what I'm going for.

  const { data } = useFetch(getTodos, {
    initialData: {
      data:
        typeof document === 'undefined'
          ? null
          : JSON.parse(document.querySelector('#__NEXT_DATA__').textContent)
              .props.pageProps.data,
    },
  });

Abstracting this out to use cache keys, I can do:

export function getNextPageProperties<T>(cacheKey: string) {
  if (typeof document === 'undefined') {
    return;
  }

  const element = document.querySelector('#__NEXT_DATA__');

  if (isNil(element)) {
    re…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by eglove
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant