未验证 提交 8277d4d5 编写于 作者: J JJ Kasper 提交者: 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
上级 2b94b1ee
......@@ -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('/')}`
......
import React from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'
export async function getServerSideProps({ query: { port } }) {
if (!port) {
......@@ -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>
</>
)
......
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>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册