Skip to content

Commit 55755df

Browse files
fix: run edge middleware on data requests (#1382)
* fix: run middleware on data requests * chore: add static page to middleware demo Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent db635d0 commit 55755df

File tree

6 files changed

+235
-163
lines changed

6 files changed

+235
-163
lines changed

demos/middleware/middleware.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { NextFetchEvent, NextRequest } from 'next/server'
33

44
export function middleware(request: NextRequest, ev: NextFetchEvent) {
55
let response
6-
const {nextUrl: {pathname}} = request
6+
const {
7+
nextUrl: { pathname },
8+
} = request
79

810
if (pathname.startsWith('/cookies')) {
911
response = NextResponse.next()
@@ -30,10 +32,14 @@ export function middleware(request: NextRequest, ev: NextFetchEvent) {
3032
if (!response) {
3133
response = NextResponse.next()
3234
}
35+
36+
if (pathname.startsWith('/shows/static')) {
37+
response.headers.set('x-middleware-date', new Date().toISOString())
38+
}
39+
3340
response.headers.set('x-modified-edge', 'true')
3441
response.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')
3542

3643
return response
3744
}
38-
3945
}

demos/middleware/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"ntl": "ntl-internal"
1010
},
1111
"dependencies": {
12-
"next": "^12.1.7-canary.12",
12+
"next": "^12.1.7-canary.33",
1313
"react": "18.0.0",
1414
"react-dom": "18.0.0"
1515
},

demos/middleware/pages/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ 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/static/3">Add header to static page</Link>
30+
</p>
2831
<p>
2932
<Link href="/cookies" prefetch={false}>
3033
Cookie API
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { useRouter } from 'next/router'
2+
import Link from 'next/link'
3+
4+
const Show = ({ show }) => {
5+
const router = useRouter()
6+
7+
if (router.isFallback) {
8+
return <div>Loading...</div>
9+
}
10+
11+
return (
12+
<div>
13+
<p>
14+
Check the network panel for the header <code>x-middleware-date</code> to ensure that it is running
15+
</p>
16+
<hr />
17+
<p>
18+
<Link href="/shows/static/3">Show 3</Link> and <Link href="/shows/static/4">show 4</Link> are pre-rendered
19+
</p>
20+
<h1>Show #{show.id}</h1>
21+
<p>{show.name}</p>
22+
23+
<hr />
24+
25+
<Link href="/">
26+
<a>Go back home</a>
27+
</Link>
28+
</div>
29+
)
30+
}
31+
32+
export async function getStaticPaths() {
33+
// Set the paths we want to pre-render
34+
const paths = [{ params: { id: '3' } }, { params: { id: '4' } }]
35+
36+
// We'll pre-render these paths at build time.
37+
// { fallback: true } means other routes will be rendered at runtime.
38+
return { paths, fallback: true }
39+
}
40+
41+
export async function getStaticProps({ params }) {
42+
// The ID to render
43+
const { id } = params
44+
45+
const res = await fetch(`https://api.tvmaze.com/shows/${id}`)
46+
const data = await res.json()
47+
48+
return {
49+
props: {
50+
show: data,
51+
},
52+
}
53+
}
54+
55+
export default Show

0 commit comments

Comments
 (0)