Skip to content

Commit 23d22a2

Browse files
committed
Add test
1 parent 93da5ee commit 23d22a2

File tree

10 files changed

+88
-0
lines changed

10 files changed

+88
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function RootLayout({
2+
children,
3+
}: {
4+
children: React.ReactNode
5+
}) {
6+
return (
7+
<html>
8+
<body>{children}</body>
9+
</html>
10+
)
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <p>hello world</p>
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('middleware skip Next.js internal routes (opt-out)', () => {
4+
const { next } = nextTestSetup({
5+
files: __dirname,
6+
})
7+
8+
it('should execute middleware on regular routes', async () => {
9+
const res = await next.fetch('/')
10+
expect(res.status).toBe(200)
11+
expect(res.headers.get('x-middleware-executed')).toBe('true')
12+
})
13+
14+
it('should ALSO execute middleware on _next routes when opted out', async () => {
15+
const res = await next.fetch('/_next/static/chunks/webpack.js')
16+
expect(res.headers.get('x-middleware-executed')).toBe('true')
17+
})
18+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { NextResponse } from 'next/server'
2+
3+
export function middleware() {
4+
const response = NextResponse.next()
5+
response.headers.set('x-middleware-executed', 'true')
6+
return response
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
skipMiddlewareNextInternalRoutes: false,
4+
}
5+
6+
module.exports = nextConfig
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function RootLayout({
2+
children,
3+
}: {
4+
children: React.ReactNode
5+
}) {
6+
return (
7+
<html>
8+
<body>{children}</body>
9+
</html>
10+
)
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <p>hello world</p>
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('middleware skip Next.js internal routes', () => {
4+
const { next } = nextTestSetup({
5+
files: __dirname,
6+
})
7+
8+
it('should execute middleware on regular routes', async () => {
9+
const res = await next.fetch('/')
10+
expect(res.status).toBe(200)
11+
expect(res.headers.get('x-middleware-executed')).toBe('true')
12+
})
13+
14+
it('should NOT execute middleware on _next routes', async () => {
15+
const res = await next.fetch('/_next/static/chunks/webpack.js')
16+
expect(res.headers.get('x-middleware-executed')).toBeFalsy()
17+
})
18+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { NextResponse } from 'next/server'
2+
3+
export function middleware() {
4+
const response = NextResponse.next()
5+
response.headers.set('x-middleware-executed', 'true')
6+
return response
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {}
3+
4+
module.exports = nextConfig

0 commit comments

Comments
 (0)