forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const metadata = { | ||
title: 'Next.js', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
export default function RootLayout({ children }) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Index(props) { | ||
return <p id="title">IndexPage</p> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
/** | ||
* The "assetPrefix" here doesn't needs to be real as we doesn't load the page in the browser in this test, | ||
* we only care about if all assets prefixed with the "assetPrefix" are having correct "crossOrigin". | ||
*/ | ||
assetPrefix: 'https://example.vercel.sh', | ||
|
||
/** | ||
* According to HTML5 Spec (https://html.spec.whatwg.org/multipage/urls-and-fetching.html#cors-settings-attributes), | ||
* crossorigin="" and crossorigin="anonymous" has the same effect. And ReactDOM's preload methods (preload, preconnect, etc.) | ||
* will prefer crossorigin="" to save bytes. | ||
* | ||
* So we use "use-credentials" here for easier testing. | ||
*/ | ||
crossOrigin: 'use-credentials', | ||
} |
50 changes: 50 additions & 0 deletions
50
test/integration/app-config-crossorigin/test/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* eslint-env jest */ | ||
import { join } from 'path' | ||
import { | ||
killApp, | ||
findPort, | ||
launchApp, | ||
renderViaHTTP, | ||
File, | ||
} from 'next-test-utils' | ||
import cheerio from 'cheerio' | ||
|
||
const appDir = join(__dirname, '../') | ||
|
||
describe('App crossOrigin config', () => { | ||
let appPort | ||
let app | ||
|
||
it('should render correctly with assetPrefix: "/"', async () => { | ||
try { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
|
||
const html = await renderViaHTTP(appPort, '/') | ||
|
||
const $ = cheerio.load(html) | ||
|
||
// Only potential external (assetPrefix) <script /> and <link /> should have crossorigin attribute | ||
$( | ||
'script[src*="https://example.vercel.sh"], link[href*="https://example.vercel.sh"]' | ||
).each((_, el) => { | ||
const crossOrigin = $(el).attr('crossorigin') | ||
expect(crossOrigin).toBe('use-credentials') | ||
}) | ||
|
||
// Inline <script /> (including RSC payload) and <link /> should not have crossorigin attribute | ||
$('script:not([src]), link:not([href])').each((_, el) => { | ||
const crossOrigin = $(el).attr('crossorigin') | ||
expect(crossOrigin).toBeUndefined() | ||
}) | ||
|
||
// Same origin <script /> and <link /> should not have crossorigin attribute either | ||
$('script[src^="/"], link[href^="/"]').each((_, el) => { | ||
const crossOrigin = $(el).attr('crossorigin') | ||
expect(crossOrigin).toBeUndefined() | ||
}) | ||
} finally { | ||
killApp(app) | ||
} | ||
}) | ||
}) |