Skip to content

Commit 4a53582

Browse files
authored
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.
1 parent db3b844 commit 4a53582

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

packages/next/client/future/image.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -825,10 +825,14 @@ export default function Image({
825825
imageSrcSetPropName = 'imageSrcSet'
826826
imageSizesPropName = 'imageSizes'
827827
}
828-
const linkProps = {
828+
const linkProps: React.DetailedHTMLProps<
829+
React.LinkHTMLAttributes<HTMLLinkElement>,
830+
HTMLLinkElement
831+
> = {
829832
// Note: imagesrcset and imagesizes are not in the link element type with react 17.
830833
[imageSrcSetPropName]: imgAttributes.srcSet,
831834
[imageSizesPropName]: imgAttributes.sizes,
835+
crossOrigin: rest.crossOrigin,
832836
}
833837

834838
const onLoadingCompleteRef = useRef(onLoadingComplete)

packages/next/client/image.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -978,10 +978,14 @@ export default function Image({
978978
imageSrcSetPropName = 'imageSrcSet'
979979
imageSizesPropName = 'imageSizes'
980980
}
981-
const linkProps = {
981+
const linkProps: React.DetailedHTMLProps<
982+
React.LinkHTMLAttributes<HTMLLinkElement>,
983+
HTMLLinkElement
984+
> = {
982985
// Note: imagesrcset and imagesizes are not in the link element type with react 17.
983986
[imageSrcSetPropName]: imgAttributes.srcSet,
984987
[imageSizesPropName]: imgAttributes.sizes,
988+
crossOrigin: rest.crossOrigin,
985989
}
986990

987991
const useLayoutEffect =

test/integration/image-component/default/pages/priority.js

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ const Page = () => {
1212
width="400"
1313
height="400"
1414
></Image>
15+
<Image
16+
priority
17+
id="basic-image-with-crossorigin"
18+
crossOrigin="anonymous"
19+
src="/test.jpg"
20+
width="400"
21+
height="400"
22+
></Image>
1523
<Image
1624
loading="eager"
1725
id="load-eager"

test/integration/image-component/default/test/index.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ function runTests(mode) {
141141
expect(
142142
await browser.elementById('basic-image').getAttribute('loading')
143143
).toBe(null)
144+
expect(
145+
await browser
146+
.elementById('basic-image-with-crossorigin')
147+
.getAttribute('loading')
148+
).toBe(null)
144149
expect(
145150
await browser.elementById('load-eager').getAttribute('loading')
146151
).toBe(null)
@@ -157,6 +162,13 @@ function runTests(mode) {
157162
expect(warnings).not.toMatch(
158163
/was detected as the Largest Contentful Paint/gm
159164
)
165+
166+
// should preload with crossorigin
167+
expect(
168+
await browser.elementsByCss(
169+
'link[rel=preload][as=image][crossorigin=anonymous][imagesrcset*="test.jpg"]'
170+
)
171+
).toHaveLength(1)
160172
} finally {
161173
if (browser) {
162174
await browser.close()

test/integration/image-future/default/pages/priority.js

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ const Page = () => {
1212
width="400"
1313
height="400"
1414
></Image>
15+
<Image
16+
priority
17+
id="basic-image-crossorigin"
18+
src="/test.jpg"
19+
width="400"
20+
height="400"
21+
crossOrigin="anonymous"
22+
></Image>
1523
<Image
1624
loading="eager"
1725
id="load-eager"

test/integration/image-future/default/test/index.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ function runTests(mode) {
157157
expect(warnings).not.toMatch(
158158
/was detected as the Largest Contentful Paint/gm
159159
)
160+
161+
// should preload with crossorigin
162+
expect(
163+
await browser.elementsByCss(
164+
'link[rel=preload][as=image][crossorigin=anonymous][imagesrcset*="test.jpg"]'
165+
)
166+
).toHaveLength(1)
160167
} finally {
161168
if (browser) {
162169
await browser.close()

0 commit comments

Comments
 (0)