@@ -7,13 +7,22 @@ import type { ImageConfigComplete, RemotePattern } from 'next/dist/shared/lib/im
7
7
import { outdent } from 'outdent'
8
8
import { join , relative , resolve } from 'pathe'
9
9
10
- import { HANDLER_FUNCTION_NAME , ODB_FUNCTION_NAME , IMAGE_FUNCTION_NAME , DEFAULT_FUNCTIONS_SRC } from '../constants'
10
+ import {
11
+ HANDLER_FUNCTION_NAME ,
12
+ ODB_FUNCTION_NAME ,
13
+ IMAGE_FUNCTION_NAME ,
14
+ DEFAULT_FUNCTIONS_SRC ,
15
+ HANDLER_FUNCTION_TITLE ,
16
+ ODB_FUNCTION_TITLE ,
17
+ IMAGE_FUNCTION_TITLE ,
18
+ } from '../constants'
11
19
import { getApiHandler } from '../templates/getApiHandler'
12
20
import { getHandler } from '../templates/getHandler'
13
21
import { getResolverForPages , getResolverForSourceFiles } from '../templates/getPageResolver'
14
22
15
23
import { ApiConfig , ApiRouteType , extractConfigFromFile } from './analysis'
16
24
import { getSourceFileForPage } from './files'
25
+ import { writeFunctionConfiguration } from './functionsMetaData'
17
26
import { getFunctionNameForPage } from './utils'
18
27
19
28
export interface ApiRouteConfig {
@@ -45,8 +54,16 @@ export const generateFunctions = async (
45
54
} )
46
55
const functionName = getFunctionNameForPage ( route , config . type === ApiRouteType . BACKGROUND )
47
56
await ensureDir ( join ( functionsDir , functionName ) )
57
+
58
+ // write main API handler file
48
59
await writeFile ( join ( functionsDir , functionName , `${ functionName } .js` ) , apiHandlerSource )
60
+
61
+ // copy handler dependencies (VercelNodeBridge, NetlifyNextServer, etc.)
49
62
await copyFile ( bridgeFile , join ( functionsDir , functionName , 'bridge.js' ) )
63
+ await copyFile (
64
+ join ( __dirname , '..' , '..' , 'lib' , 'templates' , 'server.js' ) ,
65
+ join ( functionsDir , functionName , 'server.js' ) ,
66
+ )
50
67
await copyFile (
51
68
join ( __dirname , '..' , '..' , 'lib' , 'templates' , 'handlerUtils.js' ) ,
52
69
join ( functionsDir , functionName , 'handlerUtils.js' ) ,
@@ -62,19 +79,28 @@ export const generateFunctions = async (
62
79
await writeFile ( join ( functionsDir , functionName , 'pages.js' ) , resolverSource )
63
80
}
64
81
65
- const writeHandler = async ( functionName : string , isODB : boolean ) => {
82
+ const writeHandler = async ( functionName : string , functionTitle : string , isODB : boolean ) => {
66
83
const handlerSource = await getHandler ( { isODB, publishDir, appDir : relative ( functionDir , appDir ) } )
67
84
await ensureDir ( join ( functionsDir , functionName ) )
85
+
86
+ // write main handler file (standard or ODB)
68
87
await writeFile ( join ( functionsDir , functionName , `${ functionName } .js` ) , handlerSource )
88
+
89
+ // copy handler dependencies (VercelNodeBridge, NetlifyNextServer, etc.)
69
90
await copyFile ( bridgeFile , join ( functionsDir , functionName , 'bridge.js' ) )
91
+ await copyFile (
92
+ join ( __dirname , '..' , '..' , 'lib' , 'templates' , 'server.js' ) ,
93
+ join ( functionsDir , functionName , 'server.js' ) ,
94
+ )
70
95
await copyFile (
71
96
join ( __dirname , '..' , '..' , 'lib' , 'templates' , 'handlerUtils.js' ) ,
72
97
join ( functionsDir , functionName , 'handlerUtils.js' ) ,
73
98
)
99
+ writeFunctionConfiguration ( { functionName, functionTitle, functionsDir } )
74
100
}
75
101
76
- await writeHandler ( HANDLER_FUNCTION_NAME , false )
77
- await writeHandler ( ODB_FUNCTION_NAME , true )
102
+ await writeHandler ( HANDLER_FUNCTION_NAME , HANDLER_FUNCTION_TITLE , false )
103
+ await writeHandler ( ODB_FUNCTION_NAME , ODB_FUNCTION_TITLE , true )
78
104
}
79
105
80
106
/**
@@ -138,6 +164,11 @@ export const setupImageFunction = async ({
138
164
} )
139
165
140
166
await copyFile ( join ( __dirname , '..' , '..' , 'lib' , 'templates' , 'ipx.js' ) , join ( functionDirectory , functionName ) )
167
+ writeFunctionConfiguration ( {
168
+ functionName : IMAGE_FUNCTION_NAME ,
169
+ functionTitle : IMAGE_FUNCTION_TITLE ,
170
+ functionsDir : functionsPath ,
171
+ } )
141
172
142
173
// If we have edge functions then the request will have already been rewritten
143
174
// so this won't match. This is matched if edge is disabled or unavailable.
0 commit comments