Skip to content

Commit 01168ef

Browse files
committed
do Next.js version verification earlier and make it easy to grab the version after build command finished
1 parent c60ac69 commit 01168ef

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

src/build/content/server.ts

+3-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { prerelease, lt as semverLowerThan, lte as semverLowerThanOrEqual } from
2020

2121
import { RUN_CONFIG } from '../../run/constants.js'
2222
import { PluginContext } from '../plugin-context.js'
23-
import { verifyNextVersion } from '../verification.js'
2423

2524
const tracer = wrapTracer(trace.getTracer('Next runtime'))
2625

@@ -292,26 +291,13 @@ export const copyNextDependencies = async (ctx: PluginContext): Promise<void> =>
292291

293292
await Promise.all(promises)
294293

295-
// detect if it might lead to a runtime issue and throw an error upfront on build time instead of silently failing during runtime
296294
const serverHandlerRequire = createRequire(posixJoin(ctx.serverHandlerDir, ':internal:'))
297295

298-
let nextVersion: string | undefined
299-
try {
300-
const { version } = serverHandlerRequire('next/package.json')
301-
if (version) {
302-
nextVersion = version as string
303-
}
304-
} catch {
305-
// failed to resolve package.json - currently this is resolvable in all known next versions, but if next implements
306-
// exports map it still might be a problem in the future, so we are not breaking here
307-
}
308-
309-
if (nextVersion) {
310-
verifyNextVersion(ctx, nextVersion)
311-
312-
await patchNextModules(ctx, nextVersion, serverHandlerRequire.resolve)
296+
if (ctx.nextVersion) {
297+
await patchNextModules(ctx, ctx.nextVersion, serverHandlerRequire.resolve)
313298
}
314299

300+
// detect if it might lead to a runtime issue and throw an error upfront on build time instead of silently failing during runtime
315301
try {
316302
const nextEntryAbsolutePath = serverHandlerRequire.resolve('next')
317303
const nextRequire = createRequire(nextEntryAbsolutePath)

src/build/plugin-context.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { existsSync, readFileSync } from 'node:fs'
22
import { readFile } from 'node:fs/promises'
3+
import { createRequire } from 'node:module'
34
import { join, relative, resolve } from 'node:path'
5+
import { join as posixJoin } from 'node:path/posix'
46
import { fileURLToPath } from 'node:url'
57

68
import type {
@@ -313,6 +315,25 @@ export class PluginContext {
313315
return JSON.parse(await readFile(join(this.publishDir, 'routes-manifest.json'), 'utf-8'))
314316
}
315317

318+
#nextVersion: string | null | undefined = undefined
319+
320+
/**
321+
* Get Next.js version that was used to build the site
322+
*/
323+
get nextVersion(): string | null {
324+
if (this.#nextVersion === undefined) {
325+
try {
326+
const serverHandlerRequire = createRequire(posixJoin(this.standaloneRootDir, ':internal:'))
327+
const { version } = serverHandlerRequire('next/package.json')
328+
this.#nextVersion = version as string
329+
} catch {
330+
this.#nextVersion = null
331+
}
332+
}
333+
334+
return this.#nextVersion
335+
}
336+
316337
/** Fails a build with a message and an optional error */
317338
failBuild(message: string, error?: unknown): never {
318339
return this.utils.build.failBuild(message, error instanceof Error ? { error } : undefined)

src/build/verification.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ export function verifyPublishDir(ctx: PluginContext) {
4747
`Your publish directory does not contain expected Next.js build output. Please make sure you are using Next.js version (${SUPPORTED_NEXT_VERSIONS})`,
4848
)
4949
}
50+
51+
if (
52+
ctx.nextVersion &&
53+
!satisfies(ctx.nextVersion, SUPPORTED_NEXT_VERSIONS, { includePrerelease: true })
54+
) {
55+
ctx.failBuild(
56+
`@netlify/plugin-next@5 requires Next.js version ${SUPPORTED_NEXT_VERSIONS}, but found ${ctx.nextVersion}. Please upgrade your project's Next.js version.`,
57+
)
58+
}
5059
}
5160
if (ctx.buildConfig.output === 'export') {
5261
if (!ctx.exportDetail?.success) {
@@ -60,14 +69,6 @@ export function verifyPublishDir(ctx: PluginContext) {
6069
}
6170
}
6271

63-
export function verifyNextVersion(ctx: PluginContext, nextVersion: string): void | never {
64-
if (!satisfies(nextVersion, SUPPORTED_NEXT_VERSIONS, { includePrerelease: true })) {
65-
ctx.failBuild(
66-
`@netlify/plugin-next@5 requires Next.js version ${SUPPORTED_NEXT_VERSIONS}, but found ${nextVersion}. Please upgrade your project's Next.js version.`,
67-
)
68-
}
69-
}
70-
7172
export async function verifyNoAdvancedAPIRoutes(ctx: PluginContext) {
7273
const apiRoutesConfigs = await getAPIRoutesConfigs(ctx)
7374

0 commit comments

Comments
 (0)