Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit cc839f3

Browse files
committed
fix(refresh-on.tsx): add debounce processing
1 parent 2bf19ef commit cc839f3

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/core/component/refresh-on.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
'use client';
22

3-
import { useEffect } from 'react';
3+
import { useEffect, useTransition } from 'react';
44
import { useRouter, usePathname } from 'next/navigation';
55

66
export const RefreshOn = () => {
77
const router = useRouter();
88
const pathname = usePathname();
9+
const [, startTransition] = useTransition();
910

1011
useEffect(() => {
1112
const handleClick = (e: MouseEvent) => {
1213
const target = e.target as HTMLElement;
1314
const targetAnchor = target.closest('a');
1415
if (pathname && targetAnchor instanceof HTMLAnchorElement && targetAnchor.origin === window.location.origin) router.refresh();
1516
};
16-
17+
let isRefreshing = false; // Debounce processing
1718
const observeStyleSheets = () => {
1819
const styleObserver = new MutationObserver(mutations => {
20+
if (isRefreshing) return;
1921
for (const mutation of mutations) {
2022
if (mutation.type === 'childList') {
2123
const addedNodes = Array.from(mutation.addedNodes);
22-
if (addedNodes.some(node => node instanceof HTMLStyleElement || node instanceof HTMLLinkElement)) {
23-
requestAnimationFrame(() => {
24+
if (
25+
addedNodes.some(
26+
node =>
27+
node instanceof HTMLStyleElement || (node instanceof HTMLLinkElement && !(node.rel === 'preload' && node.getAttribute('as') === 'style'))
28+
)
29+
) {
30+
startTransition(() => {
31+
isRefreshing = true;
2432
router.refresh();
33+
setTimeout(() => {
34+
isRefreshing = false;
35+
}, 100);
2536
});
2637
break;
2738
}

0 commit comments

Comments
 (0)