The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | title: Next.js |
| 3 | description: Deploy a Next.js app to Cloud Run with GCP.Website.Nextjs — Next's own production server in a container, no OpenNext. |
| 4 | sidebar: |
| 5 | order: 12 |
| 6 | --- |
| 7 | |
| 8 | import PackageManagers from "../../../../components/PackageManagers.astro"; |
| 9 | |
| 10 | export const devDeps = "@alchemy.run/frontend-frameworks"; |
| 11 | |
| 12 | `GCP.Website.Nextjs` deploys a [Next.js](https://nextjs.org) app to |
| 13 | Cloud Run. Alchemy runs `next build` and serves the app with Next's own |
| 14 | production server. There is no OpenNext: App Router, route handlers, |
| 15 | and server components run exactly as they do on `next start`. |
| 16 | |
| 17 | ## Install |
| 18 | |
| 19 | The build integration ships separately. It only runs at deploy time, |
| 20 | so a dev dependency is enough: |
| 21 | |
| 22 | <PackageManagers dev={devDeps} /> |
| 23 | |
| 24 | Docker has to be running when you deploy. The image is built on your |
| 25 | machine. |
| 26 | |
| 27 | ## Deploy the site |
| 28 | |
| 29 | ```typescript |
| 30 | // alchemy.run.ts |
| 31 | import * as Alchemy from "alchemy"; |
| 32 | import * as GCP from "@alchemy.run/gcp"; |
| 33 | import * as Effect from "effect/Effect"; |
| 34 | |
| 35 | export const Website = GCP.Website.Nextjs("Website"); |
| 36 | |
| 37 | export default Alchemy.Stack( |
| 38 | "MySite", |
| 39 | { providers: GCP.providers(), state: GCP.state() }, |
| 40 | Effect.gen(function* () { |
| 41 | const site = yield* Website; |
| 42 | return { url: site.url }; |
| 43 | }), |
| 44 | ); |
| 45 | ``` |
| 46 | |
| 47 | Run `alchemy deploy`. `site.url` is the Cloud Run service URL. |
| 48 | |
| 49 | ## Pass environment variables |
| 50 | |
| 51 | ```diff lang="typescript" |
| 52 | // alchemy.run.ts |
| 53 | -export const Website = GCP.Website.Nextjs("Website"); |
| 54 | +export const Website = GCP.Website.Nextjs("Website", { |
| 55 | + env: { |
| 56 | + GREETING: "Hello from Cloud Run!", |
| 57 | + }, |
| 58 | +}); |
| 59 | ``` |
| 60 | |
| 61 | Dynamic pages and route handlers read it from `process.env`: |
| 62 | |
| 63 | ```typescript |
| 64 | // app/api/hello/route.ts |
| 65 | export function GET() { |
| 66 | return Response.json({ greeting: process.env.GREETING }); |
| 67 | } |
| 68 | ``` |
| 69 | |
| 70 | ## Configure Next.js |
| 71 | |
| 72 | Your `next.config.*` is used as-is. Alchemy ships `.next/`, `public/`, |
| 73 | and the config into the image and installs `next`, `react`, and |
| 74 | `react-dom` there. |
| 75 | |
| 76 | ## Caching |
| 77 | |
| 78 | Each Cloud Run instance keeps its own Next.js cache in memory. |
| 79 | Instances don't share it, so revalidated pages can differ between |
| 80 | instances for a moment. Keep one warm instance, or cap |
| 81 | `maxInstanceCount`, if that matters. |
| 82 | |
| 83 | ## Tune Cloud Run |
| 84 | |
| 85 | ```typescript |
| 86 | export const Website = GCP.Website.Nextjs("Website", { |
| 87 | scaling: { minInstanceCount: 1 }, |
| 88 | resources: { limits: { cpu: "1", memory: "1Gi" } }, |
| 89 | }); |
| 90 | ``` |
| 91 | |
| 92 | One warm instance means no cold start for the first visitor. See |
| 93 | [Websites](/gcp/frontend/websites#tune-cloud-run) for the other |
| 94 | settings. |
| 95 | |
| 96 | ## Local development |
| 97 | |
| 98 | `alchemy dev` runs the framework's dev server with hot reload and |
| 99 | creates nothing in the cloud. Wrap the site in `Alchemy.remote()` to |
| 100 | deploy it for real during dev. |
| 101 | |
| 102 | ## Example |
| 103 | |
| 104 | [`examples/gcp-website-nextjs`](https://github.com/alchemy-run/alchemy/tree/main/examples/gcp-website-nextjs) |
| 105 | has a dynamic page, a route handler, Tailwind, and a live test. |
| 106 | |
| 107 | ## Where next |
| 108 | |
| 109 | - [Websites](/gcp/frontend/websites) — the settings every site shares. |
| 110 | - [Website reference](/providers/gcp/website#nextjs) — every prop. |