Skip to content

Commit 51c1b58

Browse files
authored
Add test case for middleware rewrite to fallback: true page (vercel#32626)
1 parent 026dd4c commit 51c1b58

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

test/integration/middleware/core/pages/rewrites/_middleware.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { NextResponse } from 'next/server'
33
export async function middleware(request) {
44
const url = request.nextUrl
55

6+
if (url.pathname.startsWith('/rewrites/to-blog')) {
7+
const slug = url.pathname.split('/').pop()
8+
console.log('rewriting to slug', slug)
9+
return NextResponse.rewrite(`/rewrites/fallback-true-blog/${slug}`)
10+
}
11+
612
if (url.pathname === '/rewrites/rewrite-to-ab-test') {
713
let bucket = request.cookies.bucket
814
if (!bucket) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useRouter } from 'next/router'
2+
3+
export default function Page(props) {
4+
if (useRouter().isFallback) {
5+
return <p>Loading...</p>
6+
}
7+
8+
return <p id="props">{JSON.stringify(props)}</p>
9+
}
10+
11+
export function getStaticPaths() {
12+
return {
13+
paths: ['/rewrites/fallback-true-blog/first'],
14+
fallback: true,
15+
}
16+
}
17+
18+
export function getStaticProps({ params }) {
19+
return {
20+
props: {
21+
params,
22+
time: Date.now(),
23+
},
24+
}
25+
}

test/integration/middleware/core/test/index.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { join } from 'path'
55
import cheerio from 'cheerio'
66
import webdriver from 'next-webdriver'
77
import {
8+
check,
89
fetchViaHTTP,
910
findPort,
1011
killApp,
@@ -108,6 +109,29 @@ describe('Middleware base tests', () => {
108109
})
109110

110111
function rewriteTests(locale = '') {
112+
it('should rewrite to fallback: true page successfully', async () => {
113+
const randomSlug = `another-${Date.now()}`
114+
const res2 = await fetchViaHTTP(
115+
context.appPort,
116+
`${locale}/rewrites/to-blog/${randomSlug}`
117+
)
118+
expect(res2.status).toBe(200)
119+
expect(await res2.text()).toContain('Loading...')
120+
121+
const randomSlug2 = `another-${Date.now()}`
122+
const browser = await webdriver(
123+
context.appPort,
124+
`${locale}/rewrites/to-blog/${randomSlug2}`
125+
)
126+
127+
await check(async () => {
128+
const props = JSON.parse(await browser.elementByCss('#props').text())
129+
return props.params.slug === randomSlug2
130+
? 'success'
131+
: JSON.stringify(props)
132+
}, 'success')
133+
})
134+
111135
it(`${locale} should add a cookie and rewrite to a/b test`, async () => {
112136
const res = await fetchViaHTTP(
113137
context.appPort,

0 commit comments

Comments
 (0)