Skip to content

Commit ecf8ef0

Browse files
committed
feat: udated functionality to help prevent slow build
1 parent 7abf64e commit ecf8ef0

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

packages/runtime/src/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ export const HANDLER_FUNCTION_NAME = '___netlify-handler'
22
export const ODB_FUNCTION_NAME = '___netlify-odb-handler'
33
export const IMAGE_FUNCTION_NAME = '_ipx'
44
export const NEXT_PLUGIN_NAME = '@netlify/next-runtime'
5-
5+
export const HANDLER_FUNCTION_TITLE = 'Next.js SSR handler'
6+
export const ODB_FUNCTION_TITLE = 'Next.js ISR handler'
7+
export const IMAGE_FUNCTION_TITLE = "'next/image handler'"
68
// These are paths in .next that shouldn't be publicly accessible
79
export const HIDDEN_PATHS = [
810
'/cache/*',

packages/runtime/src/helpers/functions.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {
1313
IMAGE_FUNCTION_NAME,
1414
DEFAULT_FUNCTIONS_SRC,
1515
NEXT_PLUGIN_NAME,
16+
HANDLER_FUNCTION_TITLE,
17+
ODB_FUNCTION_TITLE,
18+
IMAGE_FUNCTION_TITLE,
1619
} from '../constants'
1720
import { getApiHandler } from '../templates/getApiHandler'
1821
import { getHandler } from '../templates/getHandler'
@@ -28,24 +31,27 @@ export interface ApiRouteConfig {
2831
compiled: string
2932
}
3033

31-
const writeGeneratorField = async (functionName: string, functionTitle: string, functionsDir: string) => {
32-
const pluginPackagePath = '.netlify/plugins/package.json' || './package.json'
33-
let nextPlugin
34+
const checkForPackage = async (packageDir: string) => {
35+
const packagePlugin = existsSync(packageDir) ? await readJSON(packageDir) : null
36+
const nextPlugin = packagePlugin ? packagePlugin.dependencies['@netlify/plugin-nextjs'] : null
37+
return nextPlugin
38+
}
3439

35-
if (existsSync(pluginPackagePath)) {
36-
const packagePlugin = await readJSON(pluginPackagePath)
37-
nextPlugin = packagePlugin.dependencies['@netlify/plugin-nextjs']
38-
}
40+
const writeFunctionConfiguration = async (functionName: string, functionTitle: string, functionsDir: string) => {
41+
const pluginPackagePath = '.netlify/plugins/package.json'
42+
const depsPackagePath = './package.json'
43+
44+
const nextPluginVersion = (await checkForPackage(pluginPackagePath)) || (await checkForPackage(depsPackagePath))
3945

40-
const generator = {
46+
const metadata = {
4147
config: {
4248
name: functionTitle,
43-
generator: nextPlugin ? `${NEXT_PLUGIN_NAME}@${nextPlugin}` : 'Plugin Not Found',
49+
generator: nextPluginVersion ? `${NEXT_PLUGIN_NAME}@${nextPluginVersion}` : 'Plugin Not Found',
4450
},
45-
version: nextPlugin || 'Version Not Found',
51+
version: 1,
4652
}
4753

48-
await writeFile(join(functionsDir, functionName, `${functionName}.json`), JSON.stringify(generator))
54+
await writeFile(join(functionsDir, functionName, `${functionName}.json`), JSON.stringify(metadata))
4955
}
5056

5157
export const generateFunctions = async (
@@ -97,11 +103,11 @@ export const generateFunctions = async (
97103
join(__dirname, '..', '..', 'lib', 'templates', 'handlerUtils.js'),
98104
join(functionsDir, functionName, 'handlerUtils.js'),
99105
)
100-
writeGeneratorField(functionName, functionTitle, functionsDir)
106+
writeFunctionConfiguration(functionName, functionTitle, functionsDir)
101107
}
102108

103-
await writeHandler(HANDLER_FUNCTION_NAME, 'Next.js SSR handler', false)
104-
await writeHandler(ODB_FUNCTION_NAME, 'Next.js ISR handler', true)
109+
await writeHandler(HANDLER_FUNCTION_NAME, HANDLER_FUNCTION_TITLE, false)
110+
await writeHandler(ODB_FUNCTION_NAME, ODB_FUNCTION_TITLE, 'Next.js ISR handler', true)
105111
}
106112

107113
/**
@@ -165,7 +171,7 @@ export const setupImageFunction = async ({
165171
})
166172

167173
await copyFile(join(__dirname, '..', '..', 'lib', 'templates', 'ipx.js'), join(functionDirectory, functionName))
168-
writeGeneratorField(functionName.replace('.js', ''), 'next/image handler', functionsPath)
174+
writeFunctionConfiguration(functionName.replace('.js', ''), IMAGE_FUNCTION_TITLE, functionsPath)
169175

170176
// If we have edge functions then the request will have already been rewritten
171177
// so this won't match. This is matched if edge is disabled or unavailable.

0 commit comments

Comments
 (0)