Unverified Commit a11d9939 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Stabilize Trailing Slash API (#15331)

Closes #15330
parent 047b73b8
...@@ -4,18 +4,13 @@ description: Configure Next.js pages to resolve with or without a trailing slash ...@@ -4,18 +4,13 @@ description: Configure Next.js pages to resolve with or without a trailing slash
# Trailing Slash # Trailing Slash
> **Warning**: This feature is **experimental and may not work as expected**.
> You must enable the `trailingSlash` experimental option to try it.
By default Next.js will redirect urls with trailing slashes to their counterpart without a trailing slash. For example `/about/` will redirect to `/about`. You can configure this behavior to act the opposite way, where urls without trailing slashes are redirected to their counterparts with trailing slashes. By default Next.js will redirect urls with trailing slashes to their counterpart without a trailing slash. For example `/about/` will redirect to `/about`. You can configure this behavior to act the opposite way, where urls without trailing slashes are redirected to their counterparts with trailing slashes.
Open `next.config.js` and add the `trailingSlash` config: Open `next.config.js` and add the `trailingSlash` config:
```js ```js
module.exports = { module.exports = {
experimental: { trailingSlash: true,
trailingSlash: true,
},
} }
``` ```
......
...@@ -839,7 +839,7 @@ export default async function getBaseWebpackConfig( ...@@ -839,7 +839,7 @@ export default async function getBaseWebpackConfig(
} }
: {}), : {}),
'process.env.__NEXT_TRAILING_SLASH': JSON.stringify( 'process.env.__NEXT_TRAILING_SLASH': JSON.stringify(
config.experimental.trailingSlash config.trailingSlash
), ),
'process.env.__NEXT_EXPORT_TRAILING_SLASH': JSON.stringify( 'process.env.__NEXT_EXPORT_TRAILING_SLASH': JSON.stringify(
config.exportTrailingSlash config.exportTrailingSlash
......
...@@ -357,7 +357,7 @@ export default async function loadCustomRoutes( ...@@ -357,7 +357,7 @@ export default async function loadCustomRoutes(
loadRedirects(config), loadRedirects(config),
]) ])
if (config.experimental.trailingSlash) { if (config.trailingSlash) {
redirects.unshift( redirects.unshift(
{ {
source: '/:path*/:file.:ext/', source: '/:path*/:file.:ext/',
......
...@@ -37,8 +37,8 @@ const defaultConfig: { [key: string]: any } = { ...@@ -37,8 +37,8 @@ const defaultConfig: { [key: string]: any } = {
basePath: '', basePath: '',
exportTrailingSlash: false, exportTrailingSlash: false,
sassOptions: {}, sassOptions: {},
trailingSlash: false,
experimental: { experimental: {
trailingSlash: false,
cpus: Math.max( cpus: Math.max(
1, 1,
(Number(process.env.CIRCLE_NODE_TOTAL) || (Number(process.env.CIRCLE_NODE_TOTAL) ||
......
module.exports = { module.exports = {
experimental: { // trailingSlash: boolean,
// <placeholder>
},
// basePath: '/docs', // basePath: '/docs',
} }
...@@ -178,7 +178,7 @@ function testWithTrailingSlash() { ...@@ -178,7 +178,7 @@ function testWithTrailingSlash() {
describe('Trailing slashes', () => { describe('Trailing slashes', () => {
describe('dev mode, trailingSlash: false', () => { describe('dev mode, trailingSlash: false', () => {
beforeAll(async () => { beforeAll(async () => {
nextConfig.replace('// <placeholder>', 'trailingSlash: false') nextConfig.replace('// trailingSlash: boolean', 'trailingSlash: false')
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort) app = await launchApp(appDir, appPort)
}) })
...@@ -192,7 +192,7 @@ describe('Trailing slashes', () => { ...@@ -192,7 +192,7 @@ describe('Trailing slashes', () => {
describe('dev mode, trailingSlash: true', () => { describe('dev mode, trailingSlash: true', () => {
beforeAll(async () => { beforeAll(async () => {
nextConfig.replace('// <placeholder>', 'trailingSlash: true') nextConfig.replace('// trailingSlash: boolean', 'trailingSlash: true')
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort) app = await launchApp(appDir, appPort)
}) })
...@@ -206,7 +206,7 @@ describe('Trailing slashes', () => { ...@@ -206,7 +206,7 @@ describe('Trailing slashes', () => {
describe('production mode, trailingSlash: false', () => { describe('production mode, trailingSlash: false', () => {
beforeAll(async () => { beforeAll(async () => {
nextConfig.replace('// <placeholder>', 'trailingSlash: false') nextConfig.replace('// trailingSlash: boolean', 'trailingSlash: false')
await nextBuild(appDir) await nextBuild(appDir)
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort)
...@@ -238,7 +238,7 @@ describe('Trailing slashes', () => { ...@@ -238,7 +238,7 @@ describe('Trailing slashes', () => {
describe('production mode, trailingSlash: true', () => { describe('production mode, trailingSlash: true', () => {
beforeAll(async () => { beforeAll(async () => {
nextConfig.replace('// <placeholder>', 'trailingSlash: true') nextConfig.replace('// trailingSlash: boolean', 'trailingSlash: true')
await nextBuild(appDir) await nextBuild(appDir)
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort)
...@@ -275,7 +275,7 @@ describe('Trailing slashes', () => { ...@@ -275,7 +275,7 @@ describe('Trailing slashes', () => {
describe('dev mode, with basepath, trailingSlash: true', () => { describe('dev mode, with basepath, trailingSlash: true', () => {
beforeAll(async () => { beforeAll(async () => {
nextConfig.replace('// <placeholder>', 'trailingSlash: true') nextConfig.replace('// trailingSlash: boolean', 'trailingSlash: true')
nextConfig.replace('// basePath:', 'basePath:') nextConfig.replace('// basePath:', 'basePath:')
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort) app = await launchApp(appDir, appPort)
...@@ -300,7 +300,7 @@ describe('Trailing slashes', () => { ...@@ -300,7 +300,7 @@ describe('Trailing slashes', () => {
describe('production mode, with basepath, trailingSlash: true', () => { describe('production mode, with basepath, trailingSlash: true', () => {
beforeAll(async () => { beforeAll(async () => {
nextConfig.replace('// <placeholder>', 'trailingSlash: true') nextConfig.replace('// trailingSlash: boolean', 'trailingSlash: true')
nextConfig.replace('// basePath:', 'basePath:') nextConfig.replace('// basePath:', 'basePath:')
await nextBuild(appDir) await nextBuild(appDir)
appPort = await findPort() appPort = await findPort()
......
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