# Deploying

Deploy to a static host or a server.

`vuesho build` builds the site for a static host or for a server. A site needs a server only when something renders on request, like the pages of a [dynamic source](/dynamic-rendering#dynamic-sources) or the [AI chat](/ai#ai-chat).

## Building the Site [#building-the-site]

```npm
npm run build
```

By default, the build writes the pages and assets to `.output/public`, and the server to `.output/server`. To check the build before you deploy it, serve it locally:

```npm
npm run preview
```

## Static Hosting [#static-hosting]

For a static host, set Nitro's [`static`](https://nitro.build/config/general#static) option in `vite.config.ts`:

```ts title="vite.config.ts"
import { defineConfig } from "vite";
import vuesho from "vuesho/vite";

export default defineConfig({
  plugins: [vuesho()],
  nitro: { static: true }, // [!code highlight]
});
```

The build then writes only static files, to `.output/public` by default, which any static host serves. A dynamic source is read once, when the site builds. When something else needs a server, the build names it in an error or a warning.

### GitHub Pages [#github-pages]

1. Set the `github-pages` preset, which is static. For a project site, like `https://acme.github.io/docs/`, also set the [base path](#base-path):

   ```ts title="vite.config.ts"
   import { defineConfig } from "vite";
   import vuesho from "vuesho/vite";

   export default defineConfig({
     base: "/docs/",
     plugins: [vuesho()],
     nitro: { preset: "github-pages" },
   });
   ```

2. Add a workflow:

   ```yaml title=".github/workflows/deploy.yml"
   name: Deploy
   on:
     push:
       branches: [main]

   permissions:
     contents: read
     pages: write
     id-token: write

   jobs:
     deploy:
       runs-on: ubuntu-latest
       environment:
         name: github-pages
         url: ${{ steps.deployment.outputs.page_url }}
       steps:
         - uses: actions/checkout@v5
           with:
             fetch-depth: 0 # Full Git history, for the page dates
         - uses: actions/setup-node@v5
           with:
             node-version: 24
             cache: npm
         - run: npm ci
         - run: npm run build # `npm run docs:build` for a site in `docs/`
         - uses: actions/upload-pages-artifact@v4
           with:
             path: .output/public # `docs/.output/public` for a site in `docs/`
         - id: deployment
           uses: actions/deploy-pages@v4
   ```

3. In the repository settings, under **Pages**, set the source to **GitHub Actions**.

### Trailing Slashes [#trailing-slashes]

A static build writes the page `/guide` to `guide.html`. For a host that serves only the `index.html` of a folder, like nginx by default, end the URLs with a slash, so the build writes `guide/index.html`:

```ts title="vite.config.ts"
export default defineConfig({
  plugins: [vuesho({ trailingSlash: true })],
});
```

## Server Hosting [#server-hosting]

Without `static: true`, the build includes a [Nitro](https://nitro.build) server. Nitro detects Vercel, Netlify and Cloudflare when the site builds on them. Elsewhere, set the [preset](https://nitro.build/deploy) in `vite.config.ts`, like `nitro: { preset: "vercel" }`.

<Tabs :items='["Node.js","Docker","Vercel","Netlify","Cloudflare"]'>
<Tab value="Node.js">
The default preset, `node-server`, builds a server that runs from `.output`:

```bash
node .output/server/index.mjs
```

The server listens on `PORT`, 3000 by default, and `HOST`. Build on the same operating system and CPU architecture as the server, since native packages, like the renderer of [Open Graph images](/seo#open-graph-images), are installed for the machine that builds.
</Tab>

<Tab value="Docker">
```dockerfile title="Dockerfile"
FROM node:24-slim AS build
WORKDIR /app
COPY . .
RUN npm ci && npm run build

FROM node:24-slim
WORKDIR /app
COPY --from=build /app/.output .output
EXPOSE 3000
CMD ["node", ".output/server/index.mjs"]
```

```text title=".dockerignore"
node_modules
.output
```
</Tab>

<Tab value="Vercel">
Import the repository in [Vercel](https://vercel.com/new).
</Tab>

<Tab value="Netlify">
Import the repository in [Netlify](https://app.netlify.com/start).
</Tab>

<Tab value="Cloudflare">
The `cloudflare-module` preset builds a Worker. Install `unenv`, which Nitro needs for it:

```npm
npm i -D unenv@^2.0.0-rc.24
```

To build outside [Workers Builds](https://developers.cloudflare.com/workers/ci-cd/builds/), set the preset:

```ts title="vite.config.ts"
export default defineConfig({
  plugins: [vuesho()],
  nitro: { preset: "cloudflare-module" },
});
```

Nitro writes the Wrangler config, so `npx wrangler deploy` deploys the build. A Worker can't read a content folder while it runs, so a site with an [Obsidian](/content-sources/obsidian) vault or a [local folder](/content-sources/local-files) needs a static build on Cloudflare.
</Tab>
</Tabs>

### Error Monitoring [#error-monitoring]

When a page fails to render on the server, the server logs the error and responds with the theme's `ServerError` page and the status 500. To report the error to a monitoring service, add a [Nitro plugin](https://nitro.build/docs/plugins):

```ts tab="server/monitor.ts"
import { definePlugin } from "nitro";

export default definePlugin((nitroApp) => {
  nitroApp.hooks.hook("error", (error, { event }) => {
    // Report `error`, with `event?.req.url`
  });
});
```

```ts tab="vite.config.ts"
export default defineConfig({
  plugins: [vuesho()],
  nitro: { plugins: ["./server/monitor.ts"] }, // [!code highlight]
});
```

## Base Path [#base-path]

For a site under a path, like `https://acme.dev/docs/`, set `base` in `vite.config.ts`:

```ts title="vite.config.ts"
export default defineConfig({
  base: "/docs/", // [!code highlight]
  plugins: [vuesho()],
});
```

Write links, images, plugin routes and the head tags of the config without the base path, and Vuesho adds it. In a Vue component, use `Link` or `addBasePath()` from [`vuesho/client`](/client-api). `site.baseUrl` stays the origin, `https://acme.dev`.

## Redirects and Headers [#redirects-and-headers]

Nitro's [route rules](https://nitro.build/docs/routing#route-rules) in `vite.config.ts` redirect moved pages and set response headers:

```ts title="vite.config.ts"
export default defineConfig({
  plugins: [vuesho()],
  nitro: {
    routeRules: {
      "/old-guide": { redirect: { to: "/guide", status: 308 } },
      "/v1/**": { redirect: "/v2/**" },
      "/api/**": { headers: { "cache-control": "no-store" } },
    },
  },
});
```

A redirect responds with 307 unless it sets `status`. With a [base path](#base-path), write a rule's path without it, and the target of a redirect with it, like `/docs/guide`.

A static build keeps the rules that its host applies: Vercel applies all of them, Netlify and Cloudflare Pages all but `proxy`, and a Cloudflare Worker only `headers` and `cors`. Where the host applies no redirects, the build writes a redirect of an exact path as a page that redirects. The build warns about each rule it can't keep, which you then set in the host's config.

## Environment Variables [#environment-variables]

`vuesho.config.ts` and plugins read variables from `process.env`. `vuesho dev` and `vuesho build` load them from these files, as Vite does:

```
.env                # every mode
.env.local          # every mode
.env.[mode]         # only in that mode
.env.[mode].local   # only in that mode
```

A deployed server reads no `.env` file, so set the variables on the host. Pages rendered at build time keep the values of the build, while pages and routes rendered on request get the values of the server's host.

<Callout type="warn" title="Public Variables">
A variable that starts with `VITE_` reaches the browser through `import.meta.env`. Never give a
secret that prefix.
</Callout>

## Assets on a CDN [#assets-on-a-cdn]

To serve the hashed files of the build from another origin, use Vite's `renderBuiltUrl`:

```ts title="vite.config.ts"
export default defineConfig({
  plugins: [vuesho()],
  experimental: {
    renderBuiltUrl: (file, { type }) =>
      type === "asset" ? `https://cdn.acme.dev/${file}` : undefined,
  },
});
```

Upload `.output/public/assets/` and `.output/public/_vuesho/m/` to the same paths on the CDN, and allow CORS. On a site with pages rendered on request, return `undefined` for `.js` files, so the scripts stay on the site's origin.

## Content Security Policy [#content-security-policy]

Each page has a small inline script that applies the color mode before the page renders. A strict policy allows it by its hash, `colorModeScriptHash` from `vuesho/config`:

```ts title="vite.config.ts"
import { defineConfig } from "vite";
import { colorModeScriptHash } from "vuesho/config"; // [!code highlight]
import vuesho from "vuesho/vite";

export default defineConfig({
  plugins: [vuesho()],
  nitro: {
    routeRules: {
      "/**": {
        headers: { "content-security-policy": `script-src 'self' ${colorModeScriptHash}` },
      },
    },
  },
});
```

On another static host, set the header in the host's config, as [Redirects and Headers](#redirects-and-headers) describes. A page rendered on request can use a nonce instead: a plugin's [middleware](/routes#middleware) sets the policy with the nonce on the request it passes to `next()`, and on the response.
