0.0.15 • Published 2 years ago

svelte-adapter-fastify v0.0.15

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

svelte-adapter-fastify

License: MIT code style: prettier npm version codecov Main WorkFlow CodeQL WorkFlow

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 VersionSvelteKit Version
0.0.141.0.0-next.377
0.0.131.0.0-next.377
0.0.121.0.0-next.377
0.0.111.0.0-next.377
0.0.101.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
0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago