Skip to content

fix: fail build/deploy when using unsupported Node.js Midleware #3016

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
18 changes: 18 additions & 0 deletions src/build/content/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { trace } from '@opentelemetry/api'
import { wrapTracer } from '@opentelemetry/api/experimental'
import glob from 'fast-glob'
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin.js'
import type { FunctionsConfigManifest } from 'next-with-cache-handler-v2/dist/build/index.js'
import { prerelease, satisfies, lt as semverLowerThan, lte as semverLowerThanOrEqual } from 'semver'

import type { RunConfig } from '../../run/config.js'
Expand Down Expand Up @@ -131,6 +132,10 @@ export const copyNextServerCode = async (ctx: PluginContext): Promise<void> => {
return
}

if (path === 'server/functions-config-manifest.json') {
await verifyFunctionsConfigManifest(join(srcDir, path))
}

await cp(srcPath, destPath, { recursive: true, force: true })
}),
)
Expand Down Expand Up @@ -376,6 +381,19 @@ const replaceMiddlewareManifest = async (sourcePath: string, destPath: string) =
await writeFile(destPath, newData)
}

const verifyFunctionsConfigManifest = async (sourcePath: string) => {
const data = await readFile(sourcePath, 'utf8')
const manifest = JSON.parse(data) as FunctionsConfigManifest

// https://github.com/vercel/next.js/blob/8367faedd61501025299e92d43a28393c7bb50e2/packages/next/src/build/index.ts#L2465
// Node.js Middleware has hardcoded /_middleware path
if (manifest.functions['/_middleware']) {
throw new Error(
'Only Edge Runtime Middleware is supported. Node.js Middleware is not supported.',
)
}
}

export const verifyHandlerDirStructure = async (ctx: PluginContext) => {
const { nextConfig } = JSON.parse(
await readFile(join(ctx.serverHandlerDir, RUN_CONFIG_FILE), 'utf-8'),
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/middleware-node/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const metadata = {
title: 'Simple Next App',
description: 'Description for Simple Next App',
}

export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
7 changes: 7 additions & 0 deletions tests/fixtures/middleware-node/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Home() {
return (
<main>
<h1>Home</h1>
</main>
)
}
9 changes: 9 additions & 0 deletions tests/fixtures/middleware-node/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { NextRequest } from 'next/server'

export async function middleware(request: NextRequest) {
console.log('Node.js Middleware request:', request.method, request.nextUrl.pathname)
}

export const config = {
runtime: 'nodejs',
}
12 changes: 12 additions & 0 deletions tests/fixtures/middleware-node/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
eslint: {
ignoreDuringBuilds: true,
},
experimental: {
nodeMiddleware: true,
},
}

module.exports = nextConfig
20 changes: 20 additions & 0 deletions tests/fixtures/middleware-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "middleware-node",
"version": "0.1.0",
"private": true,
"scripts": {
"postinstall": "next build",
"dev": "next dev",
"build": "next build"
},
"dependencies": {
"next": "latest",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"test": {
"dependencies": {
"next": ">=15.2.0"
}
}
}
11 changes: 11 additions & 0 deletions tests/integration/edge-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type FixtureTestContext } from '../utils/contexts.js'
import { createFixture, invokeEdgeFunction, runPlugin } from '../utils/fixture.js'
import { generateRandomObjectID, startMockBlobStore } from '../utils/helpers.js'
import { LocalServer } from '../utils/local-server.js'
import { nextVersionSatisfies } from '../utils/next-version-helpers.mjs'

beforeEach<FixtureTestContext>(async (ctx) => {
// set for each test a new deployID and siteID
Expand Down Expand Up @@ -626,3 +627,13 @@ describe('page router', () => {
expect(bodyFr.nextUrlLocale).toBe('fr')
})
})

test.skipIf(!nextVersionSatisfies('>=15.2.0'))<FixtureTestContext>(
'should throw an Not Supported error when node middleware is used',
async (ctx) => {
await createFixture('middleware-node', ctx)
await expect(runPlugin(ctx)).rejects.toThrow(
'Only Edge Runtime Middleware is supported. Node.js Middleware is not supported.',
)
},
)
Loading