Skip to content

Next.js template

This template installs Next.js 16 and starts its development server on port 3000. Keep package.json, app/layout.jsx, and app/page.jsx next to the example source.

mjs
import { Sandbox, Template, waitForURL } from "@abox-dev/sdk";

const templateName = process.env.TEMPLATE_NAME ?? "nextjs-app";
const template = Template({ fileContextPath: "." })
  .fromNodeImage("24-bookworm-slim")
  .setWorkdir("/home/user/app")
  .copy("package.json", "/home/user/app/package.json")
  .copy("app", "/home/user/app/app")
  .runCmd("npm install")
  .setStartCmd(
    "npm run dev -- --hostname 0.0.0.0",
    waitForURL("http://localhost:3000"),
  );

await Template.build(template, templateName, {
  cpuCount: 2,
  memoryMB: 2_048,
});

const sandbox = await Sandbox.create(templateName);
try {
  const page = await sandbox.commands.run(
    "curl --silent http://localhost:3000",
  );
  console.log(page.stdout);
} finally {
  await sandbox.kill();
}

The definition copies package.json and runs npm install before it copies the application. When a page changes, AgentBox can reuse the dependency layer.

The ready command waits for Next.js to respond. The server is therefore already available at http://localhost:3000 inside the sandbox after Sandbox.create.