Unverified Commit e360c105 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update prefetch check to prevent re-prefetching (#7654)

* Update page-loader prefetch checking

* Add test for re-prefetching same bundle
parent c03e94be
......@@ -165,8 +165,12 @@ export default class PageLoader {
async prefetch (route) {
route = this.normalizeRoute(route)
const scriptRoute = route === '/' ? '/index.js' : `${route}.js`
if (this.prefetchCache.has(scriptRoute)) {
const scriptRoute = `${route === '/' ? '/index' : route}.js`
if (
this.prefetchCache.has(scriptRoute) ||
document.getElementById(`__NEXT_PAGE__${route}`)
) {
return
}
this.prefetchCache.add(scriptRoute)
......
......@@ -148,6 +148,22 @@ describe('Flying Shuttle', () => {
}
})
it('should not re-prefetch for the page its already on', async () => {
const browser = await webdriver(appPort, '/')
const links = await browser.elementsByCss('link[rel=preload]')
let found = false
for (const link of links) {
const href = await link.getAttribute('href')
if (href.endsWith('index.js')) {
found = true
break
}
}
expect(found).toBe(false)
if (browser) await browser.close()
})
it('should render /index', async () => {
const html = await renderViaHTTP(secondAppPort, '/')
expect(html).toMatch(/omega lol/)
......
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