Interactive Learning Platform Nuxt layer

Isolated learning atmosphere · v4.x

← Back to writing
Step 1 of 9Nitro + Vite + Vue Router

The Ampyard Foundation: Nitro v3 + Vite + Vue

Scaffold Ampyard as a standalone Nitro v3 app, wire the Vue Router SSR entries, add the stage route every lesson embeds, and declare the Cloudflare bindings before the first car arrives.

Ampyard starts as a brand, a build, and one route that can render any stage of the depot. No bays yet. No ledger. Just the foundation the next eight lessons build on.

ampyard / preview / 1
Live stage Open app ↗
Ampyard · live stageStage 1 / 9

What it is#

A Nitro v3 app is a Vite app with a server. The nitro() Vite plugin builds two things from one config:

  1. A client bundle, with Vue and the browser router.
  2. An SSR service, a fetch handler that returns the first HTML response.

Nitro serves the routes in server/, runs the SSR service for unmatched paths, and bundles the whole thing into one .output/ directory. The same source runs on Node, Bun, Deno, or Cloudflare Workers.

The stage route is the same idea with a parameter: /preview/:stage. The blog iframe always loads a real stage URL. Stage 1 is not a separate demo; it is the final app with later features not enabled yet.

Why you'd care#

Nuxt 4.7 lets you opt into Nitro v3 inside a Nuxt app. That is the right migration path, but it hides the server behind Nuxt conventions. Building standalone makes the seams visible:

  • You choose the frontend. Here it is Vue Router, not Nuxt pages.
  • You choose the server entry. Here it is entry-server.ts.
  • You choose the deploy. Here it is Cloudflare Workers.

A tutorial that shows screenshots rots the moment the code changes. A tutorial that embeds the app at each stage stays honest: if a bay breaks, the lesson preview breaks. The stage route also forces the app to be decomposed by feature instead of by lesson.

What we're building#

Stage 1 is the Ampyard foundation:

  • A roadside brand shell: asphalt, electric lime, split-flap mono.
  • Vue Router SSR with one client entry and one server entry.
  • /preview/:stage, rendered by the same Vue component as /.
  • A deployable Cloudflare project with D1, an optional KV namespace, cron, and a Durable Object declared.
  • A stage rail that links every future lesson to a real URL.

Before#

A new Vite + Vue app is a counter and a logo. It has no server, no stage boundary, and no honest way to embed a later version of itself.

A plain Nitro API has the opposite problem: routes and bindings, but no UI to make them legible.

After#

The same project now has:

txt
apps/ampyard/
├─ app/
│  ├─ entry-client.ts       # hydrates the SSR HTML
│  ├─ entry-server.ts       # returns the first Response
│  ├─ routes.ts             # Vue Router routes
│  ├─ App.vue               # shell + stage context
│  ├─ pages/                # Depot, Stall, Queue, Ops
│  └─ components/
├─ server/
│  ├─ api/                  # JSON routes
│  ├─ routes/ws/ops.ts      # WebSocket radio
│  ├─ middleware/
│  ├─ tasks/                # cron + CLI tasks
│  └─ utils/
├─ shared/types.ts          # types used by app + server
├─ nitro.config.ts
└─ vite.config.ts

The Vite config is the whole build:

vite.config.ts
import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";
import { nitro } from "nitro/vite";

export default defineConfig({
  plugins: [vue({ exclude: /\?assets/ }), nitro()],
  environments: {
    client: {
      build: { rollupOptions: { input: "./app/entry-client.ts" } },
    },
    ssr: {
      build: { rollupOptions: { input: "./app/entry-server.ts" } },
    },
  },
});

nitro() registers the server environment. The client and ssr environments each get their own entry. The ?assets exclusion stops the Vue plugin from trying to parse Nitro's asset manifest as a component.

The server entry is a web-standard fetch handler:

app/entry-server.ts
import { createSSRApp } from "vue";
import { renderToString } from "vue/server-renderer";
import { createMemoryHistory, createRouter } from "vue-router";
import App from "./App.vue";
import { routes } from "./routes";
import clientAssets from "./entry-client.ts?assets=client";

async function handler(request: Request): Promise<Response> {
  const url = new URL(request.url);
  const app = createSSRApp(App);
  const router = createRouter({ history: createMemoryHistory(), routes });

  app.use(router);
  await router.push(url.pathname + url.search);
  await router.isReady();

  const rendered = await renderToString(app);
  return new Response(document(rendered), {
    headers: { "content-type": "text/html; charset=utf-8" },
  });
}

export default { fetch: handler };

The client entry hydrates the same tree with a browser history:

app/entry-client.ts
import { createSSRApp } from "vue";
import { createRouter, createWebHistory } from "vue-router";
import App from "./App.vue";
import { routes } from "./routes";

async function main() {
  const app = createSSRApp(App);
  const router = createRouter({ history: createWebHistory(), routes });
  app.use(router);
  await router.isReady();
  app.mount("#root");
}

void main();

The stage is derived from the URL in App.vue, not passed down from a server flag:

app/App.vue
const route = useRoute();
const stage = computed(() => stageFromPath(route.path));
provideStage(stage);

That one line is why clicking a stage chip updates the whole page without a reload. The URL is the state.

Do it yourself#

Node 22.22.3 or newer is required. Check it first:

bash
node -v
# v22.22.3 or later

Create a fresh Nitro app with the Vite template, then add Vue:

bash
npx nitro create ampyard --template vite --packageManager pnpm
cd ampyard
pnpm add vue vue-router
pnpm add -D @vitejs/plugin-vue

Or, since Ampyard already lives in this repo:

bash
pnpm install
pnpm --filter ampyard dev

Open:

txt
http://localhost:4321/
http://localhost:4321/preview/1

The embed=1 query is what the blog iframe adds. The app uses it to tighten the shell; the route itself is unchanged.

The live stage#

The preview at the top is /preview/1 in embed mode. The full app at / is the same code at stage 9. Clicking a chip in the stage rail navigates to /preview/2, /preview/3, and so on. The app does not reload; Vue Router swaps the page and the stage context updates.

Gotchas#

  • Node floor. Nitro v3 wants Node 22.22+. A Node 20 runtime will install and then fail in ways that look unrelated.
  • h3 v1 in a monorepo. If another app pins h3 to 1.x with a workspace-wide override, Nitro v3 will resolve h3 v1 and your event.req type will be an IncomingMessage. Scope the override to h3@1 so Nitro can resolve h3 v2.
  • ?assets=client. The SSR entry needs the client asset manifest to inject the right CSS and entry script. Keep the vue({ exclude: /\?assets/ }) option or Vite will try to compile the manifest.
  • Cloudflare placeholders. Stage 1 declares D1 and optional KV bindings with placeholder IDs so local Miniflare can boot. Replace them with real resource IDs at stage 9.
  • RC discipline. Pin nitro to the RC you tested. The series uses 3.0.261103-rc.
  • One source of truth. Do not pass the stage as a prop from the server entry. Derive it from the route so client navigation stays correct.

Recap#

You now have the smallest real version of Ampyard: a branded Nitro v3 + Vite + Vue app, a web-standard SSR entry, a stage route, and a deploy path. Stage 2 gives it routes that read like the web.

Next: Routes on Web Standards.