#Netlify edge middleware

5 messages · Page 1 of 1 (latest)

ornate kraken
#

I am using the netlify adater and have the following astro middleware. It is not running on Netlify, what am I doing wrong?

import { defineMiddleware } from 'astro:middleware';

export const onRequest = defineMiddleware(async (context, next) => {
// Get the cookie to check if it exists
let prepr_uid = context.cookies.get('__prepr_uid')?.value;

// If the cookie doesn't exist yet, generate a random ID
if (!prepr_uid) {
prepr_uid = crypto.randomUUID();
context.cookies.set('__prepr_uid', prepr_uid, {
maxAge: 365 * 24 * 60 * 60, // Set for one year (in seconds)
path: '/',
httpOnly: true,
sameSite: 'lax',
});
}

// Store the Prepr Customer ID in locals to make it available
// throughout the request lifecycle
context.locals.preprCustomerId = prepr_uid;

// Get the response
const response = await next();

// Set the header for API requests
response.headers.set('Prepr-Customer-Id', prepr_uid);

return response;
});

This is the adapter config:

// @ts-check
import { defineConfig } from 'astro/config';
import tailwindcss from "@tailwindcss/vite";
import netlify from "<@&1055234544183287879>/netlify";

// https://astro.build/config
export default defineConfig({
vite: {
plugins: [tailwindcss()],
},
output: "server",
edgeMiddleware: true,
trailingSlash: "never",
adapter: netlify({
imageCDN: false,
devFeatures: {
images: false,
environmentVariables: false,
},
}),
});

limber jasperBOT
#

Hi @ornate kraken. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Your middleware code looks fine. The issue is how Edge Middleware is being enabled for Netlify.

According to the Netlify adapter docs, Astro middleware only runs at build-time for prerendered pages and at runtime for on‑demand pages. To also run it on Netlify Edge Functions for all requests (including prerendered pages and static assets), you must enable edgeMiddleware on the Netlify adapter, not as a top‑level Astro config option. [Netlify usage]

Your current config:

export default defineConfig({
  // ...
  output: "server",
  edgeMiddleware: true,          // ❌ this key is not used here
  adapter: netlify({
    imageCDN: false,
    devFeatures: {
      images: false,
      environmentVariables: false,
    },
  }),
});

Needs to be:

import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify';

export default defineConfig({
  output: 'server',
  trailingSlash: 'never',
  adapter: netlify({
    edgeMiddleware: true,        // ✅ enable Edge middleware on the adapter
    imageCDN: false,
    devFeatures: {
      images: false,
      environmentVariables: false,
    },
  }),
});

With edgeMiddleware: true on the adapter:

  • Astro will emit a Netlify Edge Function that runs your src/middleware.(js|ts) for all requests (static, prerendered, and on‑demand). [Netlify usage]
  • For on‑demand pages, context.locals is serialized and passed from the Edge function to the serverless rendering function. [Netlify usage]

Also make sure:

#
  • The middleware file is src/middleware.js|ts (or src/middleware/index.js|ts) and exports onRequest. [Middleware basic]
  • You deploy the built .netlify/ folder via netlify deploy after astro build. [Netlify usage]

If after this it still “doesn’t run,” check that you’re hitting an on‑demand route or have output: 'server' (which you do), since cookies and other SSR features only apply to routes rendered on demand. [Cookies]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@ornate kraken Kapa.ai is still learning and improving, please let me know how I did by reacting below.