> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/sveltejs/kit/llms.txt
> Use this file to discover all available pages before exploring further.

# Building your app

> Learn how SvelteKit builds your application for production deployment

Building a SvelteKit app happens in two stages, which both happen when you run `vite build` (usually via `npm run build`).

## Build stages

Firstly, Vite creates an optimized production build of your server code, your browser code, and your service worker (if you have one). Prerendering (routes marked with `export const prerender = true`) is executed at this stage, if appropriate.

Secondly, an *adapter* takes this production build and tunes it for your target environment. See the [adapters overview](/build-and-deploy/adapters) for more information.

## During the build

SvelteKit will load your `+page/layout(.server).js` files (and all files they import) for analysis during the build. Any code that should *not* be executed at this stage must check that `building` from `$app/environment` is `false`:

```javascript theme={null}
import { building } from '$app/environment';
import { initialiseDatabase } from '$lib/server/database';

if (!building) {
  initialiseDatabase();
}

export function load() {
  // ...
}
```

<Note>
  The `building` flag is only `true` during the build process. It's `false` during development and when your app is running in production.
</Note>

## Preview your app

After building, you can view your production build locally with `vite preview` (via `npm run preview`).

<Warning>
  This will run the app in Node, and so is not a perfect reproduction of your deployed app. Adapter-specific adjustments like the `platform` object do not apply to previews.
</Warning>

## Build output

The build process creates several directories and files:

<Steps>
  <Step title="Client assets">
    Optimized JavaScript, CSS, and other static assets for the browser
  </Step>

  <Step title="Server bundle">
    Server-side rendering code and API routes
  </Step>

  <Step title="Prerendered pages">
    Static HTML files for routes marked with `export const prerender = true`
  </Step>

  <Step title="Adapter output">
    Platform-specific files generated by your chosen adapter
  </Step>
</Steps>

## Build configuration

You can customize the build process through your `svelte.config.js` file:

```javascript theme={null}
/// file: svelte.config.js
import adapter from '@sveltejs/adapter-auto';

/** @type {import('@sveltejs/kit').Config} */
const config = {
  kit: {
    adapter: adapter(),
    prerender: {
      entries: ['*']
    },
    paths: {
      base: '/my-app'
    }
  }
};

export default config;
```

<Tip>
  Check your adapter's documentation for platform-specific build options and requirements.
</Tip>
