Skip to content

Commit

Permalink
enable access to cloudflare runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
AirBorne04 authored and AirBorne04 committed Oct 21, 2022
1 parent 7e3a5e5 commit da2a317
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/poor-frogs-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

enable access to cloudflare runtime [KV, R2, Durable Objects]
7 changes: 7 additions & 0 deletions packages/integrations/cloudflare/src/server.advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { App } from 'astro/app';

type Env = {
ASSETS: { fetch: (req: Request) => Promise<Response> };
name: string;
};

export function createExports(manifest: SSRManifest) {
const app = new App(manifest, false);

const fetch = async (request: Request, env: Env) => {
const { origin, pathname } = new URL(request.url);
env.name = "cloudflare";

// static assets
if (manifest.assets.has(pathname)) {
Expand All @@ -26,6 +28,11 @@ export function createExports(manifest: SSRManifest) {
Symbol.for('astro.clientAddress'),
request.headers.get('cf-connecting-ip')
);
Reflect.set(
request,
Symbol.for('runtime'),
env
);
let response = await app.render(request, routeData);

if (app.setCookieHeaders) {
Expand Down
9 changes: 8 additions & 1 deletion packages/integrations/cloudflare/src/server.directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export function createExports(manifest: SSRManifest) {
const onRequest = async ({
request,
next,
...runtimeEnv
}: {
request: Request;
next: (request: Request) => void;
}) => {
} & Record<string, unknown>) => {
const { origin, pathname } = new URL(request.url);
runtimeEnv.name = "cloudflare";

// static assets
if (manifest.assets.has(pathname)) {
Expand All @@ -28,6 +30,11 @@ export function createExports(manifest: SSRManifest) {
Symbol.for('astro.clientAddress'),
request.headers.get('cf-connecting-ip')
);
Reflect.set(
request,
Symbol.for('runtime'),
runtimeEnv
);
let response = await app.render(request, routeData);

if (app.setCookieHeaders) {
Expand Down

0 comments on commit da2a317

Please sign in to comment.