A high-performance SvelteKit adapter that leverages Bun's native APIs for optimal speed and efficiency.
@sveltejs/adapter-node
)bun add -D svelte-adapter-bun
In your svelte.config.js
:
import adapter from 'svelte-adapter-bun';
export default {
kit: {
adapter: adapter({
// Options
})
}
};
Option | Type | Default | Description |
---|---|---|---|
out |
string |
'build' |
The directory to write the built files to |
precompress |
boolean | CompressOptions |
false |
Enable precompression of assets |
envPrefix |
string |
'' |
Prefix for environment variables |
development |
boolean |
false |
Enable development mode (disables minification) |
dynamic_origin |
boolean |
false |
Enable dynamic origin support |
xff_depth |
number |
1 |
X-Forwarded-For depth for trusted proxies |
assets |
boolean |
true |
Serve static assets |
adapter({
precompress: {
brotli: true, // Enable Brotli compression
gzip: true, // Enable Gzip compression
files: ['html', 'js', 'css', 'svg', 'xml'] // File extensions to compress
}
})
This adapter includes native WebSocket support using Bun's WebSocket API. To use WebSockets in your SvelteKit app:
src/hooks.server.ts
file:import type { Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ event, resolve }) => {
return resolve(event);
};
// Export WebSocket handlers
export const handleWebsocket = {
open(ws) {
console.log('WebSocket opened');
ws.send('Welcome!');
},
message(ws, message) {
console.log('Received:', message);
ws.send(`Echo: ${message}`);
},
close(ws, code, reason) {
console.log('WebSocket closed:', code, reason);
},
// Optional: Control upgrade behavior
upgrade(request, server) {
const url = new URL(request.url);
if (url.pathname === '/ws') {
return true; // Upgrade to WebSocket
}
return false;
}
};
const ws = new WebSocket('ws://localhost:3000/ws');
ws.onmessage = (event) => console.log(event.data);
ws.send('Hello, server!');
See src/hooks.example.ts
for a complete WebSocket implementation example.
The adapter supports environment variables with optional prefixing:
adapter({
envPrefix: 'PUBLIC_'
})
This will make PUBLIC_API_URL
available as API_URL
in your app.
After building your app:
bun run build
The adapter generates a minimal package.json
in the build directory. To run the server:
cd build
bun install --production
bun ./index.js
Or simply:
bun ./build/index.js
The build output is optimized for production:
FROM oven/bun:1-alpine
WORKDIR /app
# Copy build output
COPY build build/
COPY package.json .
# Install only production dependencies
RUN cd build && bun install --production
EXPOSE 3000
CMD ["bun", "./build/index.js"]
This adapter leverages Bun's native features for optimal performance:
Bun.file()
Bun.gzipSync()
Bun.serve()
Bun.build()
While maintaining API compatibility with @sveltejs/adapter-node
, this adapter:
bun add -D svelte-adapter-bun
svelte.config.js
to use the new adapternpm/yarn
commands with bun
equivalentsMIT