This repository was archived by the owner on Jan 30, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +15
-4
lines changed
Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Original file line number Diff line number Diff line change 11'use client' ;
22
3- import { useEffect } from 'react' ;
3+ import { useEffect , useTransition } from 'react' ;
44import { useRouter , usePathname } from 'next/navigation' ;
55
66export 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 }
You can’t perform that action at this time.
0 commit comments