Unverified Commit d27cdc04 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Allow `"use cache"` with `output: 'export'` (#75794)

parent a181122a
...@@ -55,6 +55,8 @@ import { TurborepoAccessTraceResult } from '../build/turborepo-access-trace' ...@@ -55,6 +55,8 @@ import { TurborepoAccessTraceResult } from '../build/turborepo-access-trace'
import { createProgress } from '../build/progress' import { createProgress } from '../build/progress'
import type { DeepReadonly } from '../shared/lib/deep-readonly' import type { DeepReadonly } from '../shared/lib/deep-readonly'
import { isInterceptionRouteRewrite } from '../lib/generate-interception-routes-rewrites' import { isInterceptionRouteRewrite } from '../lib/generate-interception-routes-rewrites'
import type { ActionManifest } from '../build/webpack/plugins/flight-client-entry-plugin'
import { extractInfoFromServerReferenceId } from '../shared/lib/server-reference-info'
export class ExportError extends Error { export class ExportError extends Error {
code = 'NEXT_EXPORT_ERROR' code = 'NEXT_EXPORT_ERROR'
...@@ -300,11 +302,11 @@ async function exportAppImpl( ...@@ -300,11 +302,11 @@ async function exportAppImpl(
} }
} }
let serverActionsManifest let serverActionsManifest: ActionManifest | undefined
if (enabledDirectories.app) { if (enabledDirectories.app) {
serverActionsManifest = require( serverActionsManifest = require(
join(distDir, SERVER_DIRECTORY, SERVER_REFERENCE_MANIFEST + '.json') join(distDir, SERVER_DIRECTORY, SERVER_REFERENCE_MANIFEST + '.json')
) ) as ActionManifest
if (nextConfig.output === 'export') { if (nextConfig.output === 'export') {
const routesManifest = require(join(distDir, ROUTES_MANIFEST)) const routesManifest = require(join(distDir, ROUTES_MANIFEST))
...@@ -322,9 +324,16 @@ async function exportAppImpl( ...@@ -322,9 +324,16 @@ async function exportAppImpl(
} }
} }
const actionIds = [
...Object.keys(serverActionsManifest.node),
...Object.keys(serverActionsManifest.edge),
]
if ( if (
Object.keys(serverActionsManifest.node).length > 0 || actionIds.some(
Object.keys(serverActionsManifest.edge).length > 0 (actionId) =>
extractInfoFromServerReferenceId(actionId).type === 'server-action'
)
) { ) {
throw new ExportError( throw new ExportError(
`Server Actions are not supported with static export.\nRead more: https://nextjs.org/docs/app/building-your-application/deploying/static-exports#unsupported-features` `Server Actions are not supported with static export.\nRead more: https://nextjs.org/docs/app/building-your-application/deploying/static-exports#unsupported-features`
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
'use cache'
export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random'
).then((response) => response.text())
return <p>{data}</p>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
useCache: true,
},
output: 'export',
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import { renderViaHTTP, startCleanStaticServer } from 'next-test-utils'
import { join } from 'path'
import { AddressInfo, Server } from 'net'
describe('use-cache-output-export', () => {
const { next, isNextStart } = nextTestSetup({
files: __dirname,
skipDeployment: true,
skipStart: process.env.NEXT_TEST_MODE !== 'dev',
})
if (process.env.__NEXT_EXPERIMENTAL_PPR === 'true') {
return it.skip('for PPR', () => {
// PPR is not compatible with `output: 'export'`.
})
}
it('should work', async () => {
let html: string
let server: Server | undefined
if (isNextStart) {
const { cliOutput } = await next.build()
expect(cliOutput).not.toInclude(
'Server Actions are not supported with static export.'
)
server = await startCleanStaticServer(join(next.testDir, 'out'))
const { port } = server.address() as AddressInfo
html = await renderViaHTTP(port, '/')
} else {
html = await next.render('/')
}
expect(html).toMatch(/<p>[0,1]\.\d+<\/p>/)
if (server) {
await new Promise((resolve) => server.close(resolve))
}
})
})
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