Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bundling of Server Actions #51367

Merged
merged 5 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ export default async function getBaseWebpackConfig(
// so that the DefinePlugin can inject process.env values.

// Treat next internals as non-external for server layer
if (layer === WEBPACK_LAYERS.server) {
if (layer === WEBPACK_LAYERS.server || layer === WEBPACK_LAYERS.action) {
return
}

Expand Down Expand Up @@ -1520,7 +1520,7 @@ export default async function getBaseWebpackConfig(
(isEsm && isAppLayer)

if (/node_modules[/\\].*\.[mc]?js$/.test(res)) {
if (layer === WEBPACK_LAYERS.server) {
if (layer === WEBPACK_LAYERS.server || layer === WEBPACK_LAYERS.action) {
// All packages should be bundled for the server layer if they're not opted out.
// This option takes priority over the transpilePackages option.

Expand Down
9 changes: 9 additions & 0 deletions test/e2e/app-dir/actions/app-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ createNextDescribe(
files: __dirname,
dependencies: {
react: 'latest',
nanoid: 'latest',
'react-dom': 'latest',
'server-only': 'latest',
},
Expand Down Expand Up @@ -281,6 +282,14 @@ createNextDescribe(
}
})
})

it('should bundle external libraries if they are on the action layer', async () => {
await next.fetch('/client')
const pageBundle = await fs.readFile(
join(next.testDir, '.next', 'server', 'app', 'client', 'page.js')
)
expect(pageBundle.toString()).toContain('node_modules/nanoid/index.js')
})
}

describe('Edge SSR', () => {
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/app-dir/actions/app/client/actions-lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use server'

// Any arbitrary library just to ensure it's bundled.
// https://github.com/vercel/next.js/pull/51367
import nanoid from 'nanoid'

export async function test() {
console.log(nanoid)
}
4 changes: 4 additions & 0 deletions test/e2e/app-dir/actions/app/client/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { useState } from 'react'

import double, { inc, dec, redirectAction, getHeaders } from './actions'
import { test } from './actions-lib'

export default function Counter() {
const [count, setCount] = useState(0)
Expand Down Expand Up @@ -58,6 +59,9 @@ export default function Counter() {
submit
</button>
</form>
<form action={test}>
<button>test</button>
</form>
</div>
)
}