Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
/frameworkPublic archive

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

Merged
pi0 merged 22 commits into
nuxt:mainfrom
Mini-ghost:feat/nuxt-link-prefetch
Sep 13, 2022
Merged

feat(nuxt): support prefetching <nuxt-link>#4329
pi0 merged 22 commits into
nuxt:mainfrom
Mini-ghost:feat/nuxt-link-prefetch

Conversation

@Mini-ghost

@Mini-ghostMini-ghost commented Apr 13, 2022

Copy link
Copy Markdown
Member

🔗 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.

<linkrel="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

netlifyBot commented Apr 13, 2022

Copy link
Copy Markdown

Deploy Preview for nuxt3-docs canceled.

NameLink
🔨 Latest commitb7bea15
🔍 Latest deploy loghttps://app.netlify.com/sites/nuxt3-docs/deploys/6320e387fc714e0008189c59

@pi0pi0 added enhancement New feature or request app labels Apr 13, 2022
@atinux

Copy link
Copy Markdown
Member

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

Mini-ghost commented Apr 16, 2022

Copy link
Copy Markdown
MemberAuthor

@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.
constrequestIdleCallback=process.client
? window.requestIdleCallback||function(cb){conststart=Date.now()constidleDeadline={didTimeout: false,timeRemaining(){returnMath.max(0,50-(Date.now()-start))}}returnwindow.setTimeout(function(){cb(idleDeadline)},1)}
: (()=>{})asanyasWindow['requestIdleCallback']
  1. When component mounted and need prefetch, implement with Singleton.
letrequestIdleCallback: Window['requestIdleCallback']|null=null
onMounted(()=>{if(!shouldPrefetch.value){return}// add `requestIdleCallback` polyfill if not supported and need prefetchif(!requestIdleCallback){requestIdleCallback=window.requestIdleCallback||function(cb){conststart=Date.now()constidleDeadline={didTimeout: false,timeRemaining(){returnMath.max(0,50-(Date.now()-start))}}returnwindow.setTimeout(function(){cb(idleDeadline)},1)}}})

Which is the preferred approach?

@danielroe

Copy link
Copy Markdown
Member

@Mini-ghost The first approach seems better to me 👍

@danielroedanielroe left a comment

Copy link
Copy Markdown
Member

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 pi0June 15, 2022 11:39
@pi0pi0 self-assigned this Sep 13, 2022
pi0
pi0 approved these changes Sep 13, 2022
@pi0

pi0 commented Sep 13, 2022

Copy link
Copy Markdown
Member

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.

@pi0pi0 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:mainSep 13, 2022
pi0 added a commit that referenced this pull request Sep 14, 2022
Co-authored-by: Pooya Parsa <pooya@pi0.io>
@pi0pi0 mentioned this pull request Sep 15, 2022
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

3.xappenhancementNew feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@Mini-ghost@atinux@danielroe@pi0