vike icon indicating copy to clipboard operation
vike copied to clipboard

Link Prefetching: also prefetch `pageContext`

Open XiNiHa opened this issue 3 years ago • 14 comments

Currently, there's no way to do any user-defined additional preloading jobs after finishing to preload static assets. So it's impossible to prefetch data needed for routes. Having an onAfterPreload hook (tried to match the naming convention) will solve the problem and it'll be very helpful to deeply tune the page performance.

XiNiHa avatar Jan 19 '22 11:01 XiNiHa

My idea so far for this is to:

  1. Extend the prefetchLink option, e.g.:
    useClientRouter({
      prefetchLinks: {
        prefetchPageContext: {
          when: 'HOVER',
          expire: 5 * 1000 // The prefetched `pageContext` is considered fersh for 5 seconds
        },
        prefetchSaticAssets: {
          when: 'VIEWPORT'
        }
      }
    })
    
  2. Allow the definition of a client-side-only onBeforeRender() (needed for tools such as Relay).

brillout avatar Jan 19 '22 11:01 brillout

I agree with you, that sounds very simple and straightforward.

XiNiHa avatar Jan 19 '22 11:01 XiNiHa

With 0.4 the DX is quite straightforward:

// /renderer/_default.page.client.js

export const prefetchStaticAssets = { when: 'VIEWPORT' }
export const prefetchPageContext = { when: 'HOVER', expire: 10 * 1000  }

Implementation should be fairly easy.

brillout avatar Jul 10 '22 12:07 brillout

Hi @brillout, is this part of the v1 design (#578) and being worked on? I'd love to work on it!

archit-p avatar Apr 03 '23 21:04 archit-p

@archit-p It isn't but this feature can be implemented independently of the V1 design. PR welcome!

brillout avatar Apr 03 '23 21:04 brillout

Hi, @brillout! May I work on it?

usk94 avatar Apr 21 '24 14:04 usk94

Hi @usk94, yes!

brillout avatar Apr 21 '24 14:04 brillout

@brillout Let me ask a question. Should prefetchPageContext.when default to false? Also, do I need to support override for individual links?

usk94 avatar Apr 22 '24 23:04 usk94

So far I'm inclined to think that we can enable it on HOVER by default. But let's see.

Also, do I need to support override for individual links?

I think that would be nice? Or do you see an issue with that?

brillout avatar Apr 23 '24 08:04 brillout

@brillout OK, I'll implement HOVER as default for now🙆‍♂️

Or do you see an issue with that?

I don't have at the moment. I just asked to be sure, thank you!

usk94 avatar Apr 23 '24 08:04 usk94