Skip to content

Commit

Permalink
fix(cloudflare): env vars in middleware & prerendering (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderniebuhr authored Aug 3, 2024
1 parent e031aed commit a430ab1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tall-shrimps-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

Fixes an issue if environment variables where used inside the middleware and a prerendering occured.
8 changes: 7 additions & 1 deletion packages/cloudflare/src/entrypoints/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { When, whenAmI } from '@it-astro:when';
import type { MiddlewareHandler } from 'astro';

const middlewares: Record<any, MiddlewareHandler> = {
const middlewares: Record<When, MiddlewareHandler> = {
[When.Client]: () => {
throw new Error('Client should not run a middleware!');
},
[When.DevServer]: (_, next) => next(),
[When.Server]: (_, next) => next(),
[When.Prerender]: (ctx, next) => {
if (ctx.locals.runtime === undefined) {
ctx.locals.runtime = {
Expand All @@ -10,6 +15,7 @@ const middlewares: Record<any, MiddlewareHandler> = {
}
return next();
},
[When.StaticBuild]: (_, next) => next(),
};

export const onRequest = middlewares[whenAmI];

1 comment on commit a430ab1

@nlnw
Copy link

@nlnw nlnw commented on a430ab1 Aug 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks "astro dev" for everyone.
This is not a package: "@it-astro:when".

Please sign in to comment.