Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
bf5e295c
Commit
bf5e295c
authored
Dec 05, 2019
by
Joe Haddad
Committed by
JJ Kasper
Dec 05, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Correct URI Encoding Order (#9638)
* Correct URI Encoding Order * Add prefetch test * verify encoding
parent
926a4e5a
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
7 deletions
+48
-7
page-loader.js
packages/next/client/page-loader.js
+8
-7
[hello].js
test/integration/preload-viewport/pages/dynamic/[hello].js
+1
-0
multi-prefetch.js
test/integration/preload-viewport/pages/multi-prefetch.js
+19
-0
index.test.js
test/integration/preload-viewport/test/index.test.js
+20
-0
No files found.
packages/next/client/page-loader.js
View file @
bf5e295c
...
@@ -15,7 +15,7 @@ function preloadScript(url) {
...
@@ -15,7 +15,7 @@ function preloadScript(url) {
const
link
=
document
.
createElement
(
'link'
)
const
link
=
document
.
createElement
(
'link'
)
link
.
rel
=
'preload'
link
.
rel
=
'preload'
link
.
crossOrigin
=
process
.
crossOrigin
link
.
crossOrigin
=
process
.
crossOrigin
link
.
href
=
encodeURI
(
url
)
link
.
href
=
url
link
.
as
=
'script'
link
.
as
=
'script'
document
.
head
.
appendChild
(
link
)
document
.
head
.
appendChild
(
link
)
}
}
...
@@ -44,7 +44,8 @@ export default class PageLoader {
...
@@ -44,7 +44,8 @@ export default class PageLoader {
// Returns a promise for the dependencies for a particular route
// Returns a promise for the dependencies for a particular route
getDependencies
(
route
)
{
getDependencies
(
route
)
{
return
this
.
promisedBuildManifest
.
then
(
return
this
.
promisedBuildManifest
.
then
(
man
=>
(
man
[
route
]
&&
man
[
route
].
map
(
url
=>
`/_next/
${
url
}
`
))
||
[]
man
=>
(
man
[
route
]
&&
man
[
route
].
map
(
url
=>
`/_next/
${
encodeURI
(
url
)}
`
))
||
[]
)
)
}
}
...
@@ -122,7 +123,7 @@ export default class PageLoader {
...
@@ -122,7 +123,7 @@ export default class PageLoader {
const
url
=
`
${
this
.
assetPrefix
}
/_next/static/
${
encodeURIComponent
(
const
url
=
`
${
this
.
assetPrefix
}
/_next/static/
${
encodeURIComponent
(
this
.
buildId
this
.
buildId
)}
/pages
${
scriptRoute
}
`
)}
/pages
${
encodeURI
(
scriptRoute
)
}
`
this
.
loadScript
(
url
,
route
,
true
)
this
.
loadScript
(
url
,
route
,
true
)
}
}
...
@@ -135,7 +136,7 @@ export default class PageLoader {
...
@@ -135,7 +136,7 @@ export default class PageLoader {
if
(
isPage
)
url
=
url
.
replace
(
/
\.
js$/
,
'.module.js'
)
if
(
isPage
)
url
=
url
.
replace
(
/
\.
js$/
,
'.module.js'
)
}
}
script
.
crossOrigin
=
process
.
crossOrigin
script
.
crossOrigin
=
process
.
crossOrigin
script
.
src
=
encodeURI
(
url
)
script
.
src
=
url
script
.
onerror
=
()
=>
{
script
.
onerror
=
()
=>
{
const
error
=
new
Error
(
`Error loading script
${
url
}
`
)
const
error
=
new
Error
(
`Error loading script
${
url
}
`
)
error
.
code
=
'PAGE_LOAD_ERROR'
error
.
code
=
'PAGE_LOAD_ERROR'
...
@@ -194,9 +195,9 @@ export default class PageLoader {
...
@@ -194,9 +195,9 @@ export default class PageLoader {
this
.
assetPrefix
+
this
.
assetPrefix
+
(
isDependency
(
isDependency
?
route
?
route
:
`/_next/static/
${
encodeURIComponent
(
:
`/_next/static/
${
encodeURIComponent
(
this
.
buildId
)}
/pages
${
encodeURI
(
this
.
buildId
scriptRoute
)}
/pages
${
scriptRoute
}
`
)
)}
`
)
// n.b. If preload is not supported, we fall back to `loadPage` which has
// n.b. If preload is not supported, we fall back to `loadPage` which has
// its own deduping mechanism.
// its own deduping mechanism.
...
...
test/integration/preload-viewport/pages/dynamic/[hello].js
0 → 100644
View file @
bf5e295c
export
default
()
=>
<
p
>
Hello
world
<
/p
>
test/integration/preload-viewport/pages/multi-prefetch.js
0 → 100644
View file @
bf5e295c
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
>
)
}
test/integration/preload-viewport/test/index.test.js
View file @
bf5e295c
...
@@ -138,4 +138,24 @@ describe('Prefetching Links in viewport', () => {
...
@@ -138,4 +138,24 @@ describe('Prefetching Links in viewport', () => {
}
}
expect
(
found
).
toBe
(
false
)
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
)
})
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment