From 4a53582e14c313056f29bf8a241c9352dc17db70 Mon Sep 17 00:00:00 2001 From: Sukka Date: Tue, 20 Sep 2022 01:25:27 +0800 Subject: [PATCH] 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. --- packages/next/client/future/image.tsx | 6 +++++- packages/next/client/image.tsx | 6 +++++- .../image-component/default/pages/priority.js | 8 ++++++++ .../image-component/default/test/index.test.ts | 12 ++++++++++++ .../image-future/default/pages/priority.js | 8 ++++++++ .../image-future/default/test/index.test.ts | 7 +++++++ 6 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/next/client/future/image.tsx b/packages/next/client/future/image.tsx index e42a606e21dc3..dc64f1640d66b 100644 --- a/packages/next/client/future/image.tsx +++ b/packages/next/client/future/image.tsx @@ -825,10 +825,14 @@ export default function Image({ imageSrcSetPropName = 'imageSrcSet' imageSizesPropName = 'imageSizes' } - const linkProps = { + const linkProps: React.DetailedHTMLProps< + React.LinkHTMLAttributes, + 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) diff --git a/packages/next/client/image.tsx b/packages/next/client/image.tsx index d986ae3a93c28..4018ca7b7e1f0 100644 --- a/packages/next/client/image.tsx +++ b/packages/next/client/image.tsx @@ -978,10 +978,14 @@ export default function Image({ imageSrcSetPropName = 'imageSrcSet' imageSizesPropName = 'imageSizes' } - const linkProps = { + const linkProps: React.DetailedHTMLProps< + React.LinkHTMLAttributes, + 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 = diff --git a/test/integration/image-component/default/pages/priority.js b/test/integration/image-component/default/pages/priority.js index 741ae056ea023..975d7a39c2c13 100644 --- a/test/integration/image-component/default/pages/priority.js +++ b/test/integration/image-component/default/pages/priority.js @@ -12,6 +12,14 @@ const Page = () => { width="400" height="400" > + { width="400" height="400" > +