Adapter for building SvelteKit applications on Cloudflare Pages with Workers integration
—
Configuration options and main adapter factory function for customizing the build process and deployment behavior on Cloudflare infrastructure.
Creates a SvelteKit adapter configured for Cloudflare deployment with customizable options.
import { Adapter } from '@sveltejs/kit';
import { GetPlatformProxyOptions } from 'wrangler';
/**
* Creates a SvelteKit adapter for Cloudflare deployment
* @param options - Configuration options for the adapter
* @returns SvelteKit Adapter instance
*/
function adapter(options?: AdapterOptions): Adapter;Usage Examples:
// Basic usage with default settings
import adapter from "@sveltejs/adapter-cloudflare";
export default {
kit: {
adapter: adapter()
}
};
// With custom configuration
export default {
kit: {
adapter: adapter({
config: './custom-wrangler.toml',
fallback: 'spa',
routes: {
include: ['/*'],
exclude: ['<all>']
}
})
}
};Interface defining all available configuration options for the adapter.
interface AdapterOptions {
/**
* Path to your Wrangler configuration file
* @see https://developers.cloudflare.com/workers/wrangler/configuration/
*/
config?: string;
/**
* Whether to render a plaintext 404.html page or a rendered SPA fallback page
* for non-matching asset requests.
*
* For Cloudflare Workers, the default behaviour is to return a null-body
* 404-status response for non-matching assets requests. However, if the
* assets.not_found_handling Wrangler configuration setting is set to "404-page",
* this page will be served if a request fails to match an asset. If
* assets.not_found_handling is set to "single-page-application", the adapter
* will render a SPA fallback index.html page regardless of the fallback option specified.
*
* For Cloudflare Pages, this page will only be served when a request that
* matches an entry in routes.exclude fails to match an asset.
*
* Most of the time 'plaintext' is sufficient, but if you are using routes.exclude
* to manually exclude a set of prerendered pages without exceeding the 100 route
* limit, you may wish to use 'spa' instead to avoid showing an unstyled 404 page
* to users.
*
* @see https://developers.cloudflare.com/pages/configuration/serving-pages/#not-found-behavior
* @default 'plaintext'
*/
fallback?: 'plaintext' | 'spa';
/**
* Only for Cloudflare Pages. Customize the automatically-generated _routes.json file.
* @see https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file
*/
routes?: {
/**
* Routes that will be invoked by functions. Accepts wildcards.
* @default ["/*"]
*/
include?: string[];
/**
* Routes that will not be invoked by functions. Accepts wildcards.
* exclude takes priority over include.
*
* To have the adapter automatically exclude certain things, you can use these placeholders:
* - <build> to exclude build artifacts (files generated by Vite)
* - <files> for the contents of your static directory
* - <prerendered> for prerendered routes
* - <all> to exclude all of the above
*
* @default ["<all>"]
*/
exclude?: string[];
};
/**
* Config object passed to getPlatformProxy during development and preview.
* @see https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy
*/
platformProxy?: GetPlatformProxyOptions;
}Detailed configuration for Cloudflare Pages routing behavior.
Route Limitations:
Cloudflare Pages Functions have specific limits for _routes.json:
includeinclude and excludeexclude rules will be dropped (causing unnecessary function invocations)include and exclude must be arrays_routes.json files are not allowed - the adapter will throw an error if one exists in your project rootImportant: If you have an existing _routes.json file, you must remove it and configure routing through the adapter options instead.
Route Placeholders:
<build> - Excludes build artifacts (files generated by Vite)<files> - Excludes contents of your static directory<prerendered> - Excludes prerendered routes<all> - Excludes all of the aboveUsage Examples:
// Custom routing configuration
adapter({
routes: {
include: ['/*'],
exclude: [
'<build>',
'<files>',
'/api/static/*',
'/public/*'
]
}
});
// Minimal function routing
adapter({
routes: {
include: ['/api/*', '/auth/*'],
exclude: ['<all>']
}
});Controls how 404 pages are rendered for non-matching requests.
Plaintext Mode ('plaintext'):
SPA Mode ('spa'):
Deployment-Specific Behavior:
Cloudflare Workers: The default behavior is to return a null-body 404-status response for non-matching assets requests. However, if the assets.not_found_handling Wrangler configuration setting is set to "404-page", this page will be served if a request fails to match an asset. If assets.not_found_handling is set to "single-page-application", the adapter will render a SPA fallback index.html page regardless of the fallback option specified.
Cloudflare Pages: This page will only be served when a request that matches an entry in routes.exclude fails to match an asset.
Usage Considerations:
Most of the time plaintext is sufficient, but if you are using routes.exclude to manually exclude a set of prerendered pages without exceeding the 100 route limit, you may wish to use spa instead to avoid showing an unstyled 404 page to users.
// For SPA applications
adapter({
fallback: 'spa'
});
// For traditional server-rendered apps
adapter({
fallback: 'plaintext'
});Install with Tessl CLI
npx tessl i tessl/npm-sveltejs--adapter-cloudflare