matthewp / astro-fastify

A Fastify adapter for Astro
62 stars 16 forks source link

Can't set cookies on Astro routes #37

Open PeterDraex opened 5 months ago

PeterDraex commented 5 months ago

I set up a Fastify middleware like to create a cookie. The cookie is created correctly for Fastify routes (/api/todos), but the cookies don't make it to the HTTP response of Astro pages.

My index.ts:

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

const defineRoutes: DefineFastifyRoutes = (fastify) => {
    fastify.register(fastifyCookie);

    fastify.addHook("onRequest", async (request, reply) => {
      reply.setCookie("someCookie", "someValue");
    });

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

I've tried adding a console.log to the middleware and it gets printed for both pages, so it seems like reply.setCookie is executed in both cases. Why is the cookie missing from Astro routes HTTP response (I've checked via Chrome Developer Tools)? It seems like a bug? Is there a workaround?

Thanks!