withastro / withastro/docs

Missing detail for node adapter middleware examples

Open Beginner friendly
#13,864 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

improve or update documentation
Dominant language
MDX
Stars
1.7k
Forks
1.9k
Avg merge
1d 12h
Merged PRs (30d)
97

Description

๐Ÿ“š 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up โ€” it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the middleware section at docs.astro.build/en/guides/integrations-guide/node/#middleware and compare its Fastify example with packages/integrations/node/src/serve-app.ts around line 119. Update the examples or guidance so error pages such as 404.astro and client assets are both handled, then verify the documented behavior and deployment use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.