Skip to content

feat: add Bun adapter - #16695

Open
Black-Hack wants to merge 96 commits into
sveltejs:version-3from
Black-Hack:feat/bun-adapter
Open

feat: add Bun adapter#16695
Black-Hack wants to merge 96 commits into
sveltejs:version-3from
Black-Hack:feat/bun-adapter

Conversation

@Black-Hack

Copy link
Copy Markdown

closes #16070

Summary

This PR adds an official @sveltejs/adapter-bun package that builds SvelteKit applications into standalone Bun-native servers using Bun.serve, Web API requests/responses, native routes, and Bun.file.

What changed

  • Adds SSR, hydration, static assets, prerendered pages/endpoints, redirects, base paths, immutable caching, and non-GET fallthrough to SvelteKit.
  • Supports $app/server reads, instrumentation.server.js, server-sent events, graceful shutdown, request-origin normalization, and trusted proxy headers.
  • Exposes the original Request and Bun Server through event.platform, including Bun metrics and requestIP.
  • Supports regular JavaScript builds and self-contained executables with asset embedding, cross-compilation, minification, bytecode, and source maps.

Key differences from adapter-node

Area adapter-bun adapter-node
Runtime Native Bun.serve and Web APIs Node HTTP, Polka, and request conversion
Static files Native Bun routes and file responses sirv with Brotli/gzip precompression
Build output JavaScript server or compiled executable JavaScript server only
Customization Bun server context through event.platform Reusable handler.js and Node request context
Lifecycle Bun timeouts and second-signal forced exit Systemd activation and Node-specific timeout controls

Application imports follow Bun's bundler behavior rather than adapter-node's dependencies/devDependencies externalization policy.

Decisions and tradeoffs

  • Native routes preserve Bun's optimized file serving, conditional requests, ranges, streaming, and automatic HEAD behavior.
  • Filesystem assets remain unbuffered in regular builds; compiled builds embed them into the executable.
  • The generated server owns fetch and routes to preserve SvelteKit behavior.
  • SvelteKit receives a normalized public URL while event.platform.request retains the original Bun request required by native APIs.
  • Node-specific behavior is not emulated where Bun provides a different native model.

Known limitations and future work

  • Bun treats * as a route wildcard, so filenames containing a literal * are currently rejected at build time.
  • WebSockets, TLS, custom error handlers, HTTP/3, and explicit HTTP/1 configuration require a custom Bun integration.
  • This PR does not provide a reusable custom-server handler or built-in gzip/Brotli precompression.
  • Systemd socket activation and Node-specific shutdown, keep-alive, and header timeouts are out of scope.
  • Follow-ups may expand buildOptions, dependency externalization controls, MIME handling, and literal-wildcard support.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

… configuration

- Add env.js for managing environment variables with validation and fallback options.
- Introduce handler.js to manage the Bun-native SvelteKit request handling.
- Create index.js to configure server options based on environment variables.
- Implement static.js for serving static files and handling prerendered paths.
- Add utils.js for utility functions related to byte parsing and header management.
- Create tests for environment variable functions and utility functions.
- Set up a basic SvelteKit application with routes, static files, and event streams.
- Configure Playwright for end-to-end testing of the application.
…asset handling

- Added TLS configuration options in serverOptions, allowing for certificate and key specification.
- Introduced embedded asset handling for serving static files with metadata (size, type, lastModified, etag).
- Updated file_route to support embedded assets and conditional requests.
- Enhanced compile options to reserve runtime target and module format, ensuring user configurations are safely composed.
- Added tests for TLS options and embedded asset functionality to ensure robustness.
- Improved documentation to clarify usage of new features and options.
Comment thread packages/adapter-bun/index.js Outdated
Comment thread packages/adapter-bun/index.js Outdated
Comment thread packages/adapter-bun/index.js Outdated
Comment thread packages/adapter-bun/index.js Outdated
Comment thread packages/adapter-bun/index.js Outdated
Comment thread packages/adapter-bun/index.js Outdated

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few small nits but this is a great addition! thank you

Namespace imports for node:path and node:fs, sync fs calls in the
build-time code with an existsSync early return instead of catching
ENOENT, fs.rmSync in place of the deprecated builder.rimraf, and
snake_case with url/filename naming for internal variables.
Matches kit's convention of one @import comment per module instead of
inline import() types at every use site.
@Nic-Polumeyv

Copy link
Copy Markdown
Contributor

@Black-Hack apologies for pushing straight to your branch, it felt like the fastest way to spare us weeks of back and forth. Please go through the commits when you get a chance. If anything I changed doesn't sit right, do not hesitate to communicate them in this thread.

@Nic-Polumeyv

This comment was marked as resolved.

@Nic-Polumeyv
Nic-Polumeyv marked this pull request as draft August 11, 2026 17:57
Comment thread packages/adapter-bun/src/routes-util.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Bun adapter

4 participants