Skip to content

Commit b208ff4

Browse files
authored
feat: add edge middleware support to ntl dev (#1546)
* feat: add edge middleware support to `ntl dev` * chore: typo * chore: null check for plugin instantiation * chore: use plugin wrapper * chore: refactor * chore: fix test * chore: update checks * chore: use opt-out for edge at build/dev time * chore: make build work
1 parent 9578fa9 commit b208ff4

File tree

16 files changed

+514
-80
lines changed

16 files changed

+514
-80
lines changed

.vscode/settings.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
"demos/middleware/.netlify/edge-functions",
55
"demos/server-components/.netlify/edge-functions",
66
],
7-
"deno.unstable": true,
8-
"deno.importMap": "demos/server-components/.netlify/edge-functions-import-map.json"
7+
"deno.unstable": true
98
}

demos/middleware/middleware.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ import { MiddlewareRequest } from '@netlify/next'
55

66
export async function middleware(req: NextRequest) {
77
let response
8-
const {
9-
nextUrl: { pathname },
10-
} = req
8+
const { pathname } = req.nextUrl
119

1210
const request = new MiddlewareRequest(req)
13-
1411
if (pathname.startsWith('/static')) {
1512
// Unlike NextResponse.next(), this actually sends the request to the origin
1613
const res = await request.next()
17-
const message = `This was static but has been transformed in ${req.geo.city}`
14+
const message = `This was static but has been transformed in ${req.geo?.city}`
1815

1916
// Transform the response HTML and props
2017
res.replaceText('p[id=message]', message)
@@ -58,6 +55,16 @@ export async function middleware(req: NextRequest) {
5855
response.headers.set('x-modified-in-rewrite', 'true')
5956
}
6057

58+
if (pathname.startsWith('/shows/redirectme')) {
59+
const url = req.nextUrl.clone()
60+
url.pathname = '/shows/100'
61+
response = NextResponse.redirect(url)
62+
}
63+
64+
if (pathname.startsWith('/shows/redirectexternal')) {
65+
response = NextResponse.redirect('http://example.com/')
66+
}
67+
6168
if (!response) {
6269
response = NextResponse.next()
6370
}

demos/middleware/netlify.toml

+13-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ publish = ".next"
44
ignore = "if [ $CACHED_COMMIT_REF == $COMMIT_REF ]; then (exit 1); else git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../..; fi;"
55

66
[[plugins]]
7+
# Switch these when testing `ntl dev`
8+
# package = "@netlify/plugin-nextjs"
79
package = "../plugin-wrapper/"
810

911
# This is a fake plugin, that makes it run npm install
@@ -15,16 +17,14 @@ included_files = [
1517
"!node_modules/sharp/vendor/8.12.2/darwin-*/**/*",
1618
"!node_modules/sharp/build/Release/sharp-darwin-*"
1719
]
18-
19-
[dev]
20-
framework = "#static"
21-
22-
[[redirects]]
23-
from = "/_next/static/*"
24-
to = "/static/:splat"
25-
status = 200
26-
27-
[[redirects]]
28-
from = "/*"
29-
to = "/.netlify/functions/___netlify-handler"
30-
status = 200
20+
# Uncomment this if testing the built files rather than dev
21+
# [dev]
22+
# framework = "#static"
23+
# [[redirects]]
24+
# from = "/_next/static/*"
25+
# to = "/static/:splat"
26+
# status = 200
27+
# [[redirects]]
28+
# from = "/*"
29+
# to = "/.netlify/functions/___netlify-handler"
30+
# status = 200

demos/middleware/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"ntl": "ntl-internal"
1010
},
1111
"dependencies": {
12-
"@netlify/plugin-nextjs": "*",
1312
"@netlify/next": "*",
13+
"@netlify/plugin-nextjs": "*",
1414
"next": "^12.2.0",
1515
"react": "18.0.0",
1616
"react-dom": "18.0.0"
@@ -24,4 +24,4 @@
2424
"npm-run-all": "^4.1.5",
2525
"typescript": "^4.6.3"
2626
}
27-
}
27+
}

demos/middleware/pages/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export default function Home() {
2525
<p>
2626
<Link href="/shows/rewrite-external">Rewrite to external URL</Link>
2727
</p>
28+
<p>
29+
<Link href="/shows/redirectme">Redirect URL</Link>
30+
</p>
31+
<p>
32+
<Link href="/shows/redirectexternal">Redirect to external URL</Link>
33+
</p>
2834
<p>
2935
<Link href="/shows/static/3">Add header to static page</Link>
3036
</p>

0 commit comments

Comments
 (0)