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 link to file url behavior with trailingSlash #14681

Merged
merged 6 commits into from
Jun 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions packages/next/lib/load-custom-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,26 @@ export default async function loadCustomRoutes(
])

redirects.unshift(
config.experimental.trailingSlash
? {
source: '/:path+',
destination: '/:path+/',
permanent: true,
}
: {
source: '/:path+/',
destination: '/:path+',
permanent: true,
}
...(config.experimental.trailingSlash
? [
{
source: '/:path*/:file.:ext/',
destination: '/:path*/:file.:ext',
permanent: true,
},
{
source: '/:path*/:notfile([^/.]+)',
destination: '/:path*/:notfile/',
permanent: true,
},
]
: [
{
source: '/:path+/',
destination: '/:path+',
permanent: true,
},
])
)

return {
Expand Down
18 changes: 13 additions & 5 deletions packages/next/next-server/lib/router/normalize-trailing-slash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ export function normalizeTrailingSlash(
path: string,
requireSlash?: boolean
): string {
if (path === '/') {
return path
} else if (path.endsWith('/')) {
return requireSlash ? path : path.slice(0, -1)
if (requireSlash) {
if (!path.endsWith('/') && !/\.[^/]+$/.test(path)) {
return path + '/'
} else if (/\.[^/]+\/$/.test(path)) {
return path.slice(0, -1)
} else {
return path
}
} else {
return requireSlash ? path + '/' : path
if (path.endsWith('/') && path !== '/') {
return path.slice(0, -1)
} else {
return path
}
}
}
15 changes: 13 additions & 2 deletions test/integration/trailing-slashes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function testWithoutTrailingSlash() {
testShouldRedirect([
['/about/', '/about'],
['/catch-all/hello/world/', '/catch-all/hello/world'],
['/catch-all/hello.world/', '/catch-all/hello.world'],
])

testShouldResolve([
Expand All @@ -138,13 +139,16 @@ function testWithoutTrailingSlash() {
['/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'],
])
}

function testWithTrailingSlash() {
testShouldRedirect([
['/about', '/about/'],
['/catch-all/hello/world', '/catch-all/hello/world/'],
['/catch-all/hello.world/', '/catch-all/hello.world'],
])

testShouldResolve([
Expand All @@ -165,6 +169,8 @@ function testWithTrailingSlash() {
['/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'],
])
}

Expand Down Expand Up @@ -273,8 +279,13 @@ describe('Trailing slashes', () => {
expect.objectContaining({
redirects: expect.arrayContaining([
expect.objectContaining({
source: '/:path+',
destination: '/:path+/',
source: '/:path*/:file.:ext/',
destination: '/:path*/:file.:ext',
statusCode: 308,
}),
expect.objectContaining({
source: '/:path*/:notfile([^/.]+)',
destination: '/:path*/:notfile/',
statusCode: 308,
}),
]),
Expand Down