Skip to content

Commit 96d6ada

Browse files
syi0808devjiwonchoi
authored andcommittedAug 26, 2024
fix(next): add cross origin in react dom preload (#67423)
Co-authored-by: Jiwon Choi <devjiwonchoi@gmail.com>

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed
 

‎packages/next/src/client/script.tsx

+14-4
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,13 @@ function Script(props: ScriptProps): JSX.Element | null {
349349
ReactDOM.preload(
350350
src,
351351
restProps.integrity
352-
? { as: 'script', integrity: restProps.integrity, nonce }
353-
: { as: 'script', nonce }
352+
? {
353+
as: 'script',
354+
integrity: restProps.integrity,
355+
nonce,
356+
crossOrigin: restProps.crossOrigin,
357+
}
358+
: { as: 'script', nonce, crossOrigin: restProps.crossOrigin }
354359
)
355360
return (
356361
<script
@@ -370,8 +375,13 @@ function Script(props: ScriptProps): JSX.Element | null {
370375
ReactDOM.preload(
371376
src,
372377
restProps.integrity
373-
? { as: 'script', integrity: restProps.integrity, nonce }
374-
: { as: 'script', nonce }
378+
? {
379+
as: 'script',
380+
integrity: restProps.integrity,
381+
nonce,
382+
crossOrigin: restProps.crossOrigin,
383+
}
384+
: { as: 'script', nonce, crossOrigin: restProps.crossOrigin }
375385
)
376386
}
377387
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ReactNode } from 'react'
2+
export default function Root({ children }: { children: ReactNode }) {
3+
return (
4+
<html>
5+
<body>{children}</body>
6+
</html>
7+
)
8+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Script from 'next/script'
2+
3+
export default function Page() {
4+
return (
5+
<Script
6+
src="https://code.jquery.com/jquery-3.7.1.min.js"
7+
crossOrigin="use-credentials"
8+
/>
9+
)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('Script component with crossOrigin props', () => {
4+
const { next } = nextTestSetup({
5+
files: __dirname,
6+
})
7+
8+
it('should be set crossOrigin also in preload link tag', async () => {
9+
const browser = await next.browser('/')
10+
11+
const crossorigin = await browser
12+
.elementByCss('link[href="https://code.jquery.com/jquery-3.7.1.min.js"]')
13+
.getAttribute('crossorigin')
14+
15+
expect(crossorigin).toBe('use-credentials')
16+
})
17+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @type {import('next').NextConfig}
3+
*/
4+
const nextConfig = {}
5+
6+
module.exports = nextConfig

0 commit comments

Comments
 (0)
Please sign in to comment.