Skip to content

Commit

Permalink
feature(cloudflare): add Caches bindings
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
alexanderniebuhr and sarah11918 committed Oct 2, 2023
1 parent 2edf350 commit 9f04df4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-kiwis-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': minor
---

Introduces support for local Caches bindings. Enhances development experience by allowing direct integration with `astro dev`.
23 changes: 3 additions & 20 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { AstroConfig, AstroIntegration, RouteData } from 'astro';

import { createRedirectsFromAstroRoutes } from '@astrojs/underscore-redirects';
import { CacheStorage } from '@miniflare/cache';
import { NoOpLog } from '@miniflare/shared';
import { MemoryStorage } from '@miniflare/storage-memory';
import { AstroError } from 'astro/errors';
import esbuild from 'esbuild';
import { Miniflare } from 'miniflare';
Expand Down Expand Up @@ -56,17 +53,6 @@ interface BuildConfig {
split?: boolean;
}

class StorageFactory {
storages = new Map();

storage(namespace: string) {
let storage = this.storages.get(namespace);
if (storage) return storage;
this.storages.set(namespace, (storage = new MemoryStorage()));
return storage;
}
}

export default function createIntegration(args?: Options): AstroIntegration {
let _config: AstroConfig;
let _buildConfig: BuildConfig;
Expand Down Expand Up @@ -140,6 +126,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
script: '',
cache: true,
cachePersist: true,
cacheWarnUsage: true,
d1Databases: D1Bindings,
d1Persist: true,
r2Buckets: R2Bindings,
Expand All @@ -161,6 +148,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
const namespace = await _mf.getKVNamespace(KVBinding);
Reflect.set(bindingsEnv, KVBinding, namespace);
}
const mfCache = await _mf.getCaches();

process.env.PWD = originalPWD;
const clientLocalsSymbol = Symbol.for('astro.locals');
Expand All @@ -183,12 +171,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
waitUntil: (_promise: Promise<any>) => {
return;
},
caches: new CacheStorage(
{ cache: true, cachePersist: false },
new NoOpLog(),
new StorageFactory(),
{}
),
caches: mfCache,
},
});
next();
Expand Down
13 changes: 10 additions & 3 deletions packages/integrations/cloudflare/test/cf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,20 @@ describe('Astro Cloudflare Runtime', () => {
await devServer?.stop();
});

it('Populates CF, Vars & Bindings', async () => {
it('adds cf object', async () => {
let res = await fixture.fetch('/');
expect(res.status).to.equal(200);
let html = await res.text();
let $ = cheerio.load(html);
expect($('#hasRuntime').text()).to.equal('true');
expect($('#hasCache').text()).to.equal('true');
expect($('#hasCF').text()).to.equal('true');
});

it('adds cache mocking', async () => {
let res = await fixture.fetch('/caches');
expect(res.status).to.equal(200);
let html = await res.text();
let $ = cheerio.load(html);
expect($('#hasCACHE').text()).to.equal('true');
});

it('adds D1 mocking', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
const runtime = Astro.locals.runtime;
---

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>CACHES</title>
</head>
<body>
<pre id="hasCACHE">{!!runtime.caches}</pre>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const runtime = Astro.locals.runtime;
</head>
<body>
<h1>Testing</h1>
<div id="hasRuntime">{!!runtime.cf?.colo}</div>
<div id="hasCache">{!!runtime.caches}</div>
<div id="hasCF">{!!runtime.cf?.colo}</div>
</body>
</html>

0 comments on commit 9f04df4

Please sign in to comment.