astro-fastify icon indicating copy to clipboard operation
astro-fastify copied to clipboard

Can't set cookies on Astro routes

Open PeterDraex opened this issue 8 months ago • 0 comments

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!

PeterDraex avatar Jun 16 '24 09:06 PeterDraex