|
1 | 1 | import { makeLocaleOptional, stripLookahead } from '../packages/runtime/src/helpers/matchers'
|
2 |
| - |
| 2 | +import { getEdgeFunctionPatternForPage } from '../packages/runtime/src/helpers/edge' |
3 | 3 | const makeDataPath = (path: string) => `/_next/data/build-id${path === '/' ? '/index' : path}.json`
|
4 | 4 |
|
5 | 5 | function checkPath(path: string, regex: string) {
|
@@ -68,3 +68,109 @@ describe('the middleware path matcher', () => {
|
68 | 68 | expect(checkPath('/shows/888', stripped)).toBe(true)
|
69 | 69 | })
|
70 | 70 | })
|
| 71 | + |
| 72 | +const pageRegexMap = new Map([ |
| 73 | + ['/api/shows/[id]', '^/api/shows/([^/]+?)(?:/)?$'], |
| 74 | + ['/api/shows/[...params]', '^/api/shows/(.+?)(?:/)?$'], |
| 75 | + ['/app-edge/[id]', '^/app\\-edge/([^/]+?)(?:/)?$'], |
| 76 | + ['/blog/[author]', '^/blog/([^/]+?)(?:/)?$'], |
| 77 | + ['/blog/[author]/[slug]', '^/blog/([^/]+?)/([^/]+?)(?:/)?$'], |
| 78 | + ['/getServerSideProps/all/[[...slug]]', '^/getServerSideProps/all(?:/(.+?))?(?:/)?$'], |
| 79 | + ['/getServerSideProps/[id]', '^/getServerSideProps/([^/]+?)(?:/)?$'], |
| 80 | +]) |
| 81 | + |
| 82 | +const appPathRoutesManifest = { |
| 83 | + '/app-edge/[id]/page': '/app-edge/[id]', |
| 84 | + '/blog/[author]/[slug]/page': '/blog/[author]/[slug]', |
| 85 | + '/blog/[author]/page': '/blog/[author]', |
| 86 | +} |
| 87 | + |
| 88 | +describe('the edge function matcher helpers', () => { |
| 89 | + it('finds the correct regex for an edge API route', () => { |
| 90 | + const regex = getEdgeFunctionPatternForPage({ |
| 91 | + pageRegexMap, |
| 92 | + appPathRoutesManifest, |
| 93 | + edgeFunctionDefinition: { |
| 94 | + name: 'pages/api/og', |
| 95 | + page: '/api/og', |
| 96 | + env: [], |
| 97 | + files: [], |
| 98 | + matchers: [ |
| 99 | + { |
| 100 | + regexp: '^/api/og$', |
| 101 | + }, |
| 102 | + ], |
| 103 | + wasm: [], |
| 104 | + assets: [], |
| 105 | + }, |
| 106 | + }) |
| 107 | + expect(regex).toBe('^/api/og/?$') |
| 108 | + expect('/api/og').toMatch(new RegExp(regex)) |
| 109 | + expect('/api/og/').toMatch(new RegExp(regex)) |
| 110 | + }) |
| 111 | + |
| 112 | + it('finds the correct regex for an appDir page with a dynamic route', () => { |
| 113 | + const regex = getEdgeFunctionPatternForPage({ |
| 114 | + pageRegexMap, |
| 115 | + appPathRoutesManifest, |
| 116 | + edgeFunctionDefinition: { |
| 117 | + env: [], |
| 118 | + files: [], |
| 119 | + name: 'app/app-edge/[id]/page', |
| 120 | + page: '/app-edge/[id]/page', |
| 121 | + matchers: [ |
| 122 | + { |
| 123 | + regexp: '^/app\\-edge/(?<id>[^/]+?)$', |
| 124 | + }, |
| 125 | + ], |
| 126 | + wasm: [], |
| 127 | + assets: [], |
| 128 | + }, |
| 129 | + }) |
| 130 | + expect(regex).toBe('^/app\\-edge/([^/]+?)(?:/)?$') |
| 131 | + expect('/app-edge/1').toMatch(new RegExp(regex)) |
| 132 | + expect('/app-edge/1/').toMatch(new RegExp(regex)) |
| 133 | + }) |
| 134 | + |
| 135 | + it('finds the correct regex for a pages edge route', () => { |
| 136 | + const regex = getEdgeFunctionPatternForPage({ |
| 137 | + pageRegexMap, |
| 138 | + appPathRoutesManifest, |
| 139 | + edgeFunctionDefinition: { |
| 140 | + env: [], |
| 141 | + files: [], |
| 142 | + name: 'pages/edge/[id]', |
| 143 | + page: '/edge/[id]', |
| 144 | + matchers: [ |
| 145 | + { |
| 146 | + regexp: '^/edge/(?<id>[^/]+?)$', |
| 147 | + }, |
| 148 | + ], |
| 149 | + wasm: [], |
| 150 | + assets: [], |
| 151 | + }, |
| 152 | + }) |
| 153 | + expect(regex).toBe('^/edge/(?<id>[^/]+?)/?$') |
| 154 | + expect('/edge/1').toMatch(new RegExp(regex)) |
| 155 | + expect('/edge/1/').toMatch(new RegExp(regex)) |
| 156 | + }) |
| 157 | + |
| 158 | + it('finds the correct regex for a pages edge route with a v1 definition', () => { |
| 159 | + const regex = getEdgeFunctionPatternForPage({ |
| 160 | + pageRegexMap, |
| 161 | + appPathRoutesManifest, |
| 162 | + edgeFunctionDefinition: { |
| 163 | + env: [], |
| 164 | + files: [], |
| 165 | + name: 'pages/edge/[id]', |
| 166 | + page: '/edge/[id]', |
| 167 | + regexp: '^/edge/(?<id>[^/]+?)$', |
| 168 | + wasm: [], |
| 169 | + assets: [], |
| 170 | + }, |
| 171 | + }) |
| 172 | + expect(regex).toBe('^/edge/(?<id>[^/]+?)/?$') |
| 173 | + expect('/edge/1').toMatch(new RegExp(regex)) |
| 174 | + expect('/edge/1/').toMatch(new RegExp(regex)) |
| 175 | + }) |
| 176 | +}) |
0 commit comments