@@ -10,6 +10,16 @@ export interface NetlifyPluginOptions {
1010 * @default false
1111 */
1212 edge ?: boolean
13+ /**
14+ * Paths to exclude from being handled by the React Router handler.
15+ *
16+ * @IMPORTANT If you have opted in to edge rendering with `edge: true` and you have your own Netlify
17+ * Functions running on custom `path`s, you must exclude those paths here to avoid conflicts.
18+ *
19+ * @type {URLPattern[] }
20+ * @default []
21+ */
22+ excludedPaths ?: string [ ]
1323}
1424
1525// https://docs.netlify.com/frameworks-api/#netlify-v1-functions
@@ -48,22 +58,23 @@ export default createRequestHandler({
4858
4959// This is written to the functions directory. It just re-exports
5060// the compiled entrypoint, along with Netlify function config.
51- function generateNetlifyFunction ( handlerPath : string ) {
61+ function generateNetlifyFunction ( handlerPath : string , excludedPath : Array < string > ) {
5262 return /* js */ `
5363 export { default } from "${ handlerPath } ";
5464
5565 export const config = {
5666 name: "React Router server handler",
5767 generator: "${ name } @${ version } ",
5868 path: "/*",
69+ excludedPath: ${ JSON . stringify ( excludedPath ) } ,
5970 preferStatic: true,
6071 };
6172 `
6273}
6374
6475// This is written to the edge functions directory. It just re-exports
6576// the compiled entrypoint, along with Netlify edge function config.
66- function generateEdgeFunction ( handlerPath : string , excludePath : Array < string > = [ ] ) {
77+ function generateEdgeFunction ( handlerPath : string , excludedPath : Array < string > ) {
6778 return /* js */ `
6879 export { default } from "${ handlerPath } ";
6980
@@ -72,13 +83,14 @@ function generateEdgeFunction(handlerPath: string, excludePath: Array<string> =
7283 generator: "${ name } @${ version } ",
7384 cache: "manual",
7485 path: "/*",
75- excludedPath: ${ JSON . stringify ( excludePath ) } ,
86+ excludedPath: ${ JSON . stringify ( excludedPath ) } ,
7687 };
7788 `
7889}
7990
8091export function netlifyPlugin ( options : NetlifyPluginOptions = { } ) : Plugin {
81- const { edge = false } = options
92+ const edge = options . edge ?? false
93+ const additionalExcludedPaths = options . excludedPaths ?? [ ]
8294 let resolvedConfig : ResolvedConfig
8395 let isProductionSsrBuild = false
8496 return {
@@ -147,9 +159,10 @@ export function netlifyPlugin(options: NetlifyPluginOptions = {}): Plugin {
147159 // not configurable, so the client out dir is always at ../client from the server out dir.
148160 const clientDir = join ( resolvedConfig . build . outDir , '..' , 'client' )
149161 const entries = await readdir ( clientDir , { withFileTypes : true } )
150- const excludePath = [
162+ const excludedPath = [
151163 '/.netlify/*' ,
152164 ...entries . map ( ( entry ) => ( entry . isDirectory ( ) ? `/${ entry . name } /*` : `/${ entry . name } ` ) ) ,
165+ ...additionalExcludedPaths ,
153166 ]
154167
155168 // Write the server entry point to the Netlify Edge Functions directory
@@ -158,14 +171,18 @@ export function netlifyPlugin(options: NetlifyPluginOptions = {}): Plugin {
158171 const relativeHandlerPath = toPosixPath ( relative ( edgeFunctionsDir , handlerPath ) )
159172 await writeFile (
160173 join ( edgeFunctionsDir , FUNCTION_FILENAME ) ,
161- generateEdgeFunction ( relativeHandlerPath , excludePath ) ,
174+ generateEdgeFunction ( relativeHandlerPath , excludedPath ) ,
162175 )
163176 } else {
164177 // Write the server entry point to the Netlify Functions directory
165178 const functionsDir = join ( resolvedConfig . root , NETLIFY_FUNCTIONS_DIR )
166179 await mkdir ( functionsDir , { recursive : true } )
167180 const relativeHandlerPath = toPosixPath ( relative ( functionsDir , handlerPath ) )
168- await writeFile ( join ( functionsDir , FUNCTION_FILENAME ) , generateNetlifyFunction ( relativeHandlerPath ) )
181+ const excludedPath = [ '/.netlify/*' , ...additionalExcludedPaths ]
182+ await writeFile (
183+ join ( functionsDir , FUNCTION_FILENAME ) ,
184+ generateNetlifyFunction ( relativeHandlerPath , excludedPath ) ,
185+ )
169186 }
170187 }
171188 } ,
0 commit comments