Unverified Commit 4a53582e authored by Sukka's avatar Sukka Committed by GitHub

fix(image): preload should respect crossOrigin (#40676)



## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

Currently, `link[rel=preload]` inserted by `priority` will not have the `crossOrigin` attribute, which will cause the preloaded response not to be used, since the CORS policy mismatches. The PR fixes that.
parent db3b8446
...@@ -825,10 +825,14 @@ export default function Image({ ...@@ -825,10 +825,14 @@ export default function Image({
imageSrcSetPropName = 'imageSrcSet' imageSrcSetPropName = 'imageSrcSet'
imageSizesPropName = 'imageSizes' imageSizesPropName = 'imageSizes'
} }
const linkProps = { const linkProps: React.DetailedHTMLProps<
React.LinkHTMLAttributes<HTMLLinkElement>,
HTMLLinkElement
> = {
// Note: imagesrcset and imagesizes are not in the link element type with react 17. // Note: imagesrcset and imagesizes are not in the link element type with react 17.
[imageSrcSetPropName]: imgAttributes.srcSet, [imageSrcSetPropName]: imgAttributes.srcSet,
[imageSizesPropName]: imgAttributes.sizes, [imageSizesPropName]: imgAttributes.sizes,
crossOrigin: rest.crossOrigin,
} }
const onLoadingCompleteRef = useRef(onLoadingComplete) const onLoadingCompleteRef = useRef(onLoadingComplete)
......
...@@ -978,10 +978,14 @@ export default function Image({ ...@@ -978,10 +978,14 @@ export default function Image({
imageSrcSetPropName = 'imageSrcSet' imageSrcSetPropName = 'imageSrcSet'
imageSizesPropName = 'imageSizes' imageSizesPropName = 'imageSizes'
} }
const linkProps = { const linkProps: React.DetailedHTMLProps<
React.LinkHTMLAttributes<HTMLLinkElement>,
HTMLLinkElement
> = {
// Note: imagesrcset and imagesizes are not in the link element type with react 17. // Note: imagesrcset and imagesizes are not in the link element type with react 17.
[imageSrcSetPropName]: imgAttributes.srcSet, [imageSrcSetPropName]: imgAttributes.srcSet,
[imageSizesPropName]: imgAttributes.sizes, [imageSizesPropName]: imgAttributes.sizes,
crossOrigin: rest.crossOrigin,
} }
const useLayoutEffect = const useLayoutEffect =
......
...@@ -12,6 +12,14 @@ const Page = () => { ...@@ -12,6 +12,14 @@ const Page = () => {
width="400" width="400"
height="400" height="400"
></Image> ></Image>
<Image
priority
id="basic-image-with-crossorigin"
crossOrigin="anonymous"
src="/test.jpg"
width="400"
height="400"
></Image>
<Image <Image
loading="eager" loading="eager"
id="load-eager" id="load-eager"
......
...@@ -141,6 +141,11 @@ function runTests(mode) { ...@@ -141,6 +141,11 @@ function runTests(mode) {
expect( expect(
await browser.elementById('basic-image').getAttribute('loading') await browser.elementById('basic-image').getAttribute('loading')
).toBe(null) ).toBe(null)
expect(
await browser
.elementById('basic-image-with-crossorigin')
.getAttribute('loading')
).toBe(null)
expect( expect(
await browser.elementById('load-eager').getAttribute('loading') await browser.elementById('load-eager').getAttribute('loading')
).toBe(null) ).toBe(null)
...@@ -157,6 +162,13 @@ function runTests(mode) { ...@@ -157,6 +162,13 @@ function runTests(mode) {
expect(warnings).not.toMatch( expect(warnings).not.toMatch(
/was detected as the Largest Contentful Paint/gm /was detected as the Largest Contentful Paint/gm
) )
// should preload with crossorigin
expect(
await browser.elementsByCss(
'link[rel=preload][as=image][crossorigin=anonymous][imagesrcset*="test.jpg"]'
)
).toHaveLength(1)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
......
...@@ -12,6 +12,14 @@ const Page = () => { ...@@ -12,6 +12,14 @@ const Page = () => {
width="400" width="400"
height="400" height="400"
></Image> ></Image>
<Image
priority
id="basic-image-crossorigin"
src="/test.jpg"
width="400"
height="400"
crossOrigin="anonymous"
></Image>
<Image <Image
loading="eager" loading="eager"
id="load-eager" id="load-eager"
......
...@@ -157,6 +157,13 @@ function runTests(mode) { ...@@ -157,6 +157,13 @@ function runTests(mode) {
expect(warnings).not.toMatch( expect(warnings).not.toMatch(
/was detected as the Largest Contentful Paint/gm /was detected as the Largest Contentful Paint/gm
) )
// should preload with crossorigin
expect(
await browser.elementsByCss(
'link[rel=preload][as=image][crossorigin=anonymous][imagesrcset*="test.jpg"]'
)
).toHaveLength(1)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment