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:
import { function getRequest({ request, base, bodySizeLimit }: {
request: import("http").IncomingMessage;
base: string;
bodySizeLimit?: number;
}): Request
getRequest, function setResponse(res: import("http").ServerResponse, response: Response): voidsetResponse } from '@sveltejs/kit/node';
/** @param {AdapterSpecificOptions} options */
export default function (options: anyoptions) {
/** @type {import('@sveltejs/kit').Adapter} */
const const adapter: Adapteradapter = {
Adapter.name: stringThe 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.
adapt(builder: Builderbuilder) {
// adapter implementation
},
async Adapter.emulate?: (() => MaybePromise<Emulator>) | undefinedCreates an Emulator, which allows the adapter to influence the environment
during dev, build and prerendering.
emulate() {
return {
async Emulator.platform?(details: {
config: any;
prerender: PrerenderOption;
}): MaybePromise<App.Platform>
A function that is called with the current route config and prerender option
and returns an App.Platform object
platform({ config: anyconfig, prerender: PrerenderOptionprerender }) {
// the returned object becomes `event.platform` during dev, build and
// preview. Its shape is that of `App.Platform`
}
}
},
Adapter.supports?: {
read?: (details: {
config: Record<string, 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: Record<string, any>config, 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.getRequest?: (({ request, base, bodySizeLimit }: {
request: import("http").IncomingMessage;
base: string;
bodySizeLimit?: number;
}) => Request) | undefined
This function overrides the default behavior to convert an http.IncomingMessage to a Request object.
To call the original setRequest function, import it from @sveltejs/kit/node.
getRequest(options: {
request: import("http").IncomingMessage;
base: string;
bodySizeLimit?: number;
}
options) {
const const request: Requestrequest = function getRequest({ request, base, bodySizeLimit }: {
request: import("http").IncomingMessage;
base: string;
bodySizeLimit?: number;
}): Request
getRequest(options: {
request: import("http").IncomingMessage;
base: string;
bodySizeLimit?: number;
}
options);
// modify the Request object here if needed
return const request: Requestrequest;
},
Adapter.setResponse?: ((res: import("http").ServerResponse, response: Response) => void) | undefinedThis function overrides the default behavior to write a Response object to an http.ServerResponse.
To call the original setResponse function, import it from @sveltejs/kit/node.
setResponse(res: ServerResponse<IncomingMessage>res, response: Responseresponse) {
// handle WebSockets here, for example
function setResponse(res: import("http").ServerResponse, response: Response): voidsetResponse(res: ServerResponse<IncomingMessage>res, response: Responseresponse);
},
Adapter.vite?: {
plugins?: {
pre?: Plugin[];
post?: Plugin[];
};
} | undefined
vite: {
plugins?: {
pre?: Plugin[];
post?: Plugin[];
} | undefined
plugins: {
// add plugins here to integrate with Vite
pre?: Plugin<any>[] | undefinedVite plugins placed before any of SvelteKit's own plugins.
pre: [],
post?: Plugin<any>[] | undefinedVite plugins placed after any of SvelteKit's own plugins.
post: []
}
}
};
return const adapter: Adapteradapter;
}Of these, name and adapt are required. emulate, vite, getRequest, setResponse, 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, andbuilder.writePrerendered - Output code that:
- Imports
Serverfrom${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
Requestif necessary, calls theserver.respond(request, { getClientAddress })function to generate aResponseand responds with it - expose any platform-specific information to SvelteKit via the
platformoption passed toserver.respond
- Imports
- 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].
Edit this page on GitHub llms.txt