Skip to main content

Configuration

Edit this page on GitHub

Your project's configuration lives in a svelte.config.js file at the root of your project. As well as SvelteKit, this config object is used by other tooling that integrates with Svelte such as editor extensions.

svelte.config.js
ts
import adapter from '@sveltejs/adapter-auto';
 
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter()
}
};
 
export default config;
ts
interface Config {}
ts
compilerOptions?: CompileOptions;
  • default {}
ts
extensions?: string[];
  • default [".svelte"]
ts
kit?: KitConfig;
ts
package?: {
source?: string;
dir?: string;
emitTypes?: boolean;
exports?(filepath: string): boolean;
files?(filepath: string): boolean;
};
ts
preprocess?: any;
ts
[key: string]: any;

The kit property configures SvelteKit, and can have the following properties:

adapterpermalink

  • default undefined

Your adapter is run when executing vite build. It determines how the output is converted for different platforms.

aliaspermalink

  • default {}

An object containing zero or more aliases used to replace values in import statements. These aliases are automatically passed to Vite and TypeScript.

svelte.config.js
ts
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
alias: {
// this will match a file
'my-file': 'path/to/my-file.js',
 
// this will match a directory and its contents
// (`my-directory/x` resolves to `path/to/my-directory/x`)
'my-directory': 'path/to/my-directory',
 
// an alias ending /* will only match
// the contents of a directory, not the directory itself
'my-directory/*': 'path/to/my-directory/*'
}
}
};

The built-in $lib alias is controlled by config.kit.files.lib as it is used for packaging.

You will need to run npm run dev to have SvelteKit automatically generate the required alias configuration in jsconfig.json or tsconfig.json.

appDirpermalink

  • default "_app"

The directory relative to paths.assets where the built JS and CSS (and imported assets) are served from. (The filenames therein contain content-based hashes, meaning they can be cached indefinitely). Must not start or end with /.

csppermalink

Content Security Policy configuration. CSP helps to protect your users against cross-site scripting (XSS) attacks, by limiting the places resources can be loaded from. For example, a configuration like this...

svelte.config.js
ts
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
csp: {
directives: {
'script-src': ['self']
},
reportOnly: {
'script-src': ['self']
}
}
}
};
 
export default config;

...would prevent scripts loading from external sites. SvelteKit will augment the specified directives with nonces or hashes (depending on mode) for any inline styles and scripts it generates.

To add a nonce for scripts and links manually included in src/app.html, you may use the placeholder %sveltekit.nonce% (for example <script nonce="%sveltekit.nonce%">).

When pages are prerendered, the CSP header is added via a <meta http-equiv> tag (note that in this case, frame-ancestors, report-uri and sandbox directives will be ignored).

When mode is 'auto', SvelteKit will use nonces for dynamically rendered pages and hashes for prerendered pages. Using nonces with prerendered pages is insecure and therefore forbidden.

Note that most Svelte transitions work by creating an inline <style> element. If you use these in your app, you must either leave the style-src directive unspecified or add unsafe-inline.

ts
mode?: 'hash' | 'nonce' | 'auto';
ts
directives?: CspDirectives;
ts
reportOnly?: CspDirectives;

csrfpermalink

Protection against cross-site request forgery attacks.

ts
checkOrigin?: boolean;
  • default true

envpermalink

Environment variable configuration

ts
dir?: string;
  • default "."
ts
publicPrefix?: string;
  • default "PUBLIC_"

filespermalink

Where to find various files within your project.

ts
assets?: string;
  • default "static"
ts
hooks?: {}
ts
client?: string;
  • default "src/hooks.client"
ts
server?: string;
  • default "src/hooks.server"
ts
lib?: string;
  • default "src/lib"
ts
params?: string;
  • default "src/params"
ts
routes?: string;
  • default "src/routes"
ts
serviceWorker?: string;
  • default "src/service-worker"
ts
appTemplate?: string;
  • default "src/app.html"
ts
errorTemplate?: string;
  • default "src/error.html"

inlineStyleThresholdpermalink

  • default 0

Inline CSS inside a <style> block at the head of the HTML. This option is a number that specifies the maximum length of a CSS file to be inlined. All CSS files needed for the page and smaller than this value are merged and inlined in a <style> block.

This results in fewer initial requests and can improve your First Contentful Paint score. However, it generates larger HTML output and reduces the effectiveness of browser caches. Use it advisedly.

moduleExtensionspermalink

  • default [".js", ".ts"]

An array of file extensions that SvelteKit will treat as modules. Files with extensions that match neither config.extensions nor config.kit.moduleExtensions will be ignored by the router.

outDirpermalink

  • default ".svelte-kit"

The directory that SvelteKit writes files to during dev and build. You should exclude this directory from version control.

pathspermalink

ts
assets?: string;
  • default ""
ts
base?: string;
  • default ""

prerenderpermalink

See Prerendering.

ts
concurrency?: number;
  • default 1
ts
crawl?: boolean;
  • default true
ts
entries?: Array<'*' | `/${string}`>;
  • default ["*"]
ts
  • default "fail"
ts
  • default "fail"
ts
origin?: string;
  • default "http://sveltekit-prerender"

serviceWorkerpermalink

ts
register?: boolean;
  • default true
ts
files?(filepath: string): boolean;
  • default (filename) => !/\.DS_Store/.test(filename)

versionpermalink

Client-side navigation can be buggy if you deploy a new version of your app while people are using it. If the code for the new page is already loaded, it may have stale content; if it isn't, the app's route manifest may point to a JavaScript file that no longer exists. SvelteKit solves this problem by falling back to traditional full-page navigation if it detects that a new version has been deployed, using the name specified here (which defaults to a timestamp of the build).

If you set pollInterval to a non-zero value, SvelteKit will poll for new versions in the background and set the value of the updated store to true when it detects one.

ts
name?: string;
  • default Date.now().toString()
ts
pollInterval?: number;
  • default 0
We stand with Ukraine. Donate → We stand with Ukraine. Petition your leaders. Show your support.