Skip to content
Draft
Prev Previous commit
Next Next commit
move respond to the adapter dev option
  • Loading branch information
teemingc committed Nov 13, 2026
commit 2e96c7b8574e3ea226812bb9fc91165ae9ae6141
5 changes: 5 additions & 0 deletions .changeset/kit-adapter-dev-respond.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: add `@sveltejs/kit/adapter/dev` and the `dev.respond` adapter option, so that adapters can render requests in their platform's runtime during `vite dev`
5 changes: 0 additions & 5 deletions .changeset/kit-vite-respond.md

This file was deleted.

10 changes: 6 additions & 4 deletions documentation/docs/25-build-and-deploy/99-writing-adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ export default function (options) {
// Return `false if it can't, or throw a descriptive error.
}
},
vite: {
dev: {
respond(request, { getClientAddress, resolve }) {
// during `vite dev`, render the request in your platform's runtime,
// or call `resolve` to render it in Node as usual
return resolve(request);
},
}
},
vite: {
getRequest(options) {
const request = getRequest(options);
// modify the Request object here if needed
Expand All @@ -69,7 +71,7 @@ export default function (options) {
}
```

Of these, `name` and `adapt` are required. `emulate`, `vite`, and `supports` are optional.
Of these, `name` and `adapt` are required. `emulate`, `dev`, `vite`, and `supports` are optional.

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

Expand All @@ -90,4 +92,4 @@ Where possible, we recommend putting the adapter output under the `build/` direc
By default, `vite dev` renders requests in Node. If your platform's runtime can run in a [Vite environment](https://vite.dev/guide/api-environment), for example through a Vite plugin that your platform provides, you can render requests there instead, so that development behaves like production:

- In the code that runs in that environment, import `server` from `@sveltejs/kit/adapter/dev`. It has the same API as the `server` that `builder.generateServerInstance` writes for production: call `await server.init({ env })` and `server.respond(request, { getClientAddress })`
- Add a `respond` function to your adapter's `vite` config that sends each request to that code and returns its response. SvelteKit calls it after serving static files, imported assets and the service worker, and logs the response as usual
- Add a `dev.respond` function to your adapter that sends each request to that code and returns its response. SvelteKit calls it after serving static files, imported assets and the service worker, and logs the response as usual
6 changes: 6 additions & 0 deletions packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ export function validate_config(config) {
}
}

if (typeof config.adapter?.dev === 'function') {
validated.adapter.dev = config.adapter.dev({
config: validated
});
}

if (typeof config.adapter?.vite === 'function') {
validated.adapter.vite = config.adapter.vite({
config: validated
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/exports/adapter/dev/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Server } from '@sveltejs/kit';
* SvelteKit's server during `vite dev`, for code that renders requests in a Vite environment
* outside the Vite process, such as an adapter's Worker in workerd. It has the same API as the
* [server](https://svelte.dev/docs/kit/@sveltejs-kit#Server) that `builder.generateServerInstance`
* writes for production. See [`respond`](https://svelte.dev/docs/kit/@sveltejs-kit#AdapterViteConfig).
* writes for production. See [`respond`](https://svelte.dev/docs/kit/@sveltejs-kit#AdapterDevConfig).
*
* ```js
* import { server } from '@sveltejs/kit/adapter/dev';
Expand Down
10 changes: 9 additions & 1 deletion packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ export interface Adapter {
* during dev, build and prerendering.
*/
emulate?: () => MaybePromise<Emulator>;
/**
* Options that only apply during `vite dev`
* @since 3.3.0
*/
dev?: AdapterDevConfig | ((ctx: { config: ValidatedConfig }) => AdapterDevConfig);
/**
* Options for configuring and interacting with Vite
* @since 3.0.0
*/
vite?: AdapterViteConfig | ((ctx: { config: ValidatedConfig }) => AdapterViteConfig);
}

export interface AdapterViteConfig {
export interface AdapterDevConfig {
/**
* During `vite dev`, SvelteKit calls this function with each request it would render, after
* serving static files, imported assets and the service worker. Use it to render requests in
Expand All @@ -82,6 +87,9 @@ export interface AdapterViteConfig {
resolve: (request: Request) => Promise<Response>;
}
) => Promise<Response>;
}

export interface AdapterViteConfig {
/**
* This function overrides the default behavior during Vite's dev and preview modes
* to convert an `http.IncomingMessage` to a `Request` object.
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ export async function dev(
throw new Error('Could not determine clientAddress');
};

const adapter_respond = svelte_config.adapter?.vite?.respond;
const adapter_respond = svelte_config.adapter?.dev?.respond;

/**
* renders the request in this process
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Emulator,
HttpError,
Adapter,
AdapterDevConfig,
AdapterViteConfig
} from '@sveltejs/kit';
import { RemoteFormIssue, RemoteQuery, RemoteLiveQuery } from '$app/server';
Expand Down Expand Up @@ -603,7 +604,7 @@ export interface Uses {
export type ValidatedConfig = RecursiveRequired<
Omit<Config, 'preprocess' | 'adapter' | 'typescript'>
> & {
adapter: Adapter & { vite?: AdapterViteConfig };
adapter: Adapter & { dev?: AdapterDevConfig; vite?: AdapterViteConfig };
preprocess: Config['preprocess'];
typescript: { config: NonNullable<Config['typescript']>['config'] };
};
Expand Down
14 changes: 11 additions & 3 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ declare module '@sveltejs/kit' {
* during dev, build and prerendering.
*/
emulate?: () => MaybePromise<Emulator>;
/**
* Options that only apply during `vite dev`
* @since 3.3.0
*/
dev?: AdapterDevConfig | ((ctx: { config: ValidatedConfig }) => AdapterDevConfig);
/**
* Options for configuring and interacting with Vite
* @since 3.0.0
*/
vite?: AdapterViteConfig | ((ctx: { config: ValidatedConfig }) => AdapterViteConfig);
}

export interface AdapterViteConfig {
export interface AdapterDevConfig {
/**
* During `vite dev`, SvelteKit calls this function with each request it would render, after
* serving static files, imported assets and the service worker. Use it to render requests in
Expand All @@ -69,6 +74,9 @@ declare module '@sveltejs/kit' {
resolve: (request: Request) => Promise<Response>;
}
) => Promise<Response>;
}

export interface AdapterViteConfig {
/**
* This function overrides the default behavior during Vite's dev and preview modes
* to convert an `http.IncomingMessage` to a `Request` object.
Expand Down Expand Up @@ -995,7 +1003,7 @@ declare module '@sveltejs/kit' {
type ValidatedConfig = RecursiveRequired<
Omit<Config, 'preprocess' | 'adapter' | 'typescript'>
> & {
adapter: Adapter & { vite?: AdapterViteConfig };
adapter: Adapter & { dev?: AdapterDevConfig; vite?: AdapterViteConfig };
preprocess: Config['preprocess'];
typescript: { config: NonNullable<Config['typescript']>['config'] };
};
Expand Down Expand Up @@ -1181,7 +1189,7 @@ declare module '@sveltejs/kit/adapter/dev' {
* SvelteKit's server during `vite dev`, for code that renders requests in a Vite environment
* outside the Vite process, such as an adapter's Worker in workerd. It has the same API as the
* [server](https://svelte.dev/docs/kit/@sveltejs-kit#Server) that `builder.generateServerInstance`
* writes for production. See [`respond`](https://svelte.dev/docs/kit/@sveltejs-kit#AdapterViteConfig).
* writes for production. See [`respond`](https://svelte.dev/docs/kit/@sveltejs-kit#AdapterDevConfig).
*
* ```js
* import { server } from '@sveltejs/kit/adapter/dev';
Expand Down
Loading