Skip to main content
This tutorial explains how to run a Next.js application inside a Blaxel sandbox and expose it securely using sandbox preview URLs.

Prerequisites

Before starting, ensure you have:
  • Blaxel CLI installed and authenticated (bl login)
  • Node.js 18+ installed
  • @blaxel/core package installed in your project (npm install @blaxel/core)

Architecture Overview

Running Next.js inside a Blaxel sandbox requires a few adjustments compared to local development:
  • Running the Next.js dev server on port 3000
  • Exposing the Next.js dev server via a Blaxel preview URL

Create a base sandbox image

Dockerfile

entrypoint.sh

Create an entrypoint script that starts the sandbox API and the dev server:

next.config.ts

Create a next.config.ts file to configure Next.js for use with Blaxel previews:
The allowedDevOrigins: ["*.preview.bl.run"] setting allows the dev server to accept requests from the Blaxel preview origin, which is required for Blaxel preview URLs to work correctly. If you have configured a custom domain in your Blaxel workspace, you should also add it to this array (e.g., ["*.preview.bl.run", "*.preview.mycompany.com"]).

blaxel.toml

Create a blaxel.toml file in the same directory as your Dockerfile:

Deploy the sandbox image

Deploy the image by running:

Create or reuse a sandbox

Create a sandbox from the base image:

Configure CORS for preview URL access

Next.js dev servers work well with permissive CORS headers when accessed through a preview URL:
Alternatively, you can use custom domains to expose previews on your own domain.

Create the preview URL

Next.js runs on port 3000, so we expose that port via a preview URL:

Generate a preview token

To securely access the preview, a token is required:

Start the dev server

If not using the entrypoint script, you can start the dev server programmatically:

Stream logs

To monitor the Next.js dev server output in real-time:

Access the Next.js application

Once everything is running, the Next.js application will be available at https://<PREVIEW-URL>?bl_preview_token=<TOKEN>

Complete example

Here is a full example combining all the steps:

Template features

Turbopack

The template uses Turbopack, Next.js’s Rust-based bundler, for significantly faster development builds. Turbopack provides:
  • Faster cold starts
  • Instant hot module replacement (HMR)
  • Optimized incremental compilation
The @next/swc-linux-x64-musl package is pre-installed for optimal performance on Alpine Linux.

App Router

The template comes pre-configured with the App Router (/app directory structure). This provides:
  • Server Components by default
  • Nested layouts
  • Loading and error states
  • Server Actions

TypeScript

Full TypeScript support is enabled out of the box with strict type checking.

Tailwind CSS

Tailwind CSS is pre-configured for styling. The src/app/globals.css file includes the Tailwind directives.

ESLint

ESLint is configured with Next.js recommended rules for code quality.
Last modified on July 10, 2026