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 { ...@@ -25,6 +25,7 @@ import {
FLIGHT_MANIFEST, FLIGHT_MANIFEST,
FLIGHT_SERVER_CSS_MANIFEST, FLIGHT_SERVER_CSS_MANIFEST,
FONT_LOADER_MANIFEST, FONT_LOADER_MANIFEST,
MIDDLEWARE_MANIFEST,
PAGES_MANIFEST, PAGES_MANIFEST,
PHASE_EXPORT, PHASE_EXPORT,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
...@@ -48,6 +49,7 @@ import { ...@@ -48,6 +49,7 @@ import {
loadRequireHook, loadRequireHook,
overrideBuiltInReactPackages, overrideBuiltInReactPackages,
} from '../build/webpack/require-hook' } from '../build/webpack/require-hook'
import { MiddlewareManifest } from '../build/webpack/plugins/middleware-plugin'
loadRequireHook() loadRequireHook()
if (process.env.NEXT_PREBUNDLED_REACT) { if (process.env.NEXT_PREBUNDLED_REACT) {
...@@ -508,13 +510,24 @@ export default async function exportApp( ...@@ -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 // Warn if the user defines a path for an API page
if (hasApiRoutes) { if (hasApiRoutes || hasMiddleware) {
if (!options.silent) { if (!options.silent) {
Log.warn( Log.warn(
chalk.yellow( 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` + `\n` +
chalk.yellow( chalk.yellow(
......
/* eslint-env jest */ /* eslint-env jest */
import { promises as fs } from 'fs' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import { import {
fetchViaHTTP, fetchViaHTTP,
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
killApp, killApp,
nextBuild, nextBuild,
nextStart, nextStart,
nextExport,
} from 'next-test-utils' } from 'next-test-utils'
let app let app
...@@ -38,7 +39,7 @@ function runDoubleMiddlewareTests() { ...@@ -38,7 +39,7 @@ function runDoubleMiddlewareTests() {
} }
async function writeRootMiddleware() { 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, force: true,
recursive: true, recursive: true,
}) })
...@@ -67,9 +68,9 @@ return response ...@@ -67,9 +68,9 @@ return response
} }
async function removeRootMiddleware() { async function removeRootMiddleware() {
await fs.rm(rootMiddlewareJSFile, { force: true }) await fs.remove(rootMiddlewareJSFile, { force: true })
await fs.rm(rootMiddlewareTSFile, { force: true }) await fs.remove(rootMiddlewareTSFile, { force: true })
await fs.rm(join(appDir, 'pages'), { force: true, recursive: true }) await fs.remove(join(appDir, 'pages'), { force: true, recursive: true })
} }
describe.each([ describe.each([
...@@ -100,13 +101,35 @@ describe.each([ ...@@ -100,13 +101,35 @@ describe.each([
}) })
describe('production mode', () => { describe('production mode', () => {
let exportOutput = ''
beforeAll(async () => { beforeAll(async () => {
await nextBuild(appDir) 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() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort)
}) })
afterAll(() => killApp(app)) 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() 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