Unverified Commit 99de0573 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Revert server action optimization (#69925)

parent 24647b9a
...@@ -939,7 +939,7 @@ export const defaultConfig: NextConfig = { ...@@ -939,7 +939,7 @@ export const defaultConfig: NextConfig = {
: false, : false,
webpackBuildWorker: undefined, webpackBuildWorker: undefined,
missingSuspenseWithCSRBailout: true, missingSuspenseWithCSRBailout: true,
optimizeServerReact: false, optimizeServerReact: true,
useEarlyImport: false, useEarlyImport: false,
staleTimes: { staleTimes: {
dynamic: 30, dynamic: 30,
......
// @ts-ignore avoid ts errors during manual testing
import { type NextInstance } from 'e2e-utils'
async function getActionsMappingByRuntime(
next: NextInstance,
runtime: 'node' | 'edge'
) {
const manifest = JSON.parse(
await next.readFile('.next/server/server-reference-manifest.json')
)
return manifest[runtime]
}
export function markLayoutAsEdge(next: NextInstance) {
beforeAll(async () => {
await next.stop()
const layoutContent = await next.readFile('app/layout.js')
await next.patchFile(
'app/layout.js',
layoutContent + `\nexport const runtime = 'edge'`
)
await next.start()
})
}
/*
{
[route path]: { [layer]: Set<workerId> ]
}
*/
type ActionsMappingOfRuntime = {
[actionId: string]: {
workers: {
[route: string]: string
}
layer: {
[route: string]: string
}
}
}
type ActionState = {
[route: string]: {
[layer: string]: number
}
}
function getActionsRoutesState(
actionsMappingOfRuntime: ActionsMappingOfRuntime
): ActionState {
const state: ActionState = {}
Object.keys(actionsMappingOfRuntime).forEach((actionId) => {
const action = actionsMappingOfRuntime[actionId]
const routePaths = Object.keys(action.workers)
routePaths.forEach((routePath) => {
if (!state[routePath]) {
state[routePath] = {}
}
const layer = action.layer[routePath]
if (!state[routePath][layer]) {
state[routePath][layer] = 0
}
state[routePath][layer]++
})
})
return state
}
export async function getActionsRoutesStateByRuntime(next: NextInstance) {
const actionsMappingOfRuntime = await getActionsMappingByRuntime(
next,
process.env.TEST_EDGE ? 'edge' : 'node'
)
return getActionsRoutesState(actionsMappingOfRuntime)
}
'use server'
export async function serverComponentAction() {
return 'server-action'
}
export async function clientComponentAction() {
return 'client-action'
}
export async function unusedExportedAction() {
return 'unused-exported-action'
}
'use client'
import { useState } from 'react'
import { clientComponentAction } from '../actions'
export default function Page() {
const [text, setText] = useState('initial')
return (
<div>
<button
id="action-1"
onClick={async () => {
setText(await clientComponentAction())
}}
>
Action 1
</button>
<span>{text}</span>
</div>
)
}
export default function Page() {
// Inline Server Action
async function inlineServerAction() {
'use server'
return 'inline-server-action'
}
return (
<form action={inlineServerAction}>
<button type="submit">Submit</button>
</form>
)
}
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { serverComponentAction } from '../actions'
export default function Page() {
return (
<form>
<input type="text" placeholder="input" />
<button formAction={serverComponentAction}>submit</button>
</form>
)
}
import { nextTestSetup } from 'e2e-utils'
import {
getActionsRoutesStateByRuntime,
markLayoutAsEdge,
} from '../_testing/utils'
describe('actions-tree-shaking - basic', () => {
const { next } = nextTestSetup({
files: __dirname,
})
if (process.env.TEST_EDGE) {
markLayoutAsEdge(next)
}
it('should not have the unused action in the manifest', async () => {
const actionsRoutesState = await getActionsRoutesStateByRuntime(next)
expect(actionsRoutesState).toMatchObject({
// only one server layer action
'app/server/page': {
rsc: 1,
},
// only one browser layer action
'app/client/page': {
'action-browser': 1,
},
'app/inline/page': {
rsc: 1,
},
})
})
})
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
'use server'
export async function esmModuleTypeAction() {
return 'esm-module-type-action'
}
export async function cjsModuleTypeAction() {
return 'cjs-module-type-action'
}
export async function unusedModuleTypeAction1() {
return 'unused-module-type-action-1'
}
const { cjsModuleTypeAction } = require('./actions')
export default function Page() {
return (
<div>
<h3>One</h3>
<form>
<input type="text" placeholder="input" />
<button formAction={cjsModuleTypeAction}>submit</button>
</form>
</div>
)
}
'use server'
export async function esmModuleTypeAction() {
return 'esm-module-type-action'
}
export async function cjsModuleTypeAction() {
return 'cjs-module-type-action'
}
export async function unusedModuleTypeAction1() {
return 'unused-module-type-action-1'
}
import { esmModuleTypeAction } from './actions'
export default function Page() {
return (
<div>
<h3>One</h3>
<form>
<input type="text" placeholder="input" />
<button formAction={esmModuleTypeAction}>submit</button>
</form>
</div>
)
}
import { nextTestSetup } from 'e2e-utils'
import {
getActionsRoutesStateByRuntime,
markLayoutAsEdge,
} from '../_testing/utils'
describe('actions-tree-shaking - mixed-module-actions', () => {
const { next } = nextTestSetup({
files: __dirname,
})
if (process.env.TEST_EDGE) {
markLayoutAsEdge(next)
}
it('should not do tree shake for cjs module when import server actions', async () => {
const actionsRoutesState = await getActionsRoutesStateByRuntime(next)
expect(actionsRoutesState).toMatchObject({
'app/mixed-module/esm/page': {
rsc: 1,
},
// CJS import is not able to tree shake, so it will include all actions
'app/mixed-module/cjs/page': {
rsc: 3,
},
})
})
})
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
'use server'
export async function sharedClientLayerAction() {
return 'shared-client-layer-action'
}
export async function unusedClientLayerAction1() {
return 'unused-client-layer-action-1'
}
export async function unusedClientLayerAction2() {
return 'unused-client-layer-action-2'
}
'use client'
import { useState } from 'react'
import { sharedClientLayerAction } from './reexport-action'
export default function Page() {
const [text, setText] = useState('initial')
return (
<div>
<button
id="action-1"
onClick={async () => {
setText(await sharedClientLayerAction())
}}
>
Action 1
</button>
<span>{text}</span>
</div>
)
}
export {
sharedClientLayerAction,
unusedClientLayerAction1,
unusedClientLayerAction2,
} from './actions'
'use server'
export async function sharedServerLayerAction() {
return 'shared-server-layer-action'
}
export async function unusedServerLayerAction1() {
return 'unused-server-layer-action-1'
}
export async function unusedServerLayerAction2() {
return 'unused-server-layer-action-2'
}
import { sharedServerLayerAction } from './reexport-action'
export default function Page() {
return (
<div>
<form>
<input type="text" placeholder="input" />
<button formAction={sharedServerLayerAction}>submit</button>
</form>
</div>
)
}
export {
sharedServerLayerAction,
unusedServerLayerAction1,
unusedServerLayerAction2,
} from './actions'
'use server'
export async function sharedClientLayerAction() {
return 'shared-client-layer-action'
}
export async function unusedClientLayerAction1() {
return 'unused-client-layer-action-1'
}
export async function unusedClientLayerAction2() {
return 'unused-client-layer-action-2'
}
'use client'
import { useState } from 'react'
import * as actionMod from './actions'
export default function Page() {
const [text, setText] = useState('initial')
return (
<div>
<button
id="action-1"
onClick={async () => {
setText(await actionMod.sharedClientLayerAction())
}}
>
Action 1
</button>
<span>{text}</span>
</div>
)
}
'use server'
export async function sharedServerLayerAction() {
return 'shared-server-layer-action'
}
export async function unusedServerLayerAction1() {
return 'unused-server-layer-action-1'
}
export async function unusedServerLayerAction2() {
return 'unused-server-layer-action-2'
}
import * as actionMod from './actions'
export default function Page() {
return (
<div>
<form>
<input type="text" placeholder="input" />
<button formAction={actionMod.sharedServerLayerAction}>submit</button>
</form>
</div>
)
}
import { nextTestSetup } from 'e2e-utils'
import {
getActionsRoutesStateByRuntime,
markLayoutAsEdge,
} from '../_testing/utils'
describe('actions-tree-shaking - reexport', () => {
const { next } = nextTestSetup({
files: __dirname,
})
if (process.env.TEST_EDGE) {
markLayoutAsEdge(next)
}
it('should not have the unused action in the manifest', async () => {
const actionsRoutesState = await getActionsRoutesStateByRuntime(next)
expect(actionsRoutesState).toMatchObject({
'app/namespace-reexport/server/page': {
rsc: 1,
},
'app/namespace-reexport/client/page': {
'action-browser': 1,
},
// We're not able to tree-shake these re-exports here
'app/named-reexport/server/page': {
rsc: 3,
},
'app/named-reexport/client/page': {
'action-browser': 3,
},
})
})
})
'use server'
export async function sharedClientLayerAction() {
return 'shared-client-layer-action'
}
export async function unusedClientLayerAction1() {
return 'unused-client-layer-action-1'
}
export async function unusedClientLayerAction2() {
return 'unused-client-layer-action-2'
}
'use client'
import { useState } from 'react'
import { sharedClientLayerAction } from '../actions'
export default function Page() {
const [text, setText] = useState('initial')
return (
<div>
<h1>One</h1>
<button
id="action-1"
onClick={async () => {
setText(await sharedClientLayerAction())
}}
>
Action 1
</button>
<span>{text}</span>
</div>
)
}
'use client'
import { useState } from 'react'
import { sharedClientLayerAction } from '../actions'
export default function Page() {
const [text, setText] = useState('initial')
return (
<div>
<h1>Two</h1>
<button
id="action-1"
onClick={async () => {
setText(await sharedClientLayerAction())
}}
>
Action 1
</button>
<span>{text}</span>
</div>
)
}
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
'use server'
export async function sharedServerLayerAction() {
return 'shared-server-layer-action'
}
export async function unusedServerLayerAction1() {
return 'unused-server-layer-action-1'
}
export async function unusedServerLayerAction2() {
return 'unused-server-layer-action-2'
}
import { sharedServerLayerAction } from '../actions'
export default function Page() {
return (
<div>
<h3>One</h3>
<form>
<input type="text" placeholder="input" />
<button formAction={sharedServerLayerAction}>submit</button>
</form>
</div>
)
}
import { sharedServerLayerAction } from '../actions'
export default function Page() {
return (
<div>
<h3>One</h3>
<form>
<input type="text" placeholder="input" />
<button formAction={sharedServerLayerAction}>submit</button>
</form>
</div>
)
}
import { nextTestSetup } from 'e2e-utils'
import {
getActionsRoutesStateByRuntime,
markLayoutAsEdge,
} from '../_testing/utils'
describe('actions-tree-shaking - shared-module-actions', () => {
const { next } = nextTestSetup({
files: __dirname,
})
if (process.env.TEST_EDGE) {
markLayoutAsEdge(next)
}
it('should not have the unused action in the manifest', async () => {
const actionsRoutesState = await getActionsRoutesStateByRuntime(next)
expect(actionsRoutesState).toMatchObject({
'app/server/one/page': {
rsc: 1,
},
'app/server/two/page': {
rsc: 1,
},
'app/client/one/page': {
'action-browser': 1,
},
'app/client/two/page': {
'action-browser': 1,
},
})
})
})
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
'use server'
export async function action1() {
return { hello: 'world' }
}
export async function action2() {
return { hello: 'action2' }
}
export async function action3() {
return { hello: 'action3' }
}
import { action3 } from './actions'
export default function MyComponent() {
// to prevent tree-shaking
if (globalThis.DO_NOT_TREE_SHAKE) {
console.log('MyComponent imported action3', action3)
}
return <span>i'm MyComponent</span>
}
'use client'
import React, { useEffect } from 'react'
import { action1, action2 } from './actions'
import MyComponent from './my-component'
export default function Page() {
// to prevent tree-shaking
if (globalThis.DO_NOT_TREE_SHAKE) {
console.log('Page imported action2', action2)
}
// calling action1 fails!!
useEffect(() => {
if (globalThis.DO_NOT_TREE_SHAKE) {
action1().then((obj) => {
console.log('action1 returned:', obj)
})
}
}, [])
return <MyComponent />
}
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
]
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
import { nextTestSetup } from 'e2e-utils'
import {
getActionsRoutesStateByRuntime,
markLayoutAsEdge,
} from '../_testing/utils'
describe('actions-tree-shaking - use-effect-actions', () => {
const { next } = nextTestSetup({
files: __dirname,
})
if (process.env.TEST_EDGE) {
markLayoutAsEdge(next)
}
it('should not tree shake the used action under useEffect', async () => {
const actionsRoutesState = await getActionsRoutesStateByRuntime(next)
expect(actionsRoutesState).toMatchObject({
'app/mixed/page': {
'action-browser': 3,
},
})
})
})
...@@ -15529,96 +15529,6 @@ ...@@ -15529,96 +15529,6 @@
"pending": [], "pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
},
"test/production/app-dir/actions-tree-shaking/basic/basic.test.ts": {
"passed": [],
"failed": [
"actions-tree-shaking - basic should not have the unused action in the manifest"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/production/app-dir/actions-tree-shaking/mixed-module-actions/mixed-module-actions.test.ts": {
"passed": [],
"failed": [
"actions-tree-shaking - mixed-module-actions should not do tree shake for cjs module when import server actions"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/production/app-dir/actions-tree-shaking/reexport/reexport.test.ts": {
"passed": [],
"failed": [
"actions-tree-shaking - reexport should not have the unused action in the manifest"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/production/app-dir/actions-tree-shaking/shared-module-actions/shared-module-actions.test.ts": {
"passed": [],
"failed": [
"actions-tree-shaking - shared-module-actions should not have the unused action in the manifest"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/production/app-dir/actions-tree-shaking/basic/basic-edge.test.ts": {
"passed": [],
"failed": [
"actions-tree-shaking - basic should not have the unused action in the manifest"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/production/app-dir/actions-tree-shaking/mixed-module-actions/mixed-module-actions-edge.test.ts": {
"passed": [],
"failed": [
"actions-tree-shaking - mixed-module-actions should not do tree shake for cjs module when import server actions"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/production/app-dir/actions-tree-shaking/reexport/reexport-edge.test.ts": {
"passed": [],
"failed": [
"actions-tree-shaking - reexport should not have the unused action in the manifest"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/production/app-dir/actions-tree-shaking/shared-module-actions/shared-module-actions-edge.test.ts": {
"passed": [],
"failed": [
"actions-tree-shaking - shared-module-actions should not have the unused action in the manifest"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/production/app-dir/actions-tree-shaking/use-effect-actions/use-effect-actions-edge.test.ts": {
"passed": [],
"failed": [
"actions-tree-shaking - use-effect-actions should not tree shake the used action under useEffect"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
"test/production/app-dir/actions-tree-shaking/use-effect-actions/use-effect-actions.test.ts": {
"passed": [],
"failed": [
"actions-tree-shaking - use-effect-actions should not tree shake the used action under useEffect"
],
"pending": [],
"flakey": [],
"runtimeError": false
} }
}, },
"rules": { "rules": {
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