From 6d1476ff1206a2cf18f91bd31eb8b811e0230e81 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Mon, 4 Mar 2024 16:44:10 +0100 Subject: [PATCH] add stricter check for 'use server' exports --- errors/invalid-use-server-value.mdx | 43 ++++++++++++ .../next-flight-loader/action-validate.ts | 17 ++++- test/e2e/app-dir/actions/app-action.test.ts | 69 ++++++++++++++++++- 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 errors/invalid-use-server-value.mdx diff --git a/errors/invalid-use-server-value.mdx b/errors/invalid-use-server-value.mdx new file mode 100644 index 0000000000000..a3a0a9786da32 --- /dev/null +++ b/errors/invalid-use-server-value.mdx @@ -0,0 +1,43 @@ +--- +title: 'Invalid "use server" Value' +--- + +## Why This Error Occurred + +This error occurs when a `"use server"` file exports a value that is not an async function. It might happen when you unintentionally export something like a configuration object, an arbitrary value, or missed the `async` keyword in the exported function declaration. + +These functions are required to be defined as async, because `"use server"` marks them as [Server Actions](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and they can be invoked directly from the client through a network request. + +Examples of incorrect code: + +```js +'use server' + +// ❌ This is incorrect: only async functions are allowed. +export const value = 1 + +// ❌ This is incorrect: missed the `async` keyword. +export function getServerData() { + return '...' +} +``` + +Correct code: + +```js +'use server' + +// ✅ This is correct: an async function is exported. +export async function getServerData() { + return '...' +} +``` + +## Possible Ways to Fix It + +Check all exported values in the `"use server"` file (including `export *`) and make sure that they are all defined as async functions. + +## Useful Links + +- [Server Actions and Mutations - Next.js](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations) +- ['use server' directive - React](https://react.dev/reference/react/use-server) diff --git a/packages/next/src/build/webpack/loaders/next-flight-loader/action-validate.ts b/packages/next/src/build/webpack/loaders/next-flight-loader/action-validate.ts index 08b9b53785f9f..19940619d67e9 100644 --- a/packages/next/src/build/webpack/loaders/next-flight-loader/action-validate.ts +++ b/packages/next/src/build/webpack/loaders/next-flight-loader/action-validate.ts @@ -6,7 +6,22 @@ export function ensureServerEntryExports(actions: any[]) { const action = actions[i] if (typeof action !== 'function') { throw new Error( - `A "use server" file can only export async functions, found ${typeof action}.` + `A "use server" file can only export async functions, found ${typeof action}.\nRead more: https://nextjs.org/docs/messages/invalid-use-server-value` + ) + } + + if (action.constructor.name !== 'AsyncFunction') { + const actionName: string = action.name || '' + + // Note: if it's from library code with `async` being transpiled to returning a promise, + // it would be annoying. But users can still wrap it in an async function to work around it. + throw new Error( + `A "use server" file can only export async functions.${ + // If the function has a name, we'll make the error message more specific. + actionName + ? ` Found "${actionName}" that is not an async function.` + : '' + }\nRead more: https://nextjs.org/docs/messages/invalid-use-server-value` ) } } diff --git a/test/e2e/app-dir/actions/app-action.test.ts b/test/e2e/app-dir/actions/app-action.test.ts index 46775c1cdd6ad..c4b1f95652da9 100644 --- a/test/e2e/app-dir/actions/app-action.test.ts +++ b/test/e2e/app-dir/actions/app-action.test.ts @@ -1,6 +1,6 @@ /* eslint-disable jest/no-standalone-expect */ import { createNextDescribe } from 'e2e-utils' -import { check, waitFor } from 'next-test-utils' +import { check, waitFor, getRedboxSource, hasRedbox } from 'next-test-utils' import type { Request, Response, Route } from 'playwright' import fs from 'fs-extra' import { join } from 'path' @@ -508,6 +508,73 @@ createNextDescribe( } if (isNextDev) { + describe('"use server" export values', () => { + it('should error when exporting non async functions at build time', async () => { + const filePath = 'app/server/actions.js' + const origContent = await next.readFile(filePath) + + try { + const browser = await next.browser('/server') + + const cnt = await browser.elementByCss('h1').text() + expect(cnt).toBe('0') + + // This can be caught by SWC directly + await next.patchFile( + filePath, + origContent + '\n\nexport const foo = 1' + ) + + expect(await hasRedbox(browser)).toBe(true) + expect(await getRedboxSource(browser)).toContain( + 'Only async functions are allowed to be exported in a "use server" file.' + ) + } finally { + await next.patchFile(filePath, origContent) + } + }) + + it('should error when exporting non async functions during runtime', async () => { + const logs: string[] = [] + next.on('stdout', (log) => { + logs.push(log) + }) + next.on('stderr', (log) => { + logs.push(log) + }) + + const filePath = 'app/server/actions.js' + const origContent = await next.readFile(filePath) + + try { + const browser = await next.browser('/server') + + const cnt = await browser.elementByCss('h1').text() + expect(cnt).toBe('0') + + // This requires the runtime to catch + await next.patchFile( + filePath, + origContent + '\n\nconst f = () => {}\nexport { f }' + ) + + await check( + () => + logs.some((log) => + log.includes( + 'Error: A "use server" file can only export async functions. Found "f" that is not an async function.' + ) + ) + ? 'true' + : '', + 'true' + ) + } finally { + await next.patchFile(filePath, origContent) + } + }) + }) + describe('HMR', () => { it('should support updating the action', async () => { const filePath = 'app/server/actions-3.js'