Skip to content

Commit

Permalink
fix(next): add cross origin in react dom preload (#67423)
Browse files Browse the repository at this point in the history
Co-authored-by: Jiwon Choi <devjiwonchoi@gmail.com>
  • Loading branch information
2 people authored and ztanner committed Aug 26, 2024
1 parent c572030 commit 96d6ada
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/next/src/client/script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,13 @@ function Script(props: ScriptProps): JSX.Element | null {
ReactDOM.preload(
src,
restProps.integrity
? { as: 'script', integrity: restProps.integrity, nonce }
: { as: 'script', nonce }
? {
as: 'script',
integrity: restProps.integrity,
nonce,
crossOrigin: restProps.crossOrigin,
}
: { as: 'script', nonce, crossOrigin: restProps.crossOrigin }
)
return (
<script
Expand All @@ -370,8 +375,13 @@ function Script(props: ScriptProps): JSX.Element | null {
ReactDOM.preload(
src,
restProps.integrity
? { as: 'script', integrity: restProps.integrity, nonce }
: { as: 'script', nonce }
? {
as: 'script',
integrity: restProps.integrity,
nonce,
crossOrigin: restProps.crossOrigin,
}
: { as: 'script', nonce, crossOrigin: restProps.crossOrigin }
)
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/app-dir/next-script/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
10 changes: 10 additions & 0 deletions test/e2e/app-dir/next-script/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Script from 'next/script'

export default function Page() {
return (
<Script
src="https://code.jquery.com/jquery-3.7.1.min.js"
crossOrigin="use-credentials"
/>
)
}
17 changes: 17 additions & 0 deletions test/e2e/app-dir/next-script/next-script.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { nextTestSetup } from 'e2e-utils'

describe('Script component with crossOrigin props', () => {
const { next } = nextTestSetup({
files: __dirname,
})

it('should be set crossOrigin also in preload link tag', async () => {
const browser = await next.browser('/')

const crossorigin = await browser
.elementByCss('link[href="https://code.jquery.com/jquery-3.7.1.min.js"]')
.getAttribute('crossorigin')

expect(crossorigin).toBe('use-credentials')
})
})
6 changes: 6 additions & 0 deletions test/e2e/app-dir/next-script/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig

0 comments on commit 96d6ada

Please sign in to comment.