-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…#68199) This reverts commit 7f677d1. Cannot reproduce the `vercel-site` failure cited in #68176, and there's no apparent reason why this PR would cause that
- Loading branch information
1 parent
6fe66a0
commit 4b59a7f
Showing
7 changed files
with
218 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useMemo, type Ref } from 'react' | ||
|
||
export function useMergedRef<TElement>( | ||
refA: Ref<TElement>, | ||
refB: Ref<TElement> | ||
): Ref<TElement> { | ||
return useMemo(() => mergeRefs(refA, refB), [refA, refB]) | ||
} | ||
|
||
export function mergeRefs<TElement>( | ||
refA: Ref<TElement>, | ||
refB: Ref<TElement> | ||
): Ref<TElement> { | ||
if (!refA || !refB) { | ||
return refA || refB | ||
} | ||
|
||
return (current: TElement) => { | ||
const cleanupA = applyRef(refA, current) | ||
const cleanupB = applyRef(refB, current) | ||
|
||
return () => { | ||
cleanupA() | ||
cleanupB() | ||
} | ||
} | ||
} | ||
|
||
function applyRef<TElement>( | ||
refA: NonNullable<Ref<TElement>>, | ||
current: TElement | ||
) { | ||
if (typeof refA === 'function') { | ||
const cleanup = refA(current) | ||
if (typeof cleanup === 'function') { | ||
return cleanup | ||
} else { | ||
return () => refA(null) | ||
} | ||
} else { | ||
refA.current = current | ||
return () => { | ||
refA.current = null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react' | ||
import Link from 'next/link' | ||
import { useCallback, useRef, useEffect, useState } from 'react' | ||
import { flushSync } from 'react-dom' | ||
|
||
export default function Page() { | ||
const [isVisible, setIsVisible] = useState(true) | ||
|
||
const statusRef = useRef({ wasInitialized: false, wasCleanedUp: false }) | ||
|
||
const refWithCleanup = useCallback((el) => { | ||
if (!el) { | ||
console.error( | ||
'callback refs that returned a cleanup should never be called with null' | ||
) | ||
return | ||
} | ||
|
||
statusRef.current.wasInitialized = true | ||
return () => { | ||
statusRef.current.wasCleanedUp = true | ||
} | ||
}, []) | ||
|
||
useEffect(() => { | ||
const timeout = setTimeout( | ||
() => { | ||
flushSync(() => { | ||
setIsVisible(false) | ||
}) | ||
if (!statusRef.current.wasInitialized) { | ||
console.error('callback ref was not initialized') | ||
} | ||
if (!statusRef.current.wasCleanedUp) { | ||
console.error('callback ref was not cleaned up') | ||
} | ||
}, | ||
100 // if we hide the Link too quickly, the prefetch won't fire, failing a test | ||
) | ||
return () => clearTimeout(timeout) | ||
}, []) | ||
|
||
if (!isVisible) { | ||
return null | ||
} | ||
|
||
return ( | ||
<Link href="/" ref={refWithCleanup}> | ||
Click me | ||
</Link> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/integration/next-image-new/app-dir/app/ref-cleanup/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use client' | ||
import Image from 'next/image' | ||
import { useCallback, useEffect, useState } from 'react' | ||
|
||
export default function Home() { | ||
const [displayImage, setDisplayImage] = useState(true) | ||
|
||
const refWithCleanup = useCallback((el) => { | ||
if (!el) { | ||
throw new Error( | ||
'callback refs that returned a cleanup should never be called with null' | ||
) | ||
} | ||
|
||
return () => { | ||
console.log('callback ref was cleaned up') | ||
} | ||
}, []) | ||
|
||
useEffect(() => { | ||
setDisplayImage(false) | ||
}, []) | ||
|
||
return ( | ||
<main> | ||
<h1>Should call ref cleanup on unmount</h1> | ||
<section> | ||
{displayImage ? ( | ||
<div style={{ position: 'relative', width: 10, height: 10 }}> | ||
<Image | ||
ref={refWithCleanup} | ||
priority | ||
fill | ||
src="/test.jpg" | ||
alt="alt" | ||
sizes="10px" | ||
/> | ||
</div> | ||
) : null} | ||
</section> | ||
</main> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters