summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alexander Niebuhr <alexander@nbhr.io> 2024-08-03 08:06:39 +0200
committerGravatar GitHub <noreply@github.com> 2024-08-03 08:06:39 +0200
commitd6aa05b92cf15e469038b16f18bfab6d92de63c9 (patch)
tree395b0ce453889d22628ffd724776a6461e6debbd
parenta5d9215b4c035795784d59a14493222d1556848c (diff)
downloadastro-d6aa05b92cf15e469038b16f18bfab6d92de63c9.tar.gz
astro-d6aa05b92cf15e469038b16f18bfab6d92de63c9.tar.zst
astro-d6aa05b92cf15e469038b16f18bfab6d92de63c9.zip
fix(cloudflare): env vars in middleware for prerendering (#340)
-rw-r--r--packages/integrations/cloudflare/package.json4
-rw-r--r--packages/integrations/cloudflare/src/entrypoints/middleware.ts15
-rw-r--r--packages/integrations/cloudflare/src/index.ts15
3 files changed, 32 insertions, 2 deletions
diff --git a/packages/integrations/cloudflare/package.json b/packages/integrations/cloudflare/package.json
index 55453b9c6..c10803cac 100644
--- a/packages/integrations/cloudflare/package.json
+++ b/packages/integrations/cloudflare/package.json
@@ -17,6 +17,7 @@
"exports": {
".": "./dist/index.js",
"./entrypoints/server.js": "./dist/entrypoints/server.js",
+ "./entrypoints/middleware.js": "./dist/entrypoints/middleware.js",
"./image-service": "./dist/entrypoints/image-service.js",
"./image-endpoint": "./dist/entrypoints/image-endpoint.js",
"./package.json": "./package.json"
@@ -35,7 +36,8 @@
"magic-string": "^0.30.10",
"miniflare": "^3.20240620.0",
"tiny-glob": "^0.2.9",
- "wrangler": "^3.62.0"
+ "wrangler": "^3.62.0",
+ "@inox-tools/astro-when": "^0.2.0"
},
"peerDependencies": {
"astro": "^4.10.3"
diff --git a/packages/integrations/cloudflare/src/entrypoints/middleware.ts b/packages/integrations/cloudflare/src/entrypoints/middleware.ts
new file mode 100644
index 000000000..3acd1e2f3
--- /dev/null
+++ b/packages/integrations/cloudflare/src/entrypoints/middleware.ts
@@ -0,0 +1,15 @@
+import { When, whenAmI } from '@it-astro:when';
+import type { MiddlewareHandler } from 'astro';
+
+const middlewares: Record<any, MiddlewareHandler> = {
+ [When.Prerender]: (ctx, next) => {
+ if (ctx.locals.runtime === undefined) {
+ ctx.locals.runtime = {
+ env: process.env,
+ };
+ }
+ return next();
+ },
+};
+
+export const onRequest = middlewares[whenAmI];
diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts
index 1a14fa957..718fac020 100644
--- a/packages/integrations/cloudflare/src/index.ts
+++ b/packages/integrations/cloudflare/src/index.ts
@@ -10,6 +10,7 @@ import {
removeLeadingForwardSlash,
} from '@astrojs/internal-helpers/path';
import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
+import astroWhen from '@inox-tools/astro-when';
import { AstroError } from 'astro/errors';
import { getPlatformProxy } from 'wrangler';
import {
@@ -108,7 +109,14 @@ export default function createIntegration(args?: Options): AstroIntegration {
return {
name: '@astrojs/cloudflare',
hooks: {
- 'astro:config:setup': ({ command, config, updateConfig, logger, addWatchFile }) => {
+ 'astro:config:setup': ({
+ command,
+ config,
+ updateConfig,
+ logger,
+ addWatchFile,
+ addMiddleware,
+ }) => {
updateConfig({
build: {
client: new URL(`.${wrapWithSlashes(config.base)}`, config.outDir),
@@ -123,10 +131,15 @@ export default function createIntegration(args?: Options): AstroIntegration {
cloudflareModulePlugin,
],
},
+ integrations: [astroWhen()],
image: setImageConfig(args?.imageService ?? 'compile', config.image, command, logger),
});
addWatchFile(new URL('./wrangler.toml', config.root));
addWatchFile(new URL('./wrangler.json', config.root));
+ addMiddleware({
+ entrypoint: '@astrojs/cloudflare/entrypoints/middleware.js',
+ order: 'pre',
+ });
},
'astro:config:done': ({ setAdapter, config }) => {
if (config.output === 'static') {