/astro-fastify

A Fastify adapter for Astro

Primary LanguageJavaScript

npm version number

@matthewp/astro-fastify

An fastify adapter to use in Astro projects. @matthewp/astro-fastify allows you to use fastify and Astro side-by-side, and deploy your apps to Node.js powered by a battle-tested HTTP server.

Unlike most adapters, @matthewp/astro-fastify also works in dev mode.

install

@matthewp/astro-fastify is needed in production so use --save.

npm install @matthewp/astro-fastify --save

Usage

@matthewp/astro-fastify is used like any other adapter for Astro. Import and use it in your astro.config.mjs file:

import fastify from '@matthewp/astro-fastify';

/** @type {import('astro').AstroUserConfig} */
export default {
  output: 'server',
  adapter: fastify({
    entry: new URL('./api/index.ts', import.meta.url)
  })
};

Options

entry

Specifies the entry point to define fastify routes and plugins. This module must export a default function that takes in the Fastify instance, where you can define routes and registry plugins.

api/index.ts

import type { DefineFastifyRoutes } from '@matthewp/astro-fastify';

const defineRoutes: DefineFastifyRoutes = (fastify) => {
  fastify.get('/api/todos', function(request, reply) {
    reply.send({
      todos: [
        { label: 'eat lunch' },
        { label: 'exercise' },
        { label: 'walk the dog' }
      ]
    });
  })
};

export default defineRoutes;

port

Specifies the port to use in production. Most hosts will set process.env.PORT and that will be used, so setting this option is unnecessary. If you do set port it will override host-specific config.

In development mode this option has no effect, as fastify runs on the same server as Astro.

logger

Specifies the Fastify logging options. See the Fastify docs to see the options. Note that these options are built into the production bundle, so options such as logger.stream do not work.

Note on route priority

Fastify runs in front of Astro's own routing, which means that any routes you define in fastify take priority over routes defined in Astro. So if, for example, you have conflicting routes the Astro route will never be hit.

Production usage

@matthewp/astro-fastify automatically configures Astro to build your fastify routes into your production build. Run your build as normal:

astro build

Or if you have an npm script:

npm run build

Which will create an entrypoint for your server, by default dist/server/entry.mjs. Running this file in Node.js automatically starts the fastify server:

node dist/server/entry.mjs

Configure your host to run this script. Assets and JavaScript are output to dist/client/assets/. You can configure your CDN to serve these files more efficiently and with long-lived cache headers.

License

BSD-2-Clause