File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed
test/integration/middleware/core Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,12 @@ import { NextResponse } from 'next/server'
3
3
export async function middleware ( request ) {
4
4
const url = request . nextUrl
5
5
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
+
6
12
if ( url . pathname === '/rewrites/rewrite-to-ab-test' ) {
7
13
let bucket = request . cookies . bucket
8
14
if ( ! bucket ) {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { join } from 'path'
5
5
import cheerio from 'cheerio'
6
6
import webdriver from 'next-webdriver'
7
7
import {
8
+ check ,
8
9
fetchViaHTTP ,
9
10
findPort ,
10
11
killApp ,
@@ -108,6 +109,29 @@ describe('Middleware base tests', () => {
108
109
} )
109
110
110
111
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
+
111
135
it ( `${ locale } should add a cookie and rewrite to a/b test` , async ( ) => {
112
136
const res = await fetchViaHTTP (
113
137
context . appPort ,
You can’t perform that action at this time.
0 commit comments