svelte-adapter-fastify
v0.0.15
Published
SvelteKit Adapter for Fastify.
Downloads
39
Maintainers
Readme
svelte-adapter-fastify
svelte-adapter-fastify
is a SvelteKit plugin for the
Fastify framework.
| :warning: WARNING: This project is considered to be in BETA
until SvelteKit is available for general use and the Adapter API is stable! |
| ----------------------------------------------------------------------------------------------------------------------------------------- |
Beta Adapter Version Compatibility
| Adapter Version | SvelteKit Version |
| --------------- | ----------------- |
| 0.0.14
| 1.0.0-next.377
|
| 0.0.13
| 1.0.0-next.377
|
| 0.0.12
| 1.0.0-next.377
|
| 0.0.11
| 1.0.0-next.377
|
| 0.0.10
| 1.0.0-next.377
|
Note: only the versions listed have been tested together, if others happen to work, it is just coincidence. This is beta software after all.
Supported Fastify versions
- Fastify v4.x
Supported Node.js versions
The latest versions of the following Node.js versions are tested and supported.
- 16
- 18
Quick Start
Install the package using npm
:
npm i --save-exact svelte-adapter-fastify
or yarn
:
yarn add svelte-adapter-fastify
or pnpm
:
pnpm add --save-exact svelte-adapter-fastify
Setup
Replace the default @sveltejs/adapter-auto
with svelte-adapter-fastify
in the svelte.config.js
file:
import preprocess from 'svelte-preprocess';
- import adapter from '@sveltejs/adapter-auto';
+ import fastifyAdapter from "svelte-adapter-fastify";
/** @type {import('@sveltejs/kit').Config} */
const config = {
preprocess: preprocess(),
kit: {
- adapter: adapter(),
+ adapter: fastifyAdapter(),
},
methodOverride: {
allowed: ['PATCH', 'DELETE'],
}
};
export default config;
Then:
npm run build
Which will generate the Fastify server ./build/server/index.js
which can be run:
PORT=3000 node ./build/server/index.js
Custom Fastify Server
To run a customized server, start by copying the default server from the svelte-adapter-fastify
module:
mkdir -p adapter/fastify
cp node_modules/svelte-adapter-fastify/files/index.js adapter/fastify
cp node_modules/svelte-adapter-fastify/files/server.js adapter/fastify
Edit the adapter/fastify/index.js
and adapter/fastify/index.js
files to meet your needs. You can add routes
and
other plugins
to the custom Fastify server.
By default the server will listen on localhost
, if you are deploying on GCP or in a Dockerfile then you would need to
set the HOST
environment variable to 0.0.0.0
.
HOST=0.0.0.0 PORT=3000 node ./build/server/index.js
A very important note: Any changes to the adapter/fastify/index.js
or adapter/fastify/server.js
file will only
reflect after a new build.
At build time refer to this custom server. When configuring the adapter in svelte.config.js
, add both serverFile
and
startFile
parameters:
import preprocess from 'svelte-preprocess';
import fastifyAdapter from 'svelte-adapter-fastify';
+ import path from 'node:path';
+ const __dirname = path.resolve();
const config = {
preprocess: preprocess(),
kit: {
adapter: fastifyAdapter({
+ serverFile: path.join(__dirname, './adapter/fastify/server.js'),
+ startFile: path.join(__dirname, './adapter/fastify/index.js'),
}),
},
methodOverride: {
allowed: ['PATCH', 'DELETE'],
}
};
export default config;
Build / Run as normal
npm run build
PORT=3000 node ./build/server/index.js