Unverified Commit 0800ea7e authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Error in next export when serverActions is enabled (#50039)

Reland #49959 as it was reverted in #50019

* Only error when runing `next export` or `output` is "export"
* Add e2e test


Closes NEXT-634
parent 771141dc
...@@ -3101,6 +3101,7 @@ export default async function build( ...@@ -3101,6 +3101,7 @@ export default async function build(
const options: ExportOptions = { const options: ExportOptions = {
isInvokedFromCli: false, isInvokedFromCli: false,
buildExport: false,
nextConfig: config, nextConfig: config,
hasAppDir, hasAppDir,
silent: true, silent: true,
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { resolve, join } from 'path' import { resolve, join } from 'path'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import arg from 'next/dist/compiled/arg/index.js' import arg from 'next/dist/compiled/arg/index.js'
import exportApp, { ExportError } from '../export' import exportApp, { ExportError, ExportOptions } from '../export'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import { printAndExit } from '../server/lib/utils' import { printAndExit } from '../server/lib/utils'
import { CliCommand } from '../lib/commands' import { CliCommand } from '../lib/commands'
...@@ -59,12 +59,13 @@ const nextExport: CliCommand = (argv) => { ...@@ -59,12 +59,13 @@ const nextExport: CliCommand = (argv) => {
printAndExit(`> No such directory exists as the project root: ${dir}`) printAndExit(`> No such directory exists as the project root: ${dir}`)
} }
const options = { const options: ExportOptions = {
silent: args['--silent'] || false, silent: args['--silent'] || false,
threads: args['--threads'], threads: args['--threads'],
outdir: args['--outdir'] ? resolve(args['--outdir']) : join(dir, 'out'), outdir: args['--outdir'] ? resolve(args['--outdir']) : join(dir, 'out'),
isInvokedFromCli: true, isInvokedFromCli: true,
hasAppDir: false, hasAppDir: false,
buildExport: false,
} }
exportApp(dir, options, nextExportCliSpan) exportApp(dir, options, nextExportCliSpan)
......
...@@ -148,7 +148,7 @@ export interface ExportOptions { ...@@ -148,7 +148,7 @@ export interface ExportOptions {
threads?: number threads?: number
debugOutput?: boolean debugOutput?: boolean
pages?: string[] pages?: string[]
buildExport?: boolean buildExport: boolean
statusMessage?: string statusMessage?: string
exportPageWorker?: typeof import('./worker').default exportPageWorker?: typeof import('./worker').default
exportAppPageWorker?: typeof import('./worker').default exportAppPageWorker?: typeof import('./worker').default
...@@ -179,9 +179,11 @@ export default async function exportApp( ...@@ -179,9 +179,11 @@ export default async function exportApp(
const threads = options.threads || nextConfig.experimental.cpus const threads = options.threads || nextConfig.experimental.cpus
const distDir = join(dir, nextConfig.distDir) const distDir = join(dir, nextConfig.distDir)
const isExportOutput = nextConfig.output === 'export'
// Running 'next export'
if (options.isInvokedFromCli) { if (options.isInvokedFromCli) {
if (nextConfig.output === 'export') { if (isExportOutput) {
Log.warn( Log.warn(
'"next export" is no longer needed when "output: export" is configured in next.config.js https://nextjs.org/docs/advanced-features/static-html-export' '"next export" is no longer needed when "output: export" is configured in next.config.js https://nextjs.org/docs/advanced-features/static-html-export'
) )
...@@ -196,6 +198,14 @@ export default async function exportApp( ...@@ -196,6 +198,14 @@ export default async function exportApp(
'"next export" is deprecated in favor of "output: export" in next.config.js https://nextjs.org/docs/advanced-features/static-html-export' '"next export" is deprecated in favor of "output: export" in next.config.js https://nextjs.org/docs/advanced-features/static-html-export'
) )
} }
// Running 'next export' or output is set to 'export'
if (options.isInvokedFromCli || isExportOutput) {
if (nextConfig.experimental.serverActions) {
throw new ExportError(
`Server Actions are not supported with static export.`
)
}
}
const telemetry = options.buildExport ? null : new Telemetry({ distDir }) const telemetry = options.buildExport ? null : new Telemetry({ distDir })
......
...@@ -126,7 +126,6 @@ describe('accumulateMetadata', () => { ...@@ -126,7 +126,6 @@ describe('accumulateMetadata', () => {
[{ themeColor: '#fff' }, null], [{ themeColor: '#fff' }, null],
] ]
const metadata = await accumulateMetadata(metadataItems) const metadata = await accumulateMetadata(metadataItems)
console.log('xxmetadata', metadata.themeColor)
expect(metadata).toMatchObject({ expect(metadata).toMatchObject({
themeColor: [{ color: '#fff' }], themeColor: [{ color: '#fff' }],
}) })
......
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app-dir action handling - next export',
{
files: __dirname,
skipStart: true,
skipDeployment: true,
},
({ next, isNextStart }) => {
if (!isNextStart) {
it('skip test for dev mode', () => {})
return
}
beforeAll(async () => {
await next.stop()
await next.patchFile(
'next.config.js',
`
module.exports = {
output: 'export',
experimental: {
serverActions: true,
},
}
`
)
try {
await next.start()
} catch {}
})
it('should error when use export output for server actions', async () => {
expect(next.cliOutput).toContain(
`Server Actions are not supported with static export.`
)
})
}
)
...@@ -3,7 +3,7 @@ import { Span } from 'next/src/trace' ...@@ -3,7 +3,7 @@ import { Span } from 'next/src/trace'
import { NextInstance } from './base' import { NextInstance } from './base'
export class NextDevInstance extends NextInstance { export class NextDevInstance extends NextInstance {
private _cliOutput: string private _cliOutput: string = ''
public get buildId() { public get buildId() {
return 'development' return 'development'
......
...@@ -6,7 +6,7 @@ import { Span } from 'next/src/trace' ...@@ -6,7 +6,7 @@ import { Span } from 'next/src/trace'
export class NextStartInstance extends NextInstance { export class NextStartInstance extends NextInstance {
private _buildId: string private _buildId: string
private _cliOutput: string private _cliOutput: string = ''
private spawnOpts: SpawnOptions private spawnOpts: SpawnOptions
public get buildId() { public get buildId() {
...@@ -53,6 +53,7 @@ export class NextStartInstance extends NextInstance { ...@@ -53,6 +53,7 @@ export class NextStartInstance extends NextInstance {
__NEXT_TEST_MODE: 'e2e', __NEXT_TEST_MODE: 'e2e',
}, },
} }
let buildArgs = ['yarn', 'next', 'build'] let buildArgs = ['yarn', 'next', 'build']
let startArgs = ['yarn', 'next', 'start'] let startArgs = ['yarn', 'next', 'start']
......
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