All chapters
Hosting
Next.js on Scava - server components, server actions, route handlers, middleware
A Next.js app goes online with the same command as any other app. When it uses a server (server components that read data at each request, server actions, route handlers in app/api, middleware), the command builds it on your computer with OpenNext, the adapter Cloudflare maintains for Next.js. The result runs as the app's server code, in its own walled-off worker. An app built with output: 'export' is plain files and goes up as its out folder, see Hosting.
Put it online #
In the folder with package.json, the same as for every app:
npx -y scava-ai setup
Nothing to install or set up before. Your agent runs it as it is.
- The command sees that the folder is a Next.js app. When the project's packages are not installed yet, it installs them. When OpenNext and Wrangler are missing, it adds them to the project as development packages, once (
@opennextjs/cloudflare@1andwrangler@4), with the project's own package manager: npm, or pnpm, yarn or bun by their lock file. - It runs
next buildthrough OpenNext and bundles the server part with Wrangler, both from the project's ownnode_modules, on your computer. It takes a minute or two. - What it gives OpenNext, it writes into
node_modules/.cache/scava: the project itself stays as it is. Anopen-next.config.tsof the project is used, with Scava's page cache on top. - Only the result goes to Scava: the files from
public/and/_next/static/, and the server part asserver/index.js. Add.open-nextto.gitignore. - A
scava.jsonnext topackage.json(or inpublic/) is used as it is, with names, data rules, devices, embeds, schedules and webhooks. The command adds"server": { "routes": "all" }to it for the upload.
What happens on Scava #
- Every request that is not a file of the live version goes to the app's server code: pages, server actions, route handlers, middleware, and the app's own 404 page. Files such as
/_next/static/...and those frompublic/come straight from Scava. - Scava's sign-in stays in front of everything. Only signed-in people with access reach a page, so the app needs no login of its own. Remove NextAuth, Clerk or a login page when you move an app.
- Every page gets Scava's script and headers, as all apps do: the same content security policy, no framing by other sites, no password fields.
- Server actions and other changes come only from the app's own pages. Another site cannot post to them in the person's name.
- Every page view is one call of the server code and counts against the plan, with its computing time. See Limits.
Who is signed in, the data, keys #
getCloudflareContext() from OpenNext gives the same env as all server code, with env.SCAVA:
import { getCloudflareContext } from "@opennextjs/cloudflare";
export const dynamic = "force-dynamic"; // per person: computed at each request, never at build time
export default async function Page() {
const { env } = getCloudflareContext();
const me = await env.SCAVA.me(); // { email, name, role, groups, roles }
const mine = await env.SCAVA.db("tasks", { as: "me" }).list();
return <p>{me.email} has {mine.length} tasks</p>;
}
- A page that uses
env.SCAVAdepends on the person, so it must be computed at each request:export const dynamic = "force-dynamic", orawait connection()fromnext/serverfirst. A page Next.js renders at build time has nobody signed in, andgetCloudflareContext()refuses there. - The same works in server actions and route handlers. Everything of
env.SCAVAworks as in any server code: the database with the rules ofscava.json, files, mail, notifications, AI with your own key and agents. - Keys are secrets of the app, set on its page, and come as
env.NAME, as in all server code.
The page cache #
Scava keeps what Next.js caches between requests, with nothing to set up:
- pages with
export const revalidate = 60and pages without anything dynamic: computed once, then served from the cache. After the time, the next visitor gets the page once more as it was, and a new one is made in the background; fetch()withnext: { revalidate },unstable_cache, and"use cache"withcacheLifeandcacheTag(withexperimental: { useCache: true }innext.config, see below);revalidatePath,revalidateTagandupdateTagin server actions and route handlers.
The cache lives where the app keeps its data (see Data location) and goes with the app. It is a cache, not data: it does not count against the storage of the plan. One app keeps up to 1 GB and 100,000 entries, one entry up to 10 MB; beyond that the oldest make room. Every new version starts with an empty cache.
A cached page is the same for everyone who opens it. A page that asks who is asking (env.SCAVA.me(), or env.SCAVA.db(..., { as: "me" })) never goes into the cache. If Next.js would share such a page with everyone anyway (with revalidate, or as a static page), Scava does not send it: the visitor gets an error, and the app's problems say to give the page export const dynamic = "force-dynamic". So one person never sees a page made for another.
What does not work #
cacheComponents, which new apps of Next.js 16 switch on: Cloudflare Workers, where the server code of every app on Scava runs, cannot run it yet, and its pages would hang. OpenNext says so itself and is working on it. The command stops and says what to change, so your agent can do it: removecacheComponentsfromnext.config; if the app uses"use cache", setexperimental: { useCache: true }instead (cacheLifeandcacheTagkeep working); a page that must be computed at every request getsexport const dynamic = "force-dynamic". The page cache works as above. Once OpenNext can runcacheComponents, the command lets it through by itself.export const runtime = "edge": remove it. Everything runs in the Node.js runtime of Workers.- Next.js 14 and older: OpenNext builds Next.js 16 and the latest version of 15.
- What Workers cannot do: packages with native code such as
sharporbcrypt(usebcryptjs), writing files that last, child processes. The same as for all server code. - WebAssembly in the server part, for example
next/ogfor social images: the command stops and names the file. - Pictures are not resized.
next/imageworks and serves each picture as it is. Pictures from other sites thatimages.remotePatternsallows come through the server code; withimages: { unoptimized: true }the browser loads them itself, and their host belongs in"embeds". - Size: the server part must fit Cloudflare's limit for one worker (10 MB compressed). Scava's test app is about 1 MB compressed.
OpenNext warns that it is not fully compatible with Windows and recommends WSL. Scava's own test app was built on Windows and runs.
Moving from Vercel or Netlify #
- Take out the own login and read the person with
env.SCAVA.me(). - Data: move it to
env.SCAVA.db(), or keep your database and call it from server code with its key as a secret. See Move an existing app. - Environment variables become secrets of the app.
NEXT_PUBLIC_values are built into the pages and need no secret. - Cron jobs become schedules, and webhooks of other services get a webhook address of the app or a route handler.