diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index b7a2274f79e27..ac4588e22a434 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -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'; @@ -23,6 +20,8 @@ import { wasmModuleLoader } from './utils/wasm-module-loader.js'; export type { AdvancedRuntime } from './entrypoints/server.advanced.js'; export type { DirectoryRuntime } from './entrypoints/server.directory.js'; +export { Response as MiniflareResponse } from 'miniflare'; + type Options = { mode?: 'directory' | 'advanced'; functionPerRoute?: boolean; @@ -56,17 +55,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; @@ -140,6 +128,7 @@ export default function createIntegration(args?: Options): AstroIntegration { script: '', cache: true, cachePersist: true, + cacheWarnUsage: true, d1Databases: D1Bindings, d1Persist: true, r2Buckets: R2Bindings, @@ -161,6 +150,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'); @@ -183,12 +173,7 @@ export default function createIntegration(args?: Options): AstroIntegration { waitUntil: (_promise: Promise) => { return; }, - caches: new CacheStorage( - { cache: true, cachePersist: false }, - new NoOpLog(), - new StorageFactory(), - {} - ), + caches: mfCache, }, }); next(); diff --git a/packages/integrations/cloudflare/test/cf.test.js b/packages/integrations/cloudflare/test/cf.test.js index a9f5607373f58..d09d23d10c8b6 100644 --- a/packages/integrations/cloudflare/test/cf.test.js +++ b/packages/integrations/cloudflare/test/cf.test.js @@ -63,13 +63,22 @@ 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'); + expect($('#hasDEFAULT').text()).to.equal('true'); + // expect($('#hasCUSTOM').text()).to.equal('true'); }); it('adds D1 mocking', async () => { diff --git a/packages/integrations/cloudflare/test/fixtures/cf/src/pages/caches.astro b/packages/integrations/cloudflare/test/fixtures/cf/src/pages/caches.astro new file mode 100644 index 0000000000000..db1a348574bee --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/cf/src/pages/caches.astro @@ -0,0 +1,30 @@ +--- +import { MiniflareResponse } from "@astrojs/cloudflare"; + +const runtime = Astro.locals.runtime; +try { + await runtime.caches.default.put( + 'http://localhost/cache', + new MiniflareResponse('true', { + headers: { 'Cache-Control': 'max-age=3600', 'X-Key': 'value' }, + }) + ); +} catch (error) { + console.log(error); +} +const response = await runtime.caches.default.match('http://localhost/cache'); +const body = await response.text(); +--- + + + + + + + CACHES + + +
{!!runtime.caches}
+
{!!body}
+ + diff --git a/packages/integrations/cloudflare/test/fixtures/cf/src/pages/index.astro b/packages/integrations/cloudflare/test/fixtures/cf/src/pages/index.astro index 0e904752fb29c..5ba11985f0c6e 100644 --- a/packages/integrations/cloudflare/test/fixtures/cf/src/pages/index.astro +++ b/packages/integrations/cloudflare/test/fixtures/cf/src/pages/index.astro @@ -7,7 +7,6 @@ const runtime = Astro.locals.runtime;

Testing

-
{!!runtime.cf?.colo}
-
{!!runtime.caches}
+
{!!runtime.cf?.colo}