Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt): support prefetching <nuxt-link> #4329

Merged
merged 22 commits into from Sep 13, 2022
Merged

feat(nuxt): support prefetching <nuxt-link> #4329

merged 22 commits into from Sep 13, 2022

Conversation

Mini-ghost
Copy link
Contributor

@Mini-ghost Mini-ghost commented Apr 13, 2022

πŸ”— Linked issue

nuxt/nuxt#13482 #2086

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Support prefetch prop for <nuxt-link>. This implementation is refer to https://github.com/nuxt/framework/discussions/2086#discussioncomment-1683819. Need a polyfill for requestIdleCallback ?

Also, I'm not sure how to use Link Prefetching like below, because path are unpredictable.

<link rel="prefetch" as="script" href="path" />

The implementation of this PR refers to nuxt2, maybe we can generate a router manifest. and mapping file path when triggering prefetch ?

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@netlify
Copy link

netlify bot commented Apr 13, 2022

βœ… Deploy Preview for nuxt3-docs canceled.

Name Link
πŸ”¨ Latest commit b7bea15
πŸ” Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/6320e387fc714e0008189c59

@pi0 pi0 added enhancement New feature or request app labels Apr 13, 2022
@Atinux
Copy link
Member

Atinux commented Apr 14, 2022

Thank you for the PR @Mini-ghost πŸ’š

From what I see, requestIdleCallback is not supported in Safari without a flag: https://caniuse.com/requestidlecallback

So I guess we need a polyfill for it, what would be the preferred approach to do so @pi0 ?

@Mini-ghost
Copy link
Contributor Author

Mini-ghost commented Apr 16, 2022

@pi0 @Atinux I will add polyfill for requestIdleCallback. And I think we have two way to polyfill.

  1. import polyfill at topest of the file and run in client side.
const requestIdleCallback = process.client
  ? window.requestIdleCallback || function (cb) {
    const start = Date.now()
    const idleDeadline = {
      didTimeout: false,
      timeRemaining () {
        return Math.max(0, 50 - (Date.now() - start))
      }
    }
    return window.setTimeout(function () {
      cb(idleDeadline)
    }, 1)
  }
  : (() => {}) as any as Window['requestIdleCallback']
  1. When component mounted and need prefetch, implement with Singleton.
let requestIdleCallback: Window['requestIdleCallback'] | null = null
onMounted(() => {
  if (!shouldPrefetch.value) { return }

  // add `requestIdleCallback` polyfill if not supported and need prefetch
  if (!requestIdleCallback) {
    requestIdleCallback = window.requestIdleCallback || function (cb) {
      const start = Date.now()
      const idleDeadline = {
        didTimeout: false,
        timeRemaining () {
          return Math.max(0, 50 - (Date.now() - start))
        }
      }
      return window.setTimeout(function () {
        cb(idleDeadline)
      }, 1)
    }
  }
})

Which is the preferred approach?

@danielroe
Copy link
Member

@Mini-ghost The first approach seems better to me πŸ‘

Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good to me but @pi0 may have some more ideas for improvement.

@danielroe danielroe requested a review from pi0 June 15, 2022 11:39
@pi0 pi0 self-assigned this Sep 13, 2022
@pi0
Copy link
Member

pi0 commented Sep 13, 2022

Thank you so much for helping on this feature @Mini-ghost <3

I've made few more reactors to the PR to simplify do small fixes and also integrated with new payload prerendering using link:prefetch hook to be ready to land.

@pi0 pi0 changed the title feat(nuxt3): support prefetch prop for <nuxt-link> feat(nuxt): support prefetching <nuxt-link> Sep 13, 2022
@pi0 pi0 merged commit addcb5c into nuxt:main Sep 13, 2022
pi0 added a commit that referenced this pull request Sep 14, 2022
Co-authored-by: Pooya Parsa <pooya@pi0.io>
@pi0 pi0 mentioned this pull request Sep 15, 2022
@danielroe danielroe added the 3.x label Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3.x app enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants