Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(image): preload should respect crossOrigin #40676

Merged
merged 7 commits into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/next/client/future/image.tsx
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion packages/next/client/image.tsx
Expand Up @@ -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 =
Expand Down
8 changes: 8 additions & 0 deletions test/integration/image-component/default/pages/priority.js
Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions test/integration/image-component/default/test/index.test.ts
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions test/integration/image-future/default/pages/priority.js
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions test/integration/image-future/default/test/index.test.ts
Expand Up @@ -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()
Expand Down