Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure multi-level basePath works properly #18715

Merged
merged 3 commits into from Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/next/next-server/server/next-server.ts
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
@@ -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
@@ -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