Skip to content
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

fix(hydration): provide compat fallback for idle callback #11935

Merged
merged 13 commits into from
Oct 11, 2024
23 changes: 22 additions & 1 deletion packages/runtime-core/src/hydrationStrategies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import { isString } from '@vue/shared'
import { getGlobalThis, isString } from '@vue/shared'
import { DOMNodeTypes, isComment } from './hydration'

const globalThis = getGlobalThis()
Copy link
Member

Choose a reason for hiding this comment

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

Move this into the functions where it's needed and call it inline (it's cached) so that this can be treeshaken with the functions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Typescript appears to have issues with this approach

// https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/compat/idle-callback.ts
// Polyfills for Safari support
// https://caniuse.com/requestidlecallback
const requestIdleCallback: typeof globalThis.requestIdleCallback =
globalThis.requestIdleCallback ||
yyx990803 marked this conversation as resolved.
Show resolved Hide resolved
(cb => {
const start = Date.now()
const idleDeadline = {
GalacticHypernova marked this conversation as resolved.
Show resolved Hide resolved
didTimeout: false,
timeRemaining: () => Math.max(0, 50 - (Date.now() - start)),
}
return setTimeout(cb, 1)
})

const cancelIdleCallback: typeof globalThis.cancelIdleCallback =
globalThis.cancelIdleCallback ||
(id => {
clearTimeout(id)
})

/**
* A lazy hydration strategy for async components.
* @param hydrate - call this to perform the actual hydration.
Expand Down