diff --git a/routes/_utils/scheduleIdleTask.js b/routes/_utils/scheduleIdleTask.js index 578018f..e5f6a94 100644 --- a/routes/_utils/scheduleIdleTask.js +++ b/routes/_utils/scheduleIdleTask.js @@ -8,13 +8,19 @@ import { mark, stop } from './marks' const taskQueue = new Queue() let runningRequestIdleCallback = false +function getRIC () { + // we load polyfills asynchronously, so there's a tiny chance this is not defined + return typeof requestIdleCallback !== 'undefined' ? requestIdleCallback : setTimeout +} + function runTasks (deadline) { mark('scheduleIdleTask:runTasks()') while (taskQueue.length && deadline.timeRemaining() > 0) { taskQueue.shift()() } if (taskQueue.length) { - requestIdleCallback(runTasks) + let rIC = getRIC() + rIC(runTasks) } else { runningRequestIdleCallback = false } @@ -25,6 +31,7 @@ export function scheduleIdleTask (task) { taskQueue.push(task) if (!runningRequestIdleCallback) { runningRequestIdleCallback = true - requestIdleCallback(runTasks) + let rIC = getRIC() + rIC(runTasks) } }