Skip to content

Commit

Permalink
Ensure multi-level basePath works properly (#18715)
Browse files Browse the repository at this point in the history
This ensures the `basePath` property works correctly when a multi-level value is defined (`/hello/world`)

Fixes: #17889
  • Loading branch information
ijjk committed Nov 3, 2020
1 parent 2b94b1e commit 8277d4d
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 444 deletions.
15 changes: 13 additions & 2 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,19 @@ export default class Server {

// if basePath is defined require it be present
if (basePath) {
if (pathParts[0] !== basePath.substr(1)) return { finished: false }
pathParts.shift()
const basePathParts = basePath.split('/')
// remove first empty value
basePathParts.shift()

if (
!basePathParts.every((part: string, idx: number) => {
return part === pathParts[idx]
})
) {
return { finished: false }
}

pathParts.splice(0, basePathParts.length)
}

const path = `/${pathParts.join('/')}`
Expand Down
9 changes: 7 additions & 2 deletions test/integration/basepath/pages/absolute-url-basepath.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'

export async function getServerSideProps({ query: { port } }) {
if (!port) {
Expand All @@ -9,10 +10,14 @@ export async function getServerSideProps({ query: { port } }) {
}

export default function Page({ port }) {
const router = useRouter()
return (
<>
<Link href={`http://localhost:${port}/docs/something-else`}>
<a id="absolute-link">http://localhost:{port}/docs/something-else</a>
<Link href={`http://localhost:${port}${router.basePath}/something-else`}>
<a id="absolute-link">
http://localhost:{port}
{router.basePath}/something-else
</a>
</Link>
</>
)
Expand Down
3 changes: 2 additions & 1 deletion test/integration/basepath/pages/invalid-manual-basepath.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Link from 'next/link'
import { useRouter } from 'next/router'

export default () => (
<>
<Link href="/docs/other-page">
<Link href={`${useRouter().basePath}/other-page`}>
<a id="other-page-link">
<h1>Hello World</h1>
</a>
Expand Down

0 comments on commit 8277d4d

Please sign in to comment.