Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct URI Encoding Order #9638

Merged
merged 4 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/next/client/page-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function preloadScript(url) {
const link = document.createElement('link')
link.rel = 'preload'
link.crossOrigin = process.crossOrigin
link.href = encodeURI(url)
link.href = url
link.as = 'script'
document.head.appendChild(link)
}
Expand Down Expand Up @@ -44,7 +44,8 @@ export default class PageLoader {
// Returns a promise for the dependencies for a particular route
getDependencies(route) {
return this.promisedBuildManifest.then(
man => (man[route] && man[route].map(url => `/_next/${url}`)) || []
man =>
(man[route] && man[route].map(url => `/_next/${encodeURI(url)}`)) || []
)
}

Expand Down Expand Up @@ -122,7 +123,7 @@ export default class PageLoader {

const url = `${this.assetPrefix}/_next/static/${encodeURIComponent(
this.buildId
)}/pages${scriptRoute}`
)}/pages${encodeURI(scriptRoute)}`
this.loadScript(url, route, true)
}

Expand All @@ -135,7 +136,7 @@ export default class PageLoader {
if (isPage) url = url.replace(/\.js$/, '.module.js')
}
script.crossOrigin = process.crossOrigin
script.src = encodeURI(url)
script.src = url
script.onerror = () => {
const error = new Error(`Error loading script ${url}`)
error.code = 'PAGE_LOAD_ERROR'
Expand Down Expand Up @@ -194,9 +195,9 @@ export default class PageLoader {
this.assetPrefix +
(isDependency
? route
: `/_next/static/${encodeURIComponent(
this.buildId
)}/pages${scriptRoute}`)
: `/_next/static/${encodeURIComponent(this.buildId)}/pages${encodeURI(
scriptRoute
)}`)

// n.b. If preload is not supported, we fall back to `loadPage` which has
// its own deduping mechanism.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <p>Hello world</p>
19 changes: 19 additions & 0 deletions test/integration/preload-viewport/pages/multi-prefetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Link from 'next/link'
import { useEffect } from 'react'
import { useRouter } from 'next/router'

export default () => {
const router = useRouter()
useEffect(() => {
router.prefetch('/dynamic/[hello]')
router.prefetch('/dynamic/[hello]')
router.prefetch('/dynamic/[hello]')
}, [router])
return (
<div>
<Link prefetch={true} href="/dynamic/[hello]" as={'/dynamic/test'}>
<a>I should only be prefetched once</a>
</Link>
</div>
)
}
20 changes: 20 additions & 0 deletions test/integration/preload-viewport/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,24 @@ describe('Prefetching Links in viewport', () => {
}
expect(found).toBe(false)
})

it('should not duplicate prefetches', async () => {
const browser = await webdriver(appPort, '/multi-prefetch')
await waitFor(2 * 1000)

const links = await browser.elementsByCss('link[rel=preload]')

const hrefs = []
for (const link of links) {
const href = await link.getAttribute('href')
hrefs.push(href)
}
hrefs.sort()

// Ensure no duplicates
expect(hrefs).toEqual([...new Set(hrefs)])

// Verify encoding
expect(hrefs.some(e => e.includes(`%5Bhello%5D.js`))).toBe(true)
})
})