Skip to content

Commit 2c22be2

Browse files
committed
Add Define URL Redirects In The Next Config as a Next.js til
1 parent a57a90c commit 2c22be2

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
99

1010
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1111

12-
_959 TILs and counting..._
12+
_960 TILs and counting..._
1313

1414
---
1515

@@ -32,6 +32,7 @@ _959 TILs and counting..._
3232
* [Mac](#mac)
3333
* [MongoDB](#mongodb)
3434
* [MySQL](#mysql)
35+
* [Next.js](#next-js)
3536
* [Phoenix](#phoenix)
3637
* [PostgreSQL](#postgresql)
3738
* [Python](#python)
@@ -423,6 +424,10 @@ _959 TILs and counting..._
423424
- [Show Tables That Match A Pattern](mysql/show-tables-that-match-a-pattern.md)
424425
- [Show Indexes For A Table](mysql/show-indexes-for-a-table.md)
425426

427+
### Next.js
428+
429+
- [Define URL Redirects In The Next Config](nextjs/define-url-redirects-in-the-next-config.md)
430+
426431
### Phoenix
427432

428433
- [Bypass Template Rendering](phoenix/bypass-template-rendering.md)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Define URL Redirects In The Next Config
2+
3+
In [Add Web Server Layer Redirects](vercel/add-web-server-layer-redirects.md),
4+
I explained how to define URL redirects to your [Vercel](https://vercel.com/)
5+
configuration for a [Next.js](https://nextjs.org/) app. Because these redirect
6+
rules are defined in `vercel.json` which is processed at the time of deployment
7+
on the Vercel platform, you are unable to experience these redirects with your
8+
local dev instance of the app. That could be misleading and cause confusion
9+
during development.
10+
11+
Instead, you can define your redirects in `next.config.js` as part of the
12+
Next.js app's configuration. When locally running the Next dev server, these
13+
redirects will be processed and active.
14+
15+
Here is an example of these redirects in `next.config.js`:
16+
17+
```javascript
18+
const nextConfig = {
19+
async redirects() {
20+
return [
21+
{
22+
source: "blog/old-blog-post-name",
23+
destination: "blog/new-blog-post-name",
24+
permanent: true,
25+
},
26+
{
27+
source: "/store",
28+
destination: "store.example.com"
29+
permanent: true,
30+
},
31+
]
32+
},
33+
}
34+
```
35+
36+
These will be 308 Permanent Redirects because of `permanent: true`. You can
37+
change that to `false` to make them into 307s.

0 commit comments

Comments
 (0)