@@ -12,14 +12,18 @@ import type {
1212
1313export type CreateRpcFn = ( functionId : string , splitImportFn ?: string ) => any
1414
15+ export type GenerateFunctionIdFnOptional = (
16+ opts : Parameters < GenerateFunctionIdFn > [ 0 ] ,
17+ ) => string | undefined
18+
1519export type ServerFnPluginOpts = {
1620 /**
1721 * The virtual import ID that will be used to import the server function manifest.
1822 * This virtual import ID will be used in the server build to import the manifest
1923 * and its modules.
2024 */
2125 manifestVirtualImportId : string
22- generateFunctionId ?: GenerateFunctionIdFn
26+ generateFunctionId ?: GenerateFunctionIdFnOptional
2327 client : ServerFnPluginEnvOpts
2428 ssr : ServerFnPluginEnvOpts
2529 server : ServerFnPluginEnvOpts
@@ -282,39 +286,38 @@ function buildGenerateFunctionId(
282286 next : ( dev : boolean , value ?: string ) => string ,
283287 ) => string ,
284288) : GenerateFunctionIdFn {
285- const currentIdToGeneratedId = new Map < string , string > ( )
286- const generatedIds = new Set < string > ( )
289+ const entryIdToFunctionId = new Map < string , string > ( )
290+ const functionIds = new Set < string > ( )
287291 return ( opts ) => {
288- // Keep the previous id in case we already generated it. This is for consistency
289- // between client / server builds and hot reload
290- let generatedId = currentIdToGeneratedId . get ( opts . currentId )
291- if ( generatedId === undefined ) {
292- generatedId = delegate ( opts , ( dev , newId ) => {
292+ const entryId = `${ opts . filename } --${ opts . functionName } `
293+ let functionId = entryIdToFunctionId . get ( entryId )
294+ if ( functionId === undefined ) {
295+ functionId = delegate ( opts , ( dev , updatedFunctionId ) => {
293296 // If no value provided, then return the url-safe currentId on development
294297 // and SHA256 using the currentId as seed on production
295- if ( newId === undefined ) {
296- if ( dev ) newId = makeFunctionIdUrlSafe ( opts . currentId )
298+ if ( updatedFunctionId === undefined ) {
299+ if ( dev ) updatedFunctionId = makeFunctionIdUrlSafe ( entryId )
297300 else
298- newId = crypto
301+ updatedFunctionId = crypto
299302 . createHash ( 'sha256' )
300- . update ( opts . currentId )
303+ . update ( entryId )
301304 . digest ( 'hex' )
302305 }
303- return newId
306+ return updatedFunctionId
304307 } )
305308 // Deduplicate in case the generated id conflicts with an existing id
306- if ( generatedIds . has ( generatedId ) ) {
309+ if ( functionIds . has ( functionId ) ) {
307310 let deduplicatedId
308311 let iteration = 0
309312 do {
310- deduplicatedId = `${ generatedId } _${ ++ iteration } `
311- } while ( generatedIds . has ( deduplicatedId ) )
312- generatedId = deduplicatedId
313+ deduplicatedId = `${ functionId } _${ ++ iteration } `
314+ } while ( functionIds . has ( deduplicatedId ) )
315+ functionId = deduplicatedId
313316 }
314- currentIdToGeneratedId . set ( opts . currentId , generatedId )
315- generatedIds . add ( generatedId )
317+ entryIdToFunctionId . set ( entryId , functionId )
318+ functionIds . add ( functionId )
316319 }
317- return generatedId
320+ return functionId
318321 }
319322}
320323
0 commit comments