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
1b033423
Unverified
Commit
1b033423
authored
Aug 01, 2020
by
Jan Potoms
Committed by
GitHub
Aug 01, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix asPath of rewrite without basePath (#15760)
Fixes
https://github.com/vercel/next.js/issues/15755
parent
63667275
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
12 deletions
+60
-12
index.js
packages/next/client/index.js
+2
-10
router.ts
packages/next/next-server/lib/router/router.ts
+5
-2
index.test.js
test/integration/basepath/test/index.test.js
+53
-0
No files found.
packages/next/client/index.js
View file @
1b033423
...
@@ -9,7 +9,7 @@ import { RouterContext } from '../next-server/lib/router-context'
...
@@ -9,7 +9,7 @@ import { RouterContext } from '../next-server/lib/router-context'
import
{
isDynamicRoute
}
from
'../next-server/lib/router/utils/is-dynamic'
import
{
isDynamicRoute
}
from
'../next-server/lib/router/utils/is-dynamic'
import
*
as
envConfig
from
'../next-server/lib/runtime-config'
import
*
as
envConfig
from
'../next-server/lib/runtime-config'
import
{
getURL
,
loadGetInitialProps
,
ST
}
from
'../next-server/lib/utils'
import
{
getURL
,
loadGetInitialProps
,
ST
}
from
'../next-server/lib/utils'
import
{
delBasePath
}
from
'../next-server/lib/router/router'
import
{
hasBasePath
,
delBasePath
}
from
'../next-server/lib/router/router'
import
initHeadManager
from
'./head-manager'
import
initHeadManager
from
'./head-manager'
import
PageLoader
from
'./page-loader'
import
PageLoader
from
'./page-loader'
import
measureWebVitals
from
'./performance-relayer'
import
measureWebVitals
from
'./performance-relayer'
...
@@ -52,15 +52,7 @@ envConfig.setConfig({
...
@@ -52,15 +52,7 @@ envConfig.setConfig({
let
asPath
=
getURL
()
let
asPath
=
getURL
()
// make sure not to attempt stripping basePath for 404s
// make sure not to attempt stripping basePath for 404s
if
(
if
(
hasBasePath
(
asPath
))
{
page
!==
'/404'
&&
!
(
page
===
'/_error'
&&
hydrateProps
&&
hydrateProps
.
pageProps
&&
hydrateProps
.
pageProps
.
statusCode
===
404
)
)
{
asPath
=
delBasePath
(
asPath
)
asPath
=
delBasePath
(
asPath
)
}
}
...
...
packages/next/next-server/lib/router/router.ts
View file @
1b033423
...
@@ -30,6 +30,10 @@ function buildCancellationError() {
...
@@ -30,6 +30,10 @@ function buildCancellationError() {
})
})
}
}
export
function
hasBasePath
(
path
:
string
):
boolean
{
return
path
===
basePath
||
path
.
startsWith
(
basePath
+
'/'
)
}
export
function
addBasePath
(
path
:
string
):
string
{
export
function
addBasePath
(
path
:
string
):
string
{
return
basePath
return
basePath
?
path
===
'/'
?
path
===
'/'
...
@@ -269,7 +273,6 @@ export default class Router implements BaseRouter {
...
@@ -269,7 +273,6 @@ export default class Router implements BaseRouter {
if
(
as
.
substr
(
0
,
2
)
!==
'//'
)
{
if
(
as
.
substr
(
0
,
2
)
!==
'//'
)
{
// in order for `e.state` to work on the `onpopstate` event
// in order for `e.state` to work on the `onpopstate` event
// we have to register the initial route upon initialization
// we have to register the initial route upon initialization
this
.
changeState
(
this
.
changeState
(
'replaceState'
,
'replaceState'
,
formatWithValidation
({
pathname
:
addBasePath
(
pathname
),
query
}),
formatWithValidation
({
pathname
:
addBasePath
(
pathname
),
query
}),
...
@@ -450,7 +453,7 @@ export default class Router implements BaseRouter {
...
@@ -450,7 +453,7 @@ export default class Router implements BaseRouter {
this
.
abortComponentLoad
(
this
.
_inFlightRoute
)
this
.
abortComponentLoad
(
this
.
_inFlightRoute
)
}
}
const
cleanedAs
=
delBasePath
(
as
)
const
cleanedAs
=
hasBasePath
(
as
)
?
delBasePath
(
as
)
:
as
this
.
_inFlightRoute
=
as
this
.
_inFlightRoute
=
as
// If the url change is only related to a hash change
// If the url change is only related to a hash change
...
...
test/integration/basepath/test/index.test.js
View file @
1b033423
...
@@ -141,6 +141,38 @@ const runTests = (context, dev = false) => {
...
@@ -141,6 +141,38 @@ const runTests = (context, dev = false) => {
expect
(
html
).
toContain
(
'getServerSideProps'
)
expect
(
html
).
toContain
(
'getServerSideProps'
)
})
})
it
(
'should have correct asPath for rewrite without basePath'
,
async
()
=>
{
const
browser
=
await
webdriver
(
context
.
appPort
,
'/rewrite-no-basePath'
)
expect
(
await
browser
.
eval
(()
=>
window
.
location
.
pathname
)).
toBe
(
'/rewrite-no-basePath'
)
expect
(
await
browser
.
eval
(()
=>
window
.
next
.
router
.
asPath
)).
toBe
(
'/rewrite-no-basePath'
)
expect
(
await
browser
.
eval
(()
=>
window
.
next
.
router
.
pathname
)).
toBe
(
'/gssp'
)
})
it
(
'should have correct asPath for rewrite without basePath on back()'
,
async
()
=>
{
const
browser
=
await
webdriver
(
context
.
appPort
,
'/rewrite-no-basePath'
)
await
browser
.
eval
(()
=>
(
window
.
navigationMarker
=
true
))
await
browser
.
eval
(()
=>
window
.
next
.
router
.
push
(
'/hello'
))
await
check
(
()
=>
browser
.
eval
(()
=>
window
.
location
.
pathname
),
'/docs/hello'
)
await
browser
.
back
()
await
check
(
()
=>
browser
.
eval
(()
=>
window
.
location
.
pathname
),
'/rewrite-no-basePath'
)
await
check
(
()
=>
browser
.
eval
(()
=>
window
.
next
.
router
.
asPath
),
'/rewrite-no-basePath'
)
expect
(
await
browser
.
eval
(()
=>
window
.
next
.
router
.
pathname
)).
toBe
(
'/gssp'
)
expect
(
await
browser
.
eval
(()
=>
window
.
navigationMarker
)).
toBe
(
true
)
})
it
(
'should redirect with basePath by default'
,
async
()
=>
{
it
(
'should redirect with basePath by default'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
const
res
=
await
fetchViaHTTP
(
context
.
appPort
,
context
.
appPort
,
...
@@ -216,6 +248,27 @@ const runTests = (context, dev = false) => {
...
@@ -216,6 +248,27 @@ const runTests = (context, dev = false) => {
expect
(
pathname
).
toBe
(
'/missing'
)
expect
(
pathname
).
toBe
(
'/missing'
)
})
})
it
(
'should handle 404 urls that start with basePath'
,
async
()
=>
{
const
browser
=
await
webdriver
(
context
.
appPort
,
'/docshello'
)
expect
(
await
browser
.
eval
(()
=>
window
.
next
.
router
.
asPath
)).
toBe
(
'/docshello'
)
expect
(
await
browser
.
eval
(()
=>
window
.
location
.
pathname
)).
toBe
(
'/docshello'
)
})
it
(
'should navigating back to a non-basepath 404 that starts with basepath'
,
async
()
=>
{
const
browser
=
await
webdriver
(
context
.
appPort
,
'/docshello'
)
await
browser
.
eval
(()
=>
window
.
next
.
router
.
push
(
'/hello'
))
await
browser
.
waitForElementByCss
(
'#pathname'
)
await
browser
.
back
()
check
(()
=>
browser
.
eval
(()
=>
window
.
location
.
pathname
),
'/docshello'
)
expect
(
await
browser
.
eval
(()
=>
window
.
next
.
router
.
asPath
)).
toBe
(
'/docshello'
)
})
it
(
'should update dynamic params after mount correctly'
,
async
()
=>
{
it
(
'should update dynamic params after mount correctly'
,
async
()
=>
{
const
browser
=
await
webdriver
(
context
.
appPort
,
'/docs/hello-dynamic'
)
const
browser
=
await
webdriver
(
context
.
appPort
,
'/docs/hello-dynamic'
)
const
text
=
await
browser
.
elementByCss
(
'#slug'
).
text
()
const
text
=
await
browser
.
elementByCss
(
'#slug'
).
text
()
...
...
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