Skip to content

Missing detail for node adapter middleware examples #13864

@james-yeoman

Description

@james-yeoman

📚 Subject area/topic

Examples

📋 Page(s) affected (or suggested, for new content)

https://docs.astro.build/en/guides/integrations-guide/node/#middleware

📋 Description of content that is out-of-date or incorrect

The fastify example doesn't take into account error pages.

As-is, the example will hit https://github.com/withastro/astro/blob/d365c975ba2d88fc1dbdfe698df2bf9e2eafadce/packages/integrations/node/src/serve-app.ts#L119 and result in a blank error response, rather than rendering e.g. 404.astro

I feel like there should at least be an additional example, in order to have both the rendering of error pages e.g. 404.astro, and serving of client assets (which is helpful when deploying to e.g. aws lambda using @fastify/aws-lambda).

import type { NextHandleFunction } from "@fastify/middie";

import { readdirSync } from "node:fs";

import Fastify from 'fastify';
import fastifyMiddie from '@fastify/middie';
import fastifyStatic from '@fastify/static';
import { fileURLToPath } from 'node:url';
import { handler as ssrHandler } from './dist/server/entry.mjs';

const app = Fastify({ logger: true });

const root = fileURLToPath(new URL('./dist/client', import.meta.url));

await app
  .register(fastifyStatic, { root })
  .register(fastifyMiddie);

const assetFilenames = readdirSync(root).map(({ name }) => `/${name}`);

const fn: NextHandleFunction = async (req, res, next) => {
    if (req.url) {
        const isAsset = assetFilenames.includes(req.url);
        if (isAsset || req.url.startsWith("/_astro")) {
            next();
            return;
        }
    }

    // Not a client asset, let astro take over
    await ssrHandler(req, res);
};

app.use(fn);

🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    improve or update documentationEnhance / update existing documentation (e.g. add example, improve description, update for changes)

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions