Skip to content

Commit

Permalink
Error in next export when serverActions is enabled (#50039)
Browse files Browse the repository at this point in the history
Reland #49959 as it was reverted in #50019

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


Closes NEXT-634
  • Loading branch information
huozhi committed May 19, 2023
1 parent 771141d commit 0800ea7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3101,6 +3101,7 @@ export default async function build(

const options: ExportOptions = {
isInvokedFromCli: false,
buildExport: false,
nextConfig: config,
hasAppDir,
silent: true,
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/cli/next-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { resolve, join } from 'path'
import { existsSync } from 'fs'
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 { printAndExit } from '../server/lib/utils'
import { CliCommand } from '../lib/commands'
Expand Down Expand Up @@ -59,12 +59,13 @@ const nextExport: CliCommand = (argv) => {
printAndExit(`> No such directory exists as the project root: ${dir}`)
}

const options = {
const options: ExportOptions = {
silent: args['--silent'] || false,
threads: args['--threads'],
outdir: args['--outdir'] ? resolve(args['--outdir']) : join(dir, 'out'),
isInvokedFromCli: true,
hasAppDir: false,
buildExport: false,
}

exportApp(dir, options, nextExportCliSpan)
Expand Down
14 changes: 12 additions & 2 deletions packages/next/src/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export interface ExportOptions {
threads?: number
debugOutput?: boolean
pages?: string[]
buildExport?: boolean
buildExport: boolean
statusMessage?: string
exportPageWorker?: typeof import('./worker').default
exportAppPageWorker?: typeof import('./worker').default
Expand Down Expand Up @@ -179,9 +179,11 @@ export default async function exportApp(

const threads = options.threads || nextConfig.experimental.cpus
const distDir = join(dir, nextConfig.distDir)
const isExportOutput = nextConfig.output === 'export'

// Running 'next export'
if (options.isInvokedFromCli) {
if (nextConfig.output === 'export') {
if (isExportOutput) {
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'
)
Expand All @@ -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'
)
}
// 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 })

Expand Down
1 change: 0 additions & 1 deletion packages/next/src/lib/metadata/resolve-metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ describe('accumulateMetadata', () => {
[{ themeColor: '#fff' }, null],
]
const metadata = await accumulateMetadata(metadataItems)
console.log('xxmetadata', metadata.themeColor)
expect(metadata).toMatchObject({
themeColor: [{ color: '#fff' }],
})
Expand Down
40 changes: 40 additions & 0 deletions test/e2e/app-dir/actions/app-action-export.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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.`
)
})
}
)
2 changes: 1 addition & 1 deletion test/lib/next-modes/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Span } from 'next/src/trace'
import { NextInstance } from './base'

export class NextDevInstance extends NextInstance {
private _cliOutput: string
private _cliOutput: string = ''

public get buildId() {
return 'development'
Expand Down
3 changes: 2 additions & 1 deletion test/lib/next-modes/next-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Span } from 'next/src/trace'

export class NextStartInstance extends NextInstance {
private _buildId: string
private _cliOutput: string
private _cliOutput: string = ''
private spawnOpts: SpawnOptions

public get buildId() {
Expand Down Expand Up @@ -53,6 +53,7 @@ export class NextStartInstance extends NextInstance {
__NEXT_TEST_MODE: 'e2e',
},
}

let buildArgs = ['yarn', 'next', 'build']
let startArgs = ['yarn', 'next', 'start']

Expand Down

0 comments on commit 0800ea7

Please sign in to comment.