Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/web/app/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* See the LICENSE file for details.
*/

import "@/lib/polyfills";
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import { HydratedRouter } from "react-router/dom";
Expand Down
7 changes: 4 additions & 3 deletions apps/web/core/components/core/render-if-visible-HOC.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import type { ReactNode, MutableRefObject } from "react";
import React, { useState, useRef, useEffect } from "react";
import { cn } from "@plane/utils";
import { scheduleIdleCallback } from "@/lib/polyfills";

type Props = {
defaultHeight?: string;
Expand Down Expand Up @@ -51,8 +52,8 @@ function RenderIfVisible(props: Props) {
const observer = new IntersectionObserver(
(entries) => {
//DO no remove comments for future
if (typeof window !== undefined && window.requestIdleCallback && useIdletime) {
window.requestIdleCallback(() => setShouldVisible(entries[entries.length - 1].isIntersecting), {
if (typeof window !== "undefined" && useIdletime) {
scheduleIdleCallback(() => setShouldVisible(entries[entries.length - 1].isIntersecting), {
timeout: 300,
});
} else {
Expand All @@ -77,7 +78,7 @@ function RenderIfVisible(props: Props) {
//Set height after render
useEffect(() => {
if (intersectionRef.current && isVisible && shouldRecordHeights) {
window.requestIdleCallback(() => {
scheduleIdleCallback(() => {
if (intersectionRef.current) placeholderHeight.current = `${intersectionRef.current.offsetHeight}px`;
});
}
Expand Down
21 changes: 18 additions & 3 deletions apps/web/core/lib/polyfills/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
* See the LICENSE file for details.
*/

if (typeof window !== "undefined" && window) {
// Add request callback polyfill to browser in case it does not exist
/** Installs `window.requestIdleCallback` / `cancelIdleCallback` when missing (e.g. older Safari). Idempotent. */
function ensureRequestIdleCallbackPolyfilled(): void {
if (typeof window === "undefined" || !window) {
return;
}

window.requestIdleCallback =
window.requestIdleCallback ??
function (cb) {
Expand All @@ -27,4 +31,15 @@ if (typeof window !== "undefined" && window) {
};
}

export {};
ensureRequestIdleCallbackPolyfilled();

export function scheduleIdleCallback(
callback: IdleRequestCallback,
options?: IdleRequestOptions,
): number {
ensureRequestIdleCallbackPolyfilled();
if (typeof window === "undefined") {
return 0;
}
return window.requestIdleCallback(callback, options);
}
Loading