1
1
import { NetlifyConfig } from '@netlify/build'
2
- import { yellowBright } from 'chalk'
3
2
import { readJSON } from 'fs-extra'
3
+ import globby from 'globby'
4
4
import { NextConfig } from 'next'
5
5
import { PrerenderManifest } from 'next/dist/build'
6
- import { outdent } from 'outdent'
7
6
import { join } from 'pathe'
8
7
9
8
import { HANDLER_FUNCTION_PATH , HIDDEN_PATHS , ODB_FUNCTION_PATH } from '../constants'
@@ -45,10 +44,10 @@ const generateLocaleRedirects = ({
45
44
46
45
export const generateRedirects = async ( {
47
46
netlifyConfig,
48
- nextConfig : { i18n, basePath, trailingSlash } ,
47
+ nextConfig : { i18n, basePath, trailingSlash, appDir } ,
49
48
} : {
50
49
netlifyConfig : NetlifyConfig
51
- nextConfig : Pick < NextConfig , 'i18n' | 'basePath' | 'trailingSlash' >
50
+ nextConfig : Pick < NextConfig , 'i18n' | 'basePath' | 'trailingSlash' | 'appDir' >
52
51
} ) => {
53
52
const { dynamicRoutes, routes : staticRoutes } : PrerenderManifest = await readJSON (
54
53
join ( netlifyConfig . build . publish , 'prerender-manifest.json' ) ,
@@ -100,6 +99,8 @@ export const generateRedirects = async ({
100
99
netlifyConfig . redirects . push ( { from : `${ basePath } /:locale/_next/static/*` , to : `/static/:splat` , status : 200 } )
101
100
}
102
101
102
+ const publicFiles = await globby ( '**/*' , { cwd : join ( appDir , 'public' ) } )
103
+
103
104
// This is only used in prod, so dev uses `next dev` directly
104
105
netlifyConfig . redirects . push (
105
106
// Static files are in `static`
@@ -115,13 +116,18 @@ export const generateRedirects = async ({
115
116
to : HANDLER_FUNCTION_PATH ,
116
117
status : 200 ,
117
118
} ,
118
- // Preview mode gets forced to the function, to bypess pre-rendered pages
119
+ // Preview mode gets forced to the function, to bypass pre-rendered pages, but static files need to be skipped
120
+ ...publicFiles . map ( ( file ) => ( {
121
+ from : `${ basePath } /${ file } ` ,
122
+ // This is a no-op, but we do it to stop it matching the following rule
123
+ to : `${ basePath } /${ file } ` ,
124
+ conditions : { Cookie : [ '__prerender_bypass' , '__next_preview_data' ] } ,
125
+ status : 200 ,
126
+ } ) ) ,
119
127
{
120
128
from : `${ basePath } /*` ,
121
129
to : HANDLER_FUNCTION_PATH ,
122
130
status : 200 ,
123
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
124
- // @ts -ignore The conditions type is incorrect
125
131
conditions : { Cookie : [ '__prerender_bypass' , '__next_preview_data' ] } ,
126
132
force : true ,
127
133
} ,
0 commit comments