|
1 | 1 | import Chance from 'chance'
|
| 2 | +import type { PrerenderManifest } from 'next/dist/build' |
2 | 3 | import { ExperimentalConfig } from 'next/dist/server/config-shared'
|
3 | 4 |
|
| 5 | +import { generateDynamicRewrites } from '../../packages/runtime/src/helpers/redirects' |
4 | 6 | import {
|
5 | 7 | getCustomImageResponseHeaders,
|
6 | 8 | getRemotePatterns,
|
7 | 9 | ImagesConfig,
|
8 | 10 | redirectsForNext404Route,
|
9 | 11 | } from '../../packages/runtime/src/helpers/utils'
|
10 | 12 |
|
| 13 | +const basePrerenderManifest: PrerenderManifest = { |
| 14 | + version: 3, |
| 15 | + routes: {}, |
| 16 | + dynamicRoutes: {}, |
| 17 | + notFoundRoutes: [], |
| 18 | +} |
| 19 | + |
| 20 | +const prerenderManifest: PrerenderManifest = { |
| 21 | + ...basePrerenderManifest, |
| 22 | + dynamicRoutes: { |
| 23 | + '/getStaticProps/[id]': { |
| 24 | + routeRegex: '^/getStaticProps/([^/]+?)(?:/)?$', |
| 25 | + dataRoute: '/_next/data/build-id/getStaticProps/[id].json', |
| 26 | + fallback: false, |
| 27 | + dataRouteRegex: '^/_next/data/build\\-id/getStaticProps/([^/]+?)\\.json$', |
| 28 | + }, |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +const dynamicRoutes = [ |
| 33 | + { |
| 34 | + page: '/getStaticProps/[id]', |
| 35 | + regex: '^/getStaticProps/([^/]+?)(?:/)?$', |
| 36 | + routeKeys: { |
| 37 | + nextParamid: 'nextParamid', |
| 38 | + }, |
| 39 | + namedRegex: '^/getStaticProps/(?<nextParamid>[^/]+?)(?:/)?$', |
| 40 | + }, |
| 41 | +] |
| 42 | + |
| 43 | +const route = { |
| 44 | + dynamicRoutes, |
| 45 | + prerenderedDynamicRoutes: prerenderManifest.dynamicRoutes, |
| 46 | + basePath: '', |
| 47 | + i18n: null, |
| 48 | + buildId: 'test', |
| 49 | + middleware: [], |
| 50 | + is404Isr: false, |
| 51 | +} |
| 52 | + |
11 | 53 | const chance = new Chance()
|
12 | 54 |
|
13 | 55 | describe('getCustomImageResponseHeaders', () => {
|
@@ -132,4 +174,52 @@ describe('redirectsForNext404Route', () => {
|
132 | 174 | { force: false, from: '/fr/test', status: 404, to: '/server/pages/fr/404.html' },
|
133 | 175 | ])
|
134 | 176 | })
|
| 177 | + |
| 178 | + it('returns static 404 redirects when LEGACY_FALLBACK_FALSE is not set', async () => { |
| 179 | + const expected = { |
| 180 | + dynamicRewrites: [ |
| 181 | + { |
| 182 | + force: false, |
| 183 | + from: '/_next/data/test/getStaticProps/:id.json', |
| 184 | + status: 404, |
| 185 | + to: '/server/pages/404.html', |
| 186 | + }, |
| 187 | + { |
| 188 | + force: false, |
| 189 | + from: '/getStaticProps/:id', |
| 190 | + status: 404, |
| 191 | + to: '/server/pages/404.html', |
| 192 | + }, |
| 193 | + ], |
| 194 | + dynamicRoutesThatMatchMiddleware: [], |
| 195 | + } |
| 196 | + |
| 197 | + expect(generateDynamicRewrites(route)).toStrictEqual(expected) |
| 198 | + }) |
| 199 | + |
| 200 | + it('does not return static 404 redirects when LEGACY_FALLBACK_FALSE is set', async () => { |
| 201 | + process.env.LEGACY_FALLBACK_FALSE = 'true' |
| 202 | + |
| 203 | + const expected = { |
| 204 | + dynamicRewrites: [ |
| 205 | + { |
| 206 | + force: false, |
| 207 | + from: '/_next/data/test/getStaticProps/:id.json', |
| 208 | + status: 200, |
| 209 | + to: '/.netlify/builders/___netlify-odb-handler', |
| 210 | + }, |
| 211 | + { |
| 212 | + force: false, |
| 213 | + from: '/getStaticProps/:id', |
| 214 | + status: 200, |
| 215 | + to: '/.netlify/builders/___netlify-odb-handler', |
| 216 | + }, |
| 217 | + ], |
| 218 | + dynamicRoutesThatMatchMiddleware: [], |
| 219 | + } |
| 220 | + |
| 221 | + expect(generateDynamicRewrites(route)).toStrictEqual(expected) |
| 222 | + |
| 223 | + delete process.env.LEGACY_FALLBACK_FALSE |
| 224 | + }) |
135 | 225 | })
|
0 commit comments