11import { NetlifyConfig } from '@netlify/build'
2- import { yellowBright } from 'chalk'
32import { readJSON } from 'fs-extra'
3+ import globby from 'globby'
44import { NextConfig } from 'next'
55import { PrerenderManifest } from 'next/dist/build'
6- import { outdent } from 'outdent'
76import { join } from 'pathe'
87
98import { HANDLER_FUNCTION_PATH , HIDDEN_PATHS , ODB_FUNCTION_PATH } from '../constants'
@@ -45,10 +44,10 @@ const generateLocaleRedirects = ({
4544
4645export const generateRedirects = async ( {
4746 netlifyConfig,
48- nextConfig : { i18n, basePath, trailingSlash } ,
47+ nextConfig : { i18n, basePath, trailingSlash, appDir } ,
4948} : {
5049 netlifyConfig : NetlifyConfig
51- nextConfig : Pick < NextConfig , 'i18n' | 'basePath' | 'trailingSlash' >
50+ nextConfig : Pick < NextConfig , 'i18n' | 'basePath' | 'trailingSlash' | 'appDir' >
5251} ) => {
5352 const { dynamicRoutes, routes : staticRoutes } : PrerenderManifest = await readJSON (
5453 join ( netlifyConfig . build . publish , 'prerender-manifest.json' ) ,
@@ -100,6 +99,8 @@ export const generateRedirects = async ({
10099 netlifyConfig . redirects . push ( { from : `${ basePath } /:locale/_next/static/*` , to : `/static/:splat` , status : 200 } )
101100 }
102101
102+ const publicFiles = await globby ( '**/*' , { cwd : join ( appDir , 'public' ) } )
103+
103104 // This is only used in prod, so dev uses `next dev` directly
104105 netlifyConfig . redirects . push (
105106 // Static files are in `static`
@@ -115,13 +116,18 @@ export const generateRedirects = async ({
115116 to : HANDLER_FUNCTION_PATH ,
116117 status : 200 ,
117118 } ,
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+ } ) ) ,
119127 {
120128 from : `${ basePath } /*` ,
121129 to : HANDLER_FUNCTION_PATH ,
122130 status : 200 ,
123- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
124- // @ts -ignore The conditions type is incorrect
125131 conditions : { Cookie : [ '__prerender_bypass' , '__next_preview_data' ] } ,
126132 force : true ,
127133 } ,
0 commit comments