#Netlify edgeMiddleware
9 messages · Page 1 of 1 (latest)
Hi @toxic mist. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
edgeMiddleware: true does the opposite of what you want: it runs your Astro middleware via a Netlify Edge Function for all requests – including static assets and prerendered pages – in addition to on‑demand rendered (SSR) pages.[Netlify usage]
However, Astro’s middleware always runs in two cases, regardless of edgeMiddleware:
- At build time for prerendered pages.
- At request time for on‑demand rendered pages (SSR).[Middleware guide]
To approximate “only run for SSR pages” you can:
-
Turn off
edgeMiddlewarein the Netlify adapter so static assets and prerendered pages do not go through a Netlify Edge Function:// astro.config.mjs import { defineConfig } from 'astro/config'; import netlify from '@astrojs/netlify'; export default defineConfig({ // ... adapter: netlify({ edgeMiddleware: false, // or just omit this option }), }); -
Inside your middleware, check
isPrerenderedand early‑return so your logic effectively only runs for SSR/on‑demand requests:// src/middleware.ts import type { MiddlewareHandler } from 'astro'; export const onRequest: MiddlewareHandler = async (context, next) => { if (context.isPrerendered) { // Skip logic during prerender; just continue. return next(); } // Your SSR-only middleware logic here return next(); };
Astro exposes isPrerendered specifically for this kind of conditional middleware logic.[isPrerendered]
With this setup:
- Prerendered pages: middleware runs at build time but does nothing (skips).
- SSR / on‑demand pages: middleware runs normally.
- Static assets: do not go through edge middleware, because
edgeMiddlewareis disabled.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@toxic mist Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@clear ledge but if I set edgeMiddleware to false on the adapter, then the middleware won't run on Netlify?
Let me check the knowledge base for you again. One moment! :books:
You’re right to be careful here; the behavior depends on what kind of pages you have and what edgeMiddleware actually does.
From the Netlify adapter docs:
- With
edgeMiddleware: true, your Astro middleware is turned into a Netlify Edge Function that runs “for all requests including static assets, prerendered pages, and on-demand rendered pages.” [Netlify usage] - This is specifically about running middleware at the edge, so that it can affect prerendered pages and static assets as they’re served. [Netlify usage]
Separately, the general middleware guide says:
- Middleware “allows you to intercept requests and responses … every time a page or endpoint is about to be rendered. This rendering occurs at build time for all prerendered pages, but occurs when the route is requested for pages rendered on demand.” [Middleware]
Putting that together based only on the docs:
edgeMiddleware: trueis an opt‑in extra that pushes your middleware into a Netlify Edge Function so it can also run on already‑built static output (prerendered pages, assets) at request time. [Netlify usage]- The docs do not explicitly say that setting
edgeMiddleware: false(or omitting it) disables middleware entirely on Netlify; they only describe that it disables this edge‑function behavior. I don’t have a clear, explicit statement in the sources that confirms exactly how middleware runs for SSR pages whenedgeMiddlewareis off, so I cannot state that with certainty.
So, strictly from the available documentation:
edgeMiddleware: true→ middleware runs at the edge for all requests (including prerendered and static assets).- What remains when
edgeMiddlewareisfalseis not fully documented in these sources; they don’t clearly describe whether SSR pages still use middleware via Netlify Functions in that case.
If you specifically need “middleware only for SSR pages, not for prerendered or static assets,” the docs don’t describe a supported configuration that guarantees this on Netlify. You may need to:
- Keep
edgeMiddleware: trueand checkcontext.isPrerenderedin middleware to no‑op for prerendered requests, while accepting that static assets still traverse the edge function, or - Consult Netlify’s own Astro integration docs / examples to confirm the exact behavior when
edgeMiddlewareis omitted, as that detail is not explicit in the Astro docs.