From 0bf4b7f53b278080a751722f96fa9fa1e61c3c5f Mon Sep 17 00:00:00 2001 From: Alessio Gravili Date: Tue, 4 Nov 2025 19:34:57 -0800 Subject: [PATCH 1/2] fix(next): turbopack build support --- packages/next/src/withPayload.js | 84 ++++++++------------------------ 1 file changed, 21 insertions(+), 63 deletions(-) diff --git a/packages/next/src/withPayload.js b/packages/next/src/withPayload.js index 6e73f912658..acd99f7decf 100644 --- a/packages/next/src/withPayload.js +++ b/packages/next/src/withPayload.js @@ -15,24 +15,6 @@ export const withPayload = (nextConfig = {}, options = {}) => { env.NEXT_PUBLIC_ENABLE_ROUTER_CACHE_REFRESH = 'true' } - if (process.env.PAYLOAD_PATCH_TURBOPACK_WARNINGS !== 'false') { - const turbopackWarningText = - 'Packages that should be external need to be installed in the project directory, so they can be resolved from the output files.\nTry to install it into the project directory by running' - - const consoleWarn = console.warn - console.warn = (...args) => { - // Force to disable serverExternalPackages warnings: https://github.com/vercel/next.js/issues/68805 - if ( - (typeof args[1] === 'string' && args[1].includes(turbopackWarningText)) || - (typeof args[0] === 'string' && args[0].includes(turbopackWarningText)) - ) { - return - } - - consoleWarn(...args) - } - } - const poweredByHeader = { key: 'X-Powered-By', value: 'Next.js, Payload', @@ -88,25 +70,32 @@ export const withPayload = (nextConfig = {}, options = {}) => { }, serverExternalPackages: [ ...(nextConfig.serverExternalPackages || []), - 'drizzle-kit', - 'drizzle-kit/api', - 'pino', - 'libsql', - 'pino-pretty', - 'graphql', - // Do not bundle server-only packages during dev to improve compile speed + // These packages always need to be external, both during dev and production. This is because they install dependencies + // that will error when trying to bundle them (e.g. drizzle-kit, libsql, esbuild etc.). + // We cannot externalize those problem-packages directly. We can only externalize packages that are manually installed + // by the end user. Otherwise, the require('externalPackage') calls generated by the bundler would fail during runtime, + // as you cannot import dependencies of dependencies in a lot of package managers like pnpm. We'd have to force users + // to install the dependencies directly. + // Thus, we externalize the "entry-point" = the package that is installed by the end user, which would be our db adapters. + // + // + // External because it installs mongoose (part of default serverExternalPackages: https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/server-external-packages.json => would throw warning if we don't exclude the entry-point package): + '@payloadcms/db-mongodb', + // External because they install dependencies like drizzle, libsql, esbuild etc.: + '@payloadcms/db-postgres', + '@payloadcms/db-sqlite', + '@payloadcms/db-vercel-postgres', + '@payloadcms/drizzle', + // External because they install @aws-sdk/client-s3: + '@payloadcms/payload-cloud', + // TODO: We need to externalize @payloadcms/storage-s3 as well, once Next.js has the ability to exclude @payloadcms/storage-s3/client from being externalized. + // Do not bundle additional server-only packages during dev to improve compilation speed ...(process.env.NODE_ENV === 'development' && options.devBundleServerPackages === false ? [ 'payload', - '@payloadcms/db-mongodb', - '@payloadcms/db-postgres', - '@payloadcms/db-sqlite', - '@payloadcms/db-vercel-postgres', - '@payloadcms/drizzle', '@payloadcms/email-nodemailer', '@payloadcms/email-resend', '@payloadcms/graphql', - '@payloadcms/payload-cloud', '@payloadcms/plugin-redirects', // TODO: Add the following packages, excluding their /client subpath exports, once Next.js supports it //'@payloadcms/plugin-cloud-storage', @@ -129,21 +118,7 @@ export const withPayload = (nextConfig = {}, options = {}) => { return { ...incomingWebpackConfig, - externals: [ - ...(incomingWebpackConfig?.externals || []), - 'drizzle-kit', - 'drizzle-kit/api', - 'sharp', - 'libsql', - 'require-in-the-middle', - ], - ignoreWarnings: [ - ...(incomingWebpackConfig?.ignoreWarnings || []), - { module: /node_modules\/mongodb\/lib\/utils\.js/ }, - { file: /node_modules\/mongodb\/lib\/utils\.js/ }, - { module: /node_modules\/mongodb\/lib\/bson\.js/ }, - { file: /node_modules\/mongodb\/lib\/bson\.js/ }, - ], + externals: [...(incomingWebpackConfig?.externals || []), 'require-in-the-middle'], plugins: [ ...(incomingWebpackConfig?.plugins || []), // Fix cloudflare:sockets error: https://github.com/vercel/next.js/discussions/50177 @@ -151,23 +126,6 @@ export const withPayload = (nextConfig = {}, options = {}) => { resourceRegExp: /^pg-native$|^cloudflare:sockets$/, }), ], - resolve: { - ...(incomingWebpackConfig?.resolve || {}), - alias: { - ...(incomingWebpackConfig?.resolve?.alias || {}), - }, - fallback: { - ...(incomingWebpackConfig?.resolve?.fallback || {}), - '@aws-sdk/credential-providers': false, - '@mongodb-js/zstd': false, - aws4: false, - kerberos: false, - 'mongodb-client-encryption': false, - snappy: false, - 'supports-color': false, - 'yocto-queue': false, - }, - }, } }, } From ecaf240c262e3802371e70e2756ad1b2b8620b4f Mon Sep 17 00:00:00 2001 From: Alessio Gravili Date: Tue, 4 Nov 2025 22:58:41 -0800 Subject: [PATCH 2/2] attempt to fix flaky test --- test/plugin-multi-tenant/e2e.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/plugin-multi-tenant/e2e.spec.ts b/test/plugin-multi-tenant/e2e.spec.ts index b0d3bf73caa..3def510406d 100644 --- a/test/plugin-multi-tenant/e2e.spec.ts +++ b/test/plugin-multi-tenant/e2e.spec.ts @@ -577,6 +577,7 @@ test.describe('Multi Tenant', () => { page, serverURL, }) + await wait(1000) await goToListDoc({ cellClass: '.cell-name', @@ -584,10 +585,14 @@ test.describe('Multi Tenant', () => { textToMatch: 'Blue Dog', urlUtil: tenantsURL, }) + await wait(1000) await expect(page.locator('#field-name')).toBeVisible() await page.locator('#field-name').fill('Red Dog') + await wait(1000) + await saveDocAndAssert(page) + await wait(1000) await page.goto(tenantsURL.list) // Wait for backend tenant cache to update after save operation @@ -599,6 +604,7 @@ test.describe('Multi Tenant', () => { return (await getTenantOptions({ page })).sort() }) .toEqual(['Red Dog', 'Steel Cat', 'Public Tenant', 'Anchor Bar'].sort()) + await wait(1000) await goToListDoc({ cellClass: '.cell-name', @@ -606,10 +612,14 @@ test.describe('Multi Tenant', () => { textToMatch: 'Red Dog', urlUtil: tenantsURL, }) + await wait(1000) // Change the tenant back to the original name await page.locator('#field-name').fill('Blue Dog') + await wait(1000) + await saveDocAndAssert(page) + await wait(1000) await page.goto(tenantsURL.list) // Wait for backend tenant cache to update after save operation