Skip to main content

Writing adapters

If an adapter for your preferred environment doesn't yet exist, you can build your own. We recommend looking at the source for an adapter to a platform similar to yours and copying it as a starting point.

Adapter packages implement the following API, which creates an Adapter:

/** @param {AdapterSpecificOptions} options */
export default function (options: any
@param
options
options
) {
/** @type {import('@sveltejs/kit').Adapter} */ const const adapter: Adapteradapter = { Adapter.name: string

The name of the adapter, using for logging. Will typically correspond to the package name.

name
: 'adapter-package-name',
async Adapter.adapt: (builder: Builder) => MaybePromise<void>

This function is called after SvelteKit has built your app.

@param
builder An object provided by SvelteKit that contains methods for adapting the app
adapt
(builder: Builderbuilder) {
// adapter implementation },
Adapter.supports?: {
    read?: (details: {
        config: any;
        route: {
            id: string;
        };
    }) => boolean;
    instrumentation?: () => boolean;
} | undefined

Checks called during dev and build to determine whether specific features will work in production with this adapter.

supports
: {
read: ({ config: anyconfig,
route: {
    id: string;
}
route
}) => {
// Return `true` if the route with the given `config` can use `read` // from `$app/server` in production, return `false` if it can't. // Or throw a descriptive error describing how to configure the deployment }, instrumentation: () => { // Return `true` if this adapter supports loading `instrumentation.server.js`. // Return `false if it can't, or throw a descriptive error. } },
Adapter.vite?: {
    plugins?: PluginOption;
} | undefined
vite
: {
plugins?: PluginOption

Add a Vite plugin here to replace the default Node SSR environment. The provided Vite plugins should configure the dev and preview servers

@since
3.0.0
plugins
: [
// add plugins here to integrate with Vite ] } }; return const adapter: Adapter
adapter
;
}

Of these, name and adapt are required. vite.plugins and supports are optional.

Within the adapt method, there are a number of things that an adapter should do:

  • Clear out the build directory
  • Write SvelteKit output with builder.writeClient, builder.writeServer, and builder.writePrerendered
  • Output code that:
    • Imports Server from ${builder.getServerDirectory()}/index.js
    • Instantiates the app with a manifest generated with builder.generateManifest({ relativePath })
    • Listens for requests from the platform, converts them to a standard Request if necessary, calls the server.respond(request, { getClientAddress }) function to generate a Response and responds with it
    • expose any platform-specific information to SvelteKit via the platform option passed to server.respond
  • Bundle the output to avoid needing to install dependencies on the target platform, if necessary
  • Put the user's static files and the generated JS/CSS in the correct location for the target platform

Where possible, we recommend putting the adapter output under the build/ directory with any intermediate output placed under .svelte-kit/[adapter-name].

Configuring the development and preview experience

By default, SvelteKit runs your server code through a Node.js runtime when running vite dev and vite preview. You can change this behaviour by adding a Vite plugin that has a configureServer and configurePreviewServer hook to route requests to a different runtime.

The main Vite server environment SvelteKit uses is named ssr. You can change its settings by referencing it in the config hook of a Vite plugin.

config(userConfig) {


	userConfig.environments.ssr = { ... }

}

You can also create your own server entry file by importing the Server class from sveltekit:server, the environment variables loaded by Vite through env from sveltekit:env, and your app-specific information as manifest from sveltekit:server-manifest.

import { const env: Record<string, string>env } from 'sveltekit:env';
import { class ServerServer } from 'sveltekit:server';
import { const manifest: SSRManifestmanifest } from 'sveltekit:server-manifest';

const const server: Serverserver = new new Server(manifest: SSRManifest): ServerServer(const manifest: SSRManifestmanifest);

await const server: Serverserver.Server.init(options: ServerInitOptions): Promise<void>init({ ServerInitOptions.env: Record<string, string>

A map of environment variables.

env
});
export default { /** * @param {Request} request * @returns {Promise<Response>} */ async function fetch(request: any): Promise<Response>
@param
request
@returns
fetch(request: any
@param
request
request
) {
return await const server: Serverserver.Server.respond(request: Request, options: RequestOptions): Promise<Response>respond(request: any
@param
request
request
, {
RequestOptions.getClientAddress(): stringgetClientAddress: () => { return request: any
@param
request
request
.headers.get('how-your-platform-exposes-the-remote-address')
} }); } } import.meta.ImportMeta.hot?: ViteHotContext | undefinedhot?.ViteHotContext.accept(): void (+3 overloads)accept();

Edit this page on GitHub llms.txt

previous next