onNuxtReady v3.1
onNuxtReady only runs on the client-side. It is ideal for running code that should not block the initial rendering of your app.
export default defineNuxtPlugin(() => {
onNuxtReady(async () => {
const myAnalyticsLibrary = await import('my-big-analytics-library')
// do something with myAnalyticsLibrary
})
})It is 'safe' to run even after your app has initialized. In this case, then the code will be registered to run in the next idle callback.
Type
export function onNuxtReady (callback: () => any, options?: OnNuxtReadyOptions): void
interface OnNuxtReadyOptions {
timeout?: number
signal?: AbortSignal
}callback
Type: () => any
The function to run once your app has finished initializing and the browser is idle.
options (optional)
Type: OnNuxtReadyOptions
timeout(optional)Type:
numberThe maximum number of milliseconds to wait for the browser to become idle once your app is ready. It is passed to
requestIdleCallback. Without it, the callback may be delayed for several seconds if the browser does not schedule an idle period, which can happen in headless browsers.
export default defineNuxtPlugin(() => {
onNuxtReady(() => {
// runs when the browser is idle, or about 2 seconds after your app is ready, whichever comes first
}, { timeout: 2000 })
})signal(optional)Type:
AbortSignalSkips the callback if the signal is aborted before it runs. This is useful when calling
onNuxtReadyfrom a component that may unmount before your app is ready.
<script setup lang="ts">
const controller = new AbortController()
onMounted(() => {
onNuxtReady(() => {
// does not run if the component unmounts first
}, { signal: controller.signal })
})
onBeforeUnmount(() => controller.abort())
</script>