Skip to content

feat: improved script warmup #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 15, 2024
Merged

feat: improved script warmup #302

merged 13 commits into from
Oct 15, 2024

Conversation

harlan-zw
Copy link
Collaborator

πŸ”— Linked issue

#294,
#293,

❓ 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)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Current warmup behavior is a bit crude, this PR aims to improve the stability of it while also exposing end user config for more advance use cases.

Background

Nuxt Scripts will insert relevant warmup link tags to optimize the loading of your scripts. Optimizing
for the quickest load after Nuxt has finished hydrating.

For example if we have a script like so:

useScript('/script.js')

This code will load in /script.js on the onNuxtReady event. As the network may be idle while your Nuxt App is hydrating,
Nuxt Scripts will use this time to warmup the script by inserting a preload tag in the head of the document.

<link rel="preload" href="/script.js" as="script" fetchpriority="low">

The behavior is only applied when we are using the client or onNuxtReady Script Triggers.
To customize the behavior further we can use the warmupStrategy option.

warmupStrategy

The warmupStrategy option can be used to customize the link tag inserted for the script. The option can be a function
that returns an object with the following properties:

    • false - Disable warmup.
    • 'preload' - Preload the script, use when the script is loaded immediately.
    • 'preconnect' or 'dns-prefetch' - Preconnect to the script origin, use when you know a script will be loaded within 10 seconds. Only works when loading a script from a different origin, will fallback to false if the origin is the same.

All of these options can also be passed to a callback function, which can be useful when have a dynamic trigger for the script.

warmup

The warmup function can be called explicitly to add either preconnect or preload link tags for a script. This will only work the first time the function is called.

This can be useful when you know that the script is going to be loaded shortly.

const script = useScript('/video.js', {
  trigger: 'manual'
})
// warmup the script when we think the user may need the script
onVisible(videoContainer, () => {
  script.warmup('preload')
})
// load it in
onClick(videoContainer, () => {
  script.load()
})

Copy link

vercel bot commented Oct 14, 2024

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
scripts-docs βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Oct 15, 2024 7:21am
scripts-playground βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Oct 15, 2024 7:21am

@harlan-zw harlan-zw merged commit 3f168cc into main Oct 15, 2024
3 of 4 checks passed
@harlan-zw harlan-zw changed the title feat: improved warmup feat: improved script warmup Oct 15, 2024
@harlan-zw harlan-zw deleted the feat/warmup branch October 15, 2024 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant