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({
imageSrcSetPropName = 'imageSrcSet'
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.
[imageSrcSetPropName]: imgAttributes.srcSet,
[imageSizesPropName]: imgAttributes.sizes,
crossOrigin: rest.crossOrigin,
}
const onLoadingCompleteRef = useRef(onLoadingComplete)
......
......@@ -978,10 +978,14 @@ export default function Image({
imageSrcSetPropName = 'imageSrcSet'
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.
[imageSrcSetPropName]: imgAttributes.srcSet,
[imageSizesPropName]: imgAttributes.sizes,
crossOrigin: rest.crossOrigin,
}
const useLayoutEffect =
......
......@@ -12,6 +12,14 @@ const Page = () => {
width="400"
height="400"
></Image>
<Image
priority
id="basic-image-with-crossorigin"
crossOrigin="anonymous"
src="/test.jpg"
width="400"
height="400"
></Image>
<Image
loading="eager"
id="load-eager"
......
......@@ -141,6 +141,11 @@ function runTests(mode) {
expect(
await browser.elementById('basic-image').getAttribute('loading')
).toBe(null)
expect(
await browser
.elementById('basic-image-with-crossorigin')
.getAttribute('loading')
).toBe(null)
expect(
await browser.elementById('load-eager').getAttribute('loading')
).toBe(null)
......@@ -157,6 +162,13 @@ function runTests(mode) {
expect(warnings).not.toMatch(
/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 {
if (browser) {
await browser.close()
......
......@@ -12,6 +12,14 @@ const Page = () => {
width="400"
height="400"
></Image>
<Image
priority
id="basic-image-crossorigin"
src="/test.jpg"
width="400"
height="400"
crossOrigin="anonymous"
></Image>
<Image
loading="eager"
id="load-eager"
......
......@@ -157,6 +157,13 @@ function runTests(mode) {
expect(warnings).not.toMatch(
/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 {
if (browser) {
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