Skip to content

fix: guard split api routes behaviour by next version #2125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/runtime/src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import slash from 'slash'

import { HANDLER_FUNCTION_NAME, IMAGE_FUNCTION_NAME, ODB_FUNCTION_NAME } from '../constants'

import { splitApiRoutes } from './flags'
import type { APILambda } from './functions'
import type { RoutesManifest } from './types'
import { escapeStringRegexp } from './utils'
Expand Down Expand Up @@ -102,13 +101,13 @@ export const configureHandlerFunctions = async ({
publish,
ignore = [],
apiLambdas,
featureFlags,
splitApiRoutes,
}: {
netlifyConfig: NetlifyConfig
publish: string
ignore: Array<string>
apiLambdas: APILambda[]
featureFlags: Record<string, unknown>
splitApiRoutes: boolean
}) => {
const config = await getRequiredServerFiles(publish)
const files = config.files || []
Expand Down Expand Up @@ -168,7 +167,7 @@ export const configureHandlerFunctions = async ({
configureFunction(HANDLER_FUNCTION_NAME)
configureFunction(ODB_FUNCTION_NAME)

if (splitApiRoutes(featureFlags)) {
if (splitApiRoutes) {
for (const apiLambda of apiLambdas) {
const { functionName, includedFiles } = apiLambda
netlifyConfig.functions[functionName] ||= { included_files: [] }
Expand Down
18 changes: 16 additions & 2 deletions packages/runtime/src/helpers/flags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import destr from 'destr'
import { existsSync } from 'fs-extra'
import { join } from 'pathe'

/**
* If this flag is enabled, we generate individual Lambda functions for API Routes.
Expand All @@ -11,7 +13,19 @@ import destr from 'destr'
* If disabled, we bundle all API Routes into a single function.
* This is can lead to large bundle sizes.
*
* Relies on `next-server.js.nft.json`, which is only supported in Next.js 12+.
*
* Disabled by default. Can be overriden using the NEXT_SPLIT_API_ROUTES env var.
*/
export const splitApiRoutes = (featureFlags: Record<string, unknown>): boolean =>
destr(process.env.NEXT_SPLIT_API_ROUTES) ?? featureFlags.next_split_api_routes ?? false
export const splitApiRoutes = (featureFlags: Record<string, unknown>, publish: string): boolean => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Can you add some tests for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in efa9787.

const isEnabled = destr(process.env.NEXT_SPLIT_API_ROUTES) ?? featureFlags.next_split_api_routes ?? false

if (isEnabled && !existsSync(join(publish, 'next-server.js.nft.json'))) {
console.warn(
'Trace-based bundling not possible on this version of Next.js. Speed up your builds significantly by upgrading to Next.js v12 or newer.',
)
return false
}

return isEnabled
}
4 changes: 2 additions & 2 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const plugin: NetlifyPlugin = {

const buildId = readFileSync(join(publish, 'BUILD_ID'), 'utf8').trim()

const apiLambdas: APILambda[] = splitApiRoutes(featureFlags)
const apiLambdas: APILambda[] = splitApiRoutes(featureFlags, publish)
? await getAPILambdas(publish, appDir, pageExtensions)
: await getExtendedApiRouteConfigs(publish, appDir, pageExtensions).then((extendedRoutes) =>
extendedRoutes.map(packSingleFunction),
Expand All @@ -180,7 +180,7 @@ const plugin: NetlifyPlugin = {
ignore,
publish: relative(process.cwd(), publish),
apiLambdas,
featureFlags,
splitApiRoutes: splitApiRoutes(featureFlags, publish),
})

await movePublicFiles({ appDir, outdir, publish, basePath })
Expand Down
10 changes: 10 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,16 @@ describe('onBuild()', () => {
expect(existsSync(publicFile)).toBe(true)
expect(await readJson(publicFile)).toMatchObject(expect.any(Array))
})

it('does not split APIs when .nft.json files are unavailable', async () => {
await moveNextDist()

await unlink(path.join(process.cwd(), '.next', 'next-server.js.nft.json'))

await nextRuntime.onBuild(defaultArgs)

expect(netlifyConfig.functions['_api_*'].node_bundler).toEqual('nft')
})
})

describe('onPostBuild', () => {
Expand Down