Commit 2139f66a authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Add test for duplicate scripts and add extra de-duping (#8709)

parent eb8c11bb
......@@ -480,7 +480,7 @@ export class NextScript extends Component<OriginProps> {
'!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()},!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();'
getDynamicChunks() {
const { dynamicImports, assetPrefix } = this.context._documentProps
const { dynamicImports, assetPrefix, files } = this.context._documentProps
const { _devOnlyInvalidateCacheQueryString } = this.context
return dedupe(dynamicImports).map((bundle: any) => {
......@@ -491,6 +491,8 @@ export class NextScript extends Component<OriginProps> {
: { noModule: true }
}
if (files.includes(bundle.file)) return null
return (
<script
async
......
import Link from 'next/link'
export default () => (
<Link href='/'>
<a>Hello world</a>
</Link>
)
import dynamic from 'next/dynamic'
const Hello = dynamic(() => import('../components/hello'))
export default () => (
<>
<Hello />
</>
)
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import cheerio from 'cheerio'
import {
nextBuild,
nextStart,
findPort,
killApp,
renderViaHTTP
} from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '../')
let appPort
let app
describe('De-dedupes scripts in _document', () => {
beforeAll(async () => {
appPort = await findPort()
await nextBuild(appDir)
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
it('Does not have duplicate script references', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
let foundDuplicate = false
const srcs = new Set()
for (const script of $('script').toArray()) {
const { src } = script.attribs
if (!src || !src.startsWith('/_next/static')) continue
if (srcs.has(src)) {
console.error(`Found duplicate script ${src}`)
foundDuplicate = true
}
srcs.add(src)
}
expect(foundDuplicate).toBe(false)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment