-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Joe Haddad <[email protected]> Co-authored-by: Tim Neutkens <[email protected]>
- Loading branch information
1 parent
b90fa0a
commit 2142b76
Showing
25 changed files
with
536 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
description: Configure Next.js pages to resolve with or without a 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. | ||
|
||
Open `next.config.js` and add the `trailingSlash` config: | ||
|
||
```js | ||
module.exports = { | ||
experimental: { | ||
trailingSlash: true, | ||
}, | ||
} | ||
``` | ||
|
||
With this option set, urls like `/about` will redirect to `/about/`. | ||
|
||
## Related | ||
|
||
<div class="card"> | ||
<a href="/docs/api-reference/next.config.js/introduction.md"> | ||
<b>Introduction to next.config.js:</b> | ||
<small>Learn more about the configuration file used by Next.js.</small> | ||
</a> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/next/next-server/lib/router/normalize-trailing-slash.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function normalizeTrailingSlash( | ||
path: string, | ||
requireSlash?: boolean | ||
): string { | ||
if (path === '/') { | ||
return path | ||
} else if (path.endsWith('/')) { | ||
return requireSlash ? path : path.slice(0, -1) | ||
} else { | ||
return requireSlash ? path + '/' : path | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
experimental: { | ||
// <placeholder> | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function NotFound() { | ||
return <div id="page-404">404</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useState, useEffect } from 'react' | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Page() { | ||
const [isMounted, setMounted] = useState(false) | ||
useEffect(() => { | ||
setMounted(true) | ||
}, []) | ||
const router = useRouter() | ||
return ( | ||
<div> | ||
{isMounted ? <div id="hydration-marker" /> : null} | ||
<div id="page-marker">/about.js</div> | ||
<div id="router-pathname">{router.pathname}</div> | ||
</div> | ||
) | ||
} |
17 changes: 17 additions & 0 deletions
17
test/integration/trailing-slashes/pages/catch-all/[...slug].js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useState, useEffect } from 'react' | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Page() { | ||
const [isMounted, setMounted] = useState(false) | ||
useEffect(() => { | ||
setMounted(true) | ||
}, []) | ||
const router = useRouter() | ||
return ( | ||
<div> | ||
{isMounted ? <div id="hydration-marker" /> : null} | ||
<div id="page-marker">/catch-all/[...slug].js</div> | ||
<div id="router-pathname">{router.pathname}</div> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useState, useEffect } from 'react' | ||
import { useRouter } from 'next/router' | ||
|
||
export default function Page() { | ||
const [isMounted, setMounted] = useState(false) | ||
useEffect(() => { | ||
setMounted(true) | ||
}, []) | ||
const router = useRouter() | ||
return ( | ||
<div> | ||
{isMounted ? <div id="hydration-marker" /> : null} | ||
<div id="page-marker">/index.js</div> | ||
<div id="router-pathname">{router.pathname}</div> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.