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

fix basepath trailing slash #15200

Merged
merged 4 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/client/normalize-trailing-slash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function removePathTrailingSlash(path: string): string {
* Normalizes the trailing slash of a path according to the `trailingSlash` option
* in `next.config.js`.
*/
const normalizePathTrailingSlash = process.env.__NEXT_TRAILING_SLASH
export const normalizePathTrailingSlash = process.env.__NEXT_TRAILING_SLASH
? (path: string): string => {
if (/\.[^/]+\/?$/.test(path)) {
return removePathTrailingSlash(path)
Expand Down
7 changes: 6 additions & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ import { parseRelativeUrl } from './utils/parse-relative-url'
import {
normalizeTrailingSlash,
removePathTrailingSlash,
normalizePathTrailingSlash,
} from '../../../client/normalize-trailing-slash'

const basePath = (process.env.__NEXT_ROUTER_BASEPATH as string) || ''

export function addBasePath(path: string): string {
return basePath ? (path === '/' ? basePath : basePath + path) : path
return basePath
? path === '/'
? normalizePathTrailingSlash(basePath)
: basePath + path
: path
}

export function delBasePath(path: string): string {
Expand Down
1 change: 1 addition & 0 deletions test/integration/trailing-slashes/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
experimental: {
// <placeholder>
},
// basePath: '/docs',
Timer marked this conversation as resolved.
Show resolved Hide resolved
}
50 changes: 30 additions & 20 deletions test/integration/trailing-slashes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ function testShouldResolve(expectations) {
function testLinkShouldRewriteTo(expectations) {
it.each(expectations)(
'%s should have href %s',
async (href, expectedHref) => {
const content = await renderViaHTTP(appPort, `/linker?href=${href}`)
async (linkPage, expectedHref) => {
const content = await renderViaHTTP(appPort, linkPage)
const $ = cheerio.load(content)
expect($('#link').attr('href')).toBe(expectedHref)
}
)

it.each(expectations)(
'%s should navigate to %s',
async (href, expectedHref) => {
async (linkPage, expectedHref) => {
let browser
try {
browser = await webdriver(appPort, `/linker?href=${href}`)
browser = await webdriver(appPort, linkPage)
await browser.elementByCss('#link').click()

await browser.waitForElementByCss('#hydration-marker')
Expand All @@ -97,10 +97,10 @@ function testLinkShouldRewriteTo(expectations) {

it.each(expectations)(
'%s should push route to %s',
async (href, expectedHref) => {
async (linkPage, expectedHref) => {
let browser
try {
browser = await webdriver(appPort, `/linker?href=${href}`)
browser = await webdriver(appPort, linkPage)
await browser.elementByCss('#route-pusher').click()

await browser.waitForElementByCss('#hydration-marker')
Expand Down Expand Up @@ -134,13 +134,13 @@ function testWithoutTrailingSlash() {
])

testLinkShouldRewriteTo([
['/', '/'],
['/about', '/about'],
['/about/', '/about'],
['/about?hello=world', '/about?hello=world'],
['/about/?hello=world', '/about?hello=world'],
['/catch-all/hello/', '/catch-all/hello'],
['/catch-all/hello.world/', '/catch-all/hello.world'],
['/linker?href=/', '/'],
['/linker?href=/about', '/about'],
['/linker?href=/about/', '/about'],
['/linker?href=/about?hello=world', '/about?hello=world'],
['/linker?href=/about/?hello=world', '/about?hello=world'],
['/linker?href=/catch-all/hello/', '/catch-all/hello'],
['/linker?href=/catch-all/hello.world/', '/catch-all/hello.world'],
])
}

Expand All @@ -164,13 +164,13 @@ function testWithTrailingSlash() {
])

testLinkShouldRewriteTo([
['/', '/'],
['/about', '/about/'],
['/about/', '/about/'],
['/about?hello=world', '/about/?hello=world'],
['/about/?hello=world', '/about/?hello=world'],
['/catch-all/hello/', '/catch-all/hello/'],
['/catch-all/hello.world/', '/catch-all/hello.world'],
['/linker?href=/', '/'],
['/linker?href=/about', '/about/'],
['/linker?href=/about/', '/about/'],
['/linker?href=/about?hello=world', '/about/?hello=world'],
['/linker?href=/about/?hello=world', '/about/?hello=world'],
['/linker?href=/catch-all/hello/', '/catch-all/hello/'],
['/linker?href=/catch-all/hello.world/', '/catch-all/hello.world'],
])
}

Expand Down Expand Up @@ -318,6 +318,11 @@ describe('Trailing slashes', () => {
['/docs/catch-all/hello/world', '/docs/catch-all/hello/world/'],
['/docs/catch-all/hello.world/', '/docs/catch-all/hello.world'],
])

testLinkShouldRewriteTo([
['/docs/linker?href=/about', '/docs/about/'],
['/docs/linker?href=/', '/docs/'],
])
})

describe('production mode, with basepath, trailingSlash: true', () => {
Expand Down Expand Up @@ -346,5 +351,10 @@ describe('Trailing slashes', () => {
['/docs/catch-all/hello/world', '/docs/catch-all/hello/world/'],
['/docs/catch-all/hello.world/', '/docs/catch-all/hello.world'],
])

testLinkShouldRewriteTo([
['/docs/linker?href=/about', '/docs/about/'],
['/docs/linker?href=/', '/docs/'],
])
})
})