Unverified Commit ad2f145e authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure we warn for middleware with next export (#45288)

We currently don't make it obvious when next export is being leveraged
and middleware is present which won't work when deployed to a static
host so this ensures we warn the same way we do for API routes.

x-ref: https://github.com/vercel/vercel/pull/9319
parent 160997bb
......@@ -25,6 +25,7 @@ import {
FLIGHT_MANIFEST,
FLIGHT_SERVER_CSS_MANIFEST,
FONT_LOADER_MANIFEST,
MIDDLEWARE_MANIFEST,
PAGES_MANIFEST,
PHASE_EXPORT,
PRERENDER_MANIFEST,
......@@ -48,6 +49,7 @@ import {
loadRequireHook,
overrideBuiltInReactPackages,
} from '../build/webpack/require-hook'
import { MiddlewareManifest } from '../build/webpack/plugins/middleware-plugin'
loadRequireHook()
if (process.env.NEXT_PREBUNDLED_REACT) {
......@@ -508,13 +510,24 @@ export default async function exportApp(
)
}
}
let hasMiddleware = false
try {
const middlewareManifest = require(join(
distDir,
SERVER_DIRECTORY,
MIDDLEWARE_MANIFEST
)) as MiddlewareManifest
hasMiddleware = Object.keys(middlewareManifest.middleware).length > 0
} catch (_) {}
// Warn if the user defines a path for an API page
if (hasApiRoutes) {
if (hasApiRoutes || hasMiddleware) {
if (!options.silent) {
Log.warn(
chalk.yellow(
`Statically exporting a Next.js application via \`next export\` disables API routes.`
`Statically exporting a Next.js application via \`next export\` disables API routes and middleware.`
) +
`\n` +
chalk.yellow(
......
/* eslint-env jest */
import { promises as fs } from 'fs'
import fs from 'fs-extra'
import { join } from 'path'
import {
fetchViaHTTP,
......@@ -9,6 +9,7 @@ import {
killApp,
nextBuild,
nextStart,
nextExport,
} from 'next-test-utils'
let app
......@@ -38,7 +39,7 @@ function runDoubleMiddlewareTests() {
}
async function writeRootMiddleware() {
await fs.cp(join(appDir, 'src/pages'), join(appDir, 'pages'), {
await fs.copy(join(appDir, 'src/pages'), join(appDir, 'pages'), {
force: true,
recursive: true,
})
......@@ -67,9 +68,9 @@ return response
}
async function removeRootMiddleware() {
await fs.rm(rootMiddlewareJSFile, { force: true })
await fs.rm(rootMiddlewareTSFile, { force: true })
await fs.rm(join(appDir, 'pages'), { force: true, recursive: true })
await fs.remove(rootMiddlewareJSFile, { force: true })
await fs.remove(rootMiddlewareTSFile, { force: true })
await fs.remove(join(appDir, 'pages'), { force: true, recursive: true })
}
describe.each([
......@@ -100,13 +101,35 @@ describe.each([
})
describe('production mode', () => {
let exportOutput = ''
beforeAll(async () => {
await nextBuild(appDir)
const outdir = join(__dirname, '..', 'out')
await fs.remove(outdir).catch(() => {})
const result = await nextExport(
appDir,
{ outdir },
{
stderr: true,
stdout: true,
}
)
exportOutput = result.stderr + result.stdout
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
it('should warn about middleware on export', async () => {
expect(exportOutput).toContain(
'Statically exporting a Next.js application via `next export` disables API routes and middleware.'
)
})
runTest()
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment