Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add edge middleware support to ntl dev #1546

Merged
merged 14 commits into from
Aug 18, 2022
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"deno.enablePaths": [
"packages/runtime/src/templates/edge",
"demos/middleware/.netlify/edge-functions",
"demos/middleware/netlify/edge-functions",
"demos/server-components/.netlify/edge-functions",
],
"deno.unstable": true,
"deno.importMap": "demos/server-components/.netlify/edge-functions-import-map.json"
"deno.unstable": true
}
17 changes: 12 additions & 5 deletions demos/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import { MiddlewareRequest } from '@netlify/next'

export async function middleware(req: NextRequest) {
let response
const {
nextUrl: { pathname },
} = req
const { pathname } = req.nextUrl

const request = new MiddlewareRequest(req)

if (pathname.startsWith('/static')) {
// Unlike NextResponse.next(), this actually sends the request to the origin
const res = await request.next()
const message = `This was static but has been transformed in ${req.geo.city}`
const message = `This was static but has been transformed in ${req.geo?.city}`

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

if (pathname.startsWith('/shows/redirectme')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redirects weren't working, so this is a test for the fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redirects were broken, so these are the added tests

const url = req.nextUrl.clone()
url.pathname = '/shows/100'
response = NextResponse.redirect(url)
}

if (pathname.startsWith('/shows/redirectexternal')) {
response = NextResponse.redirect('http://example.com/')
}

if (!response) {
response = NextResponse.next()
}
Expand Down
23 changes: 10 additions & 13 deletions demos/middleware/netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ included_files = [
"!node_modules/sharp/vendor/8.12.2/darwin-*/**/*",
"!node_modules/sharp/build/Release/sharp-darwin-*"
]

[dev]
framework = "#static"

[[redirects]]
from = "/_next/static/*"
to = "/static/:splat"
status = 200

[[redirects]]
from = "/*"
to = "/.netlify/functions/___netlify-handler"
status = 200
# [dev]
# framework = "#static"
# [[redirects]]
# from = "/_next/static/*"
# to = "/static/:splat"
# status = 200
# [[redirects]]
# from = "/*"
# to = "/.netlify/functions/___netlify-handler"
# status = 200
6 changes: 6 additions & 0 deletions demos/middleware/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export default function Home() {
<p>
<Link href="/shows/rewrite-external">Rewrite to external URL</Link>
</p>
<p>
<Link href="/shows/redirectme">Redirect URL</Link>
</p>
<p>
<Link href="/shows/redirectexternal">Redirect to external URL</Link>
</p>
<p>
<Link href="/shows/static/3">Add header to static page</Link>
</p>
Expand Down
1 change: 1 addition & 0 deletions demos/middleware/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"exclude": [
"node_modules",
"netlify/edge-functions/**/*",
"../../src/templates/edge/*"
],
"include": [
Expand Down
Loading