@@ -9,7 +9,7 @@ const plugin = require('../src')
9
9
10
10
const { HANDLER_FUNCTION_NAME , ODB_FUNCTION_NAME } = require ( '../src/constants' )
11
11
const { join } = require ( 'pathe' )
12
- const { matchMiddleware, stripLocale } = require ( '../src/helpers/files' )
12
+ const { matchMiddleware, stripLocale, matchesRedirect , matchesRewrite } = require ( '../src/helpers/files' )
13
13
14
14
const FIXTURES_DIR = `${ __dirname } /fixtures`
15
15
const SAMPLE_PROJECT_DIR = `${ __dirname } /../demo`
@@ -31,6 +31,45 @@ const utils = {
31
31
} ,
32
32
}
33
33
34
+ const REDIRECTS = [
35
+ {
36
+ source : '/:file((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/]+\\.\\w+)/' ,
37
+ destination : '/:file' ,
38
+ locale : false ,
39
+ internal : true ,
40
+ statusCode : 308 ,
41
+ regex : '^(?:/((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/]+\\.\\w+))/$' ,
42
+ } ,
43
+ {
44
+ source : '/:notfile((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/\\.]+)' ,
45
+ destination : '/:notfile/' ,
46
+ locale : false ,
47
+ internal : true ,
48
+ statusCode : 308 ,
49
+ regex : '^(?:/((?!\\.well-known(?:/.*)?)(?:[^/]+/)*[^/\\.]+))$' ,
50
+ } ,
51
+ {
52
+ source : '/en/redirectme' ,
53
+ destination : '/' ,
54
+ statusCode : 308 ,
55
+ regex : '^(?!/_next)/en/redirectme(?:/)?$' ,
56
+ } ,
57
+ {
58
+ source : '/:nextInternalLocale(en|es|fr)/redirectme' ,
59
+ destination : '/:nextInternalLocale/' ,
60
+ statusCode : 308 ,
61
+ regex : '^(?!/_next)(?:/(en|es|fr))/redirectme(?:/)?$' ,
62
+ } ,
63
+ ]
64
+
65
+ const REWRITES = [
66
+ {
67
+ source : '/:nextInternalLocale(en|es|fr)/old/:path*' ,
68
+ destination : '/:nextInternalLocale/:path*' ,
69
+ regex : '^(?:/(en|es|fr))/old(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))?(?:/)?$' ,
70
+ } ,
71
+ ]
72
+
34
73
// Temporary switch cwd
35
74
const changeCwd = function ( cwd ) {
36
75
const originalCwd = process . cwd ( )
@@ -447,4 +486,44 @@ describe('utility functions', () => {
447
486
expect ( stripLocale ( path , locales ) ) . toEqual ( path )
448
487
}
449
488
} )
489
+
490
+ test ( 'matchesRedirect correctly matches paths with locales' , ( ) => {
491
+ const paths = [ 'en/redirectme.html' , 'en/redirectme.json' , 'fr/redirectme.html' , 'fr/redirectme.json' ]
492
+ paths . forEach ( ( path ) => {
493
+ expect ( matchesRedirect ( path , REDIRECTS ) ) . toBeTruthy ( )
494
+ } )
495
+ } )
496
+
497
+ test ( "matchesRedirect doesn't match paths with invalid locales" , ( ) => {
498
+ const paths = [ 'dk/redirectme.html' , 'dk/redirectme.json' , 'gr/redirectme.html' , 'gr/redirectme.json' ]
499
+ paths . forEach ( ( path ) => {
500
+ expect ( matchesRedirect ( path , REDIRECTS ) ) . toBeFalsy ( )
501
+ } )
502
+ } )
503
+
504
+ test ( "matchesRedirect doesn't match internal redirects" , ( ) => {
505
+ const paths = [ 'en/notrailingslash' ]
506
+ paths . forEach ( ( path ) => {
507
+ expect ( matchesRedirect ( path , REDIRECTS ) ) . toBeFalsy ( )
508
+ } )
509
+ } )
510
+
511
+ it ( 'matchesRewrite matches array of rewrites' , ( ) => {
512
+ expect ( matchesRewrite ( 'en/old/page.html' , REWRITES ) ) . toBeTruthy ( )
513
+ } )
514
+
515
+ it ( 'matchesRewrite matches beforeFiles rewrites' , ( ) => {
516
+ expect ( matchesRewrite ( 'en/old/page.html' , { beforeFiles : REWRITES } ) ) . toBeTruthy ( )
517
+ } )
518
+
519
+ it ( "matchesRewrite doesn't match afterFiles rewrites" , ( ) => {
520
+ expect ( matchesRewrite ( 'en/old/page.html' , { afterFiles : REWRITES } ) ) . toBeFalsy ( )
521
+ } )
522
+
523
+ it ( 'matchesRewrite matches various paths' , ( ) => {
524
+ const paths = [ 'en/old/page.html' , 'fr/old/page.html' , 'en/old/deep/page.html' , 'en/old.html' ]
525
+ paths . forEach ( ( path ) => {
526
+ expect ( matchesRewrite ( path , REWRITES ) ) . toBeTruthy ( )
527
+ } )
528
+ } )
450
529
} )
0 commit comments