Next Sandbox is a lightweight package for testing and monitoring server actions in your Next.js application. It provides a simple UI to execute actions, view logs, and measure execution times.
- Execute Actions: Run server actions directly from the UI.
- View Logs & Metrics: Monitor execution status, logs, and performance metrics (AVG, P75, P95).
pnpm i next-sandbox
Create a dedicated route for sandbox usage and directly export withSandbox
in page.tsx
:
app/sandbox/page.tsx
import { withSandbox } from 'next-sandbox';
import { seedPosts } from './seed-posts';
export default withSandbox({
functions: [
{
name: 'Seed Posts',
function: seedPosts,
},
],
});
- functions: An array of server actions with a unique
name
and an asynchronousfunction
. - enable (optional): Boolean flag to enable/disable the sandbox route (default is
true
). When set tofalse
, it will callnotFound()
fromnext/navigation
, directly responding with a 404 error. A practical use case is to add anenable
check insidewithSandbox
to determine if the environment is in production, ensuring the sandbox route is only available in development or staging environments. For example:
import { withSandbox } from 'next-sandbox';
export default withSandbox({
enable: process.env.NODE_ENV !== 'production',
// ...
});
There are two ways to declare your server actions:
-
Define the function above
withSandbox
: Declare the function within the same file aswithSandbox
, ensuring that you include'use server'
at the beginning of the function to indicate that it is a server action. Then, reference this function insidewithSandbox
. -
Import the server action from another file: Store your server actions in separate files where
'use server'
is declared at the top of the file. Then, import and reference these functions insidewithSandbox
, as shown in the example at the top of this document.
withSandbox
and add 'use server'
inside it. This will not work correctly.