Unverified Commit 8277d4d5 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure multi-level basePath works properly (#18715)

This ensures the `basePath` property works correctly when a multi-level value is defined (`/hello/world`)

Fixes: https://github.com/vercel/next.js/issues/17889
parent 2b94b1ee
...@@ -970,8 +970,19 @@ export default class Server { ...@@ -970,8 +970,19 @@ export default class Server {
// if basePath is defined require it be present // if basePath is defined require it be present
if (basePath) { if (basePath) {
if (pathParts[0] !== basePath.substr(1)) return { finished: false } const basePathParts = basePath.split('/')
pathParts.shift() // 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('/')}` const path = `/${pathParts.join('/')}`
......
import React from 'react' import React from 'react'
import Link from 'next/link' import Link from 'next/link'
import { useRouter } from 'next/router'
export async function getServerSideProps({ query: { port } }) { export async function getServerSideProps({ query: { port } }) {
if (!port) { if (!port) {
...@@ -9,10 +10,14 @@ export async function getServerSideProps({ query: { port } }) { ...@@ -9,10 +10,14 @@ export async function getServerSideProps({ query: { port } }) {
} }
export default function Page({ port }) { export default function Page({ port }) {
const router = useRouter()
return ( return (
<> <>
<Link href={`http://localhost:${port}/docs/something-else`}> <Link href={`http://localhost:${port}${router.basePath}/something-else`}>
<a id="absolute-link">http://localhost:{port}/docs/something-else</a> <a id="absolute-link">
http://localhost:{port}
{router.basePath}/something-else
</a>
</Link> </Link>
</> </>
) )
......
import Link from 'next/link' import Link from 'next/link'
import { useRouter } from 'next/router'
export default () => ( export default () => (
<> <>
<Link href="/docs/other-page"> <Link href={`${useRouter().basePath}/other-page`}>
<a id="other-page-link"> <a id="other-page-link">
<h1>Hello World</h1> <h1>Hello World</h1>
</a> </a>
......
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