Add experimental skipTrailingSlashRedirect

parent cb1da96e
...@@ -609,50 +609,52 @@ export default async function loadCustomRoutes( ...@@ -609,50 +609,52 @@ export default async function loadCustomRoutes(
) )
} }
if (config.trailingSlash) { if (!config.experimental?.skipTrailingSlashRedirect) {
redirects.unshift( if (config.trailingSlash) {
{ redirects.unshift(
source: '/:file((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/]+\\.\\w+)/', {
destination: '/:file', source: '/:file((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/]+\\.\\w+)/',
permanent: true, destination: '/:file',
locale: config.i18n ? false : undefined, permanent: true,
internal: true, locale: config.i18n ? false : undefined,
} as Redirect, internal: true,
{ } as Redirect,
source: '/:notfile((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/\\.]+)', {
destination: '/:notfile/', source: '/:notfile((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/\\.]+)',
permanent: true, destination: '/:notfile/',
locale: config.i18n ? false : undefined, permanent: true,
internal: true, locale: config.i18n ? false : undefined,
} as Redirect internal: true,
) } as Redirect
if (config.basePath) { )
redirects.unshift({ if (config.basePath) {
source: config.basePath, redirects.unshift({
destination: config.basePath + '/', source: config.basePath,
permanent: true, destination: config.basePath + '/',
basePath: false, permanent: true,
locale: config.i18n ? false : undefined, basePath: false,
internal: true, locale: config.i18n ? false : undefined,
} as Redirect) internal: true,
} } as Redirect)
} else { }
redirects.unshift({ } else {
source: '/:path+/',
destination: '/:path+',
permanent: true,
locale: config.i18n ? false : undefined,
internal: true,
} as Redirect)
if (config.basePath) {
redirects.unshift({ redirects.unshift({
source: config.basePath + '/', source: '/:path+/',
destination: config.basePath, destination: '/:path+',
permanent: true, permanent: true,
basePath: false,
locale: config.i18n ? false : undefined, locale: config.i18n ? false : undefined,
internal: true, internal: true,
} as Redirect) } as Redirect)
if (config.basePath) {
redirects.unshift({
source: config.basePath + '/',
destination: config.basePath,
permanent: true,
basePath: false,
locale: config.i18n ? false : undefined,
internal: true,
} as Redirect)
}
} }
} }
......
...@@ -355,6 +355,9 @@ const configSchema = { ...@@ -355,6 +355,9 @@ const configSchema = {
sharedPool: { sharedPool: {
type: 'boolean', type: 'boolean',
}, },
skipTrailingSlashRedirect: {
type: 'boolean',
},
swcFileReading: { swcFileReading: {
type: 'boolean', type: 'boolean',
}, },
......
...@@ -78,6 +78,7 @@ export interface NextJsWebpackConfig { ...@@ -78,6 +78,7 @@ export interface NextJsWebpackConfig {
} }
export interface ExperimentalConfig { export interface ExperimentalConfig {
skipTrailingSlashRedirect?: boolean
optimisticClientCache?: boolean optimisticClientCache?: boolean
legacyBrowsers?: boolean legacyBrowsers?: boolean
browsersListForSwc?: boolean browsersListForSwc?: boolean
......
import Link from 'next/link'
export default function Page(props) {
return (
<>
<p id="another">another page</p>
<Link href="/">
<a id="to-index">to index</a>
</Link>
<br />
</>
)
}
import Link from 'next/link'
export default function Page(props) {
return (
<>
<p id="blog">blog page</p>
<Link href="/">
<a id="to-index">to index</a>
</Link>
<br />
</>
)
}
import Link from 'next/link'
export default function Page(props) {
return (
<>
<p id="index">index page</p>
<Link href="/another">
<a id="to-another">to another</a>
</Link>
<br />
<Link href="/blog/first">
<a id="to-blog-first">to /blog/first</a>
</Link>
<br />
</>
)
}
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'
import { join } from 'path'
import webdriver from 'next-webdriver'
describe('skip-trailing-slash-redirect', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(join(__dirname, 'app')),
dependencies: {},
nextConfig: {
experimental: {
skipTrailingSlashRedirect: true,
},
async redirects() {
return [
{
source: '/redirect-me',
destination: '/another',
permanent: false,
},
]
},
async rewrites() {
return [
{
source: '/rewrite-me',
destination: '/another',
},
]
},
},
})
})
afterAll(() => next.destroy())
if ((global as any).isNextStart) {
it('should not have trailing slash redirects in manifest', async () => {
const routesManifest = JSON.parse(
await next.readFile('.next/routes-manifest.json')
)
expect(
routesManifest.redirects.some((redirect) => {
return (
redirect.statusCode === 308 &&
(redirect.destination === '/:path+' ||
redirect.destination === '/:path+/')
)
})
).toBe(false)
})
}
it('should apply config redirect correctly', async () => {
const res = await fetchViaHTTP(next.url, '/redirect-me', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(307)
expect(new URL(res.headers.get('location'), 'http://n').pathname).toBe(
'/another'
)
})
it('should apply config rewrites correctly', async () => {
const res = await fetchViaHTTP(next.url, '/rewrite-me', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain('another page')
})
it('should not apply trailing slash redirect (with slash)', async () => {
const res = await fetchViaHTTP(next.url, '/another/', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain('another page')
})
it('should not apply trailing slash redirect (without slash)', async () => {
const res = await fetchViaHTTP(next.url, '/another', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain('another page')
})
it('should respond to index correctly', async () => {
const res = await fetchViaHTTP(next.url, '/', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain('index page')
})
it('should respond to dynamic route correctly', async () => {
const res = await fetchViaHTTP(next.url, '/blog/first', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(await res.text()).toContain('blog page')
})
it('should navigate client side correctly', async () => {
const browser = await webdriver(next.url, '/')
expect(await browser.eval('location.pathname')).toBe('/')
await browser.elementByCss('#to-another').click()
await browser.waitForElementByCss('#another')
expect(await browser.eval('location.pathname')).toBe('/another')
await browser.back()
await browser.waitForElementByCss('#index')
expect(await browser.eval('location.pathname')).toBe('/')
await browser.elementByCss('#to-blog-first').click()
await browser.waitForElementByCss('#blog')
expect(await browser.eval('location.pathname')).toBe('/blog/first')
})
})
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