|
| 1 | +/** |
| 2 | + * `CacheHint.lastModified` carries the content's `updated_at`, and Astro emits it |
| 3 | + * as the response `Last-Modified`. A deploy that changes only code therefore |
| 4 | + * leaves the validator untouched: a returning visitor revalidates, gets 304, and |
| 5 | + * keeps HTML referencing `/_astro/*` files the new deployment no longer has — |
| 6 | + * 404 on Workers, so the page renders without CSS or JS. |
| 7 | + * |
| 8 | + * The middleware folds the build timestamp into the validator, so the response |
| 9 | + * date reflects the response rather than only the content. |
| 10 | + */ |
| 11 | +import { beforeEach, describe, it, expect, vi } from "vitest"; |
| 12 | + |
| 13 | +vi.mock("astro:middleware", () => ({ |
| 14 | + defineMiddleware: (handler: unknown) => handler, |
| 15 | +})); |
| 16 | + |
| 17 | +const { BUILD_TIME, MOCK_RUNTIME } = vi.hoisted(() => { |
| 18 | + const ok = async () => ({ success: true }); |
| 19 | + return { |
| 20 | + BUILD_TIME: Date.parse("2026-08-07T22:26:49.000Z"), |
| 21 | + MOCK_RUNTIME: { |
| 22 | + storage: { getPublicUrl: vi.fn((key: string) => `https://media.example.com/${key}`) }, |
| 23 | + db: {}, |
| 24 | + hooks: {}, |
| 25 | + email: null, |
| 26 | + configuredPlugins: [], |
| 27 | + getPluginRouteMeta: () => null, |
| 28 | + handlePluginApiRoute: async () => ({ success: true }), |
| 29 | + getMediaProvider: () => undefined, |
| 30 | + getMediaProviderList: () => [], |
| 31 | + collectPageMetadata: async () => [], |
| 32 | + collectPageFragments: async () => [], |
| 33 | + ensureSearchHealthy: async () => undefined, |
| 34 | + getManifest: async () => ({}), |
| 35 | + getSandboxRunner: () => null, |
| 36 | + isSandboxBypassed: () => false, |
| 37 | + syncMarketplacePlugins: async () => undefined, |
| 38 | + syncRegistryPlugins: async () => undefined, |
| 39 | + setPluginStatus: async () => undefined, |
| 40 | + handleContentList: ok, |
| 41 | + }, |
| 42 | + }; |
| 43 | +}); |
| 44 | + |
| 45 | +vi.mock("virtual:emdash/build", () => ({ buildTime: BUILD_TIME }), { virtual: true }); |
| 46 | +vi.mock( |
| 47 | + "virtual:emdash/config", |
| 48 | + () => ({ default: { database: { config: { binding: "DB" } }, auth: { mode: "none" } } }), |
| 49 | + { virtual: true }, |
| 50 | +); |
| 51 | +vi.mock( |
| 52 | + "virtual:emdash/dialect", |
| 53 | + () => ({ createDialect: vi.fn(), createRequestScopedDb: vi.fn().mockReturnValue(null) }), |
| 54 | + { virtual: true }, |
| 55 | +); |
| 56 | +vi.mock("virtual:emdash/media-providers", () => ({ mediaProviders: [] }), { virtual: true }); |
| 57 | +vi.mock("virtual:emdash/plugins", () => ({ plugins: [] }), { virtual: true }); |
| 58 | +vi.mock( |
| 59 | + "virtual:emdash/sandbox-runner", |
| 60 | + () => ({ createSandboxRunner: null, sandboxBypassed: false, sandboxEnabled: false }), |
| 61 | + { virtual: true }, |
| 62 | +); |
| 63 | +vi.mock("virtual:emdash/sandboxed-plugins", () => ({ sandboxedPlugins: [] }), { virtual: true }); |
| 64 | +vi.mock("virtual:emdash/storage", () => ({ createStorage: null }), { virtual: true }); |
| 65 | +vi.mock("virtual:emdash/wait-until", () => ({ waitUntil: undefined }), { virtual: true }); |
| 66 | +vi.mock("virtual:emdash/scheduler", () => ({ createScheduler: null }), { virtual: true }); |
| 67 | + |
| 68 | +vi.mock("../../../src/emdash-runtime.js", () => ({ |
| 69 | + DB_INIT_DEADLINE_MS: 30_000, |
| 70 | + EmDashRuntime: { create: async () => MOCK_RUNTIME }, |
| 71 | +})); |
| 72 | + |
| 73 | +vi.mock("../../../src/loader.js", () => ({ |
| 74 | + getDb: vi.fn(async () => ({ |
| 75 | + selectFrom: () => ({ selectAll: () => ({ limit: () => ({ execute: async () => [] }) }) }), |
| 76 | + })), |
| 77 | +})); |
| 78 | + |
| 79 | +import onRequest from "../../../src/astro/middleware.js"; |
| 80 | + |
| 81 | +/** |
| 82 | + * Stand-in for Astro's `AstroCache`, mirroring the accumulation rules the real |
| 83 | + * one applies in `core/cache/runtime/cache.js`: `lastModified` keeps the later |
| 84 | + * date, `set(false)` clears accumulated state, and any later `set()` re-enables. |
| 85 | + */ |
| 86 | +function createCache(enabled = true) { |
| 87 | + let disabled = false; |
| 88 | + const options: { lastModified?: Date; tags?: string[] } = {}; |
| 89 | + return { |
| 90 | + enabled, |
| 91 | + set(input: { lastModified?: Date; tags?: string[] } | false) { |
| 92 | + if (input === false) { |
| 93 | + disabled = true; |
| 94 | + delete options.lastModified; |
| 95 | + delete options.tags; |
| 96 | + return; |
| 97 | + } |
| 98 | + disabled = false; |
| 99 | + if ( |
| 100 | + input.lastModified && |
| 101 | + (!options.lastModified || input.lastModified > options.lastModified) |
| 102 | + ) { |
| 103 | + options.lastModified = input.lastModified; |
| 104 | + } |
| 105 | + if (input.tags) options.tags = [...(options.tags ?? []), ...input.tags]; |
| 106 | + }, |
| 107 | + get disabled() { |
| 108 | + return disabled; |
| 109 | + }, |
| 110 | + get options() { |
| 111 | + return options; |
| 112 | + }, |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +type TestCache = ReturnType<typeof createCache>; |
| 117 | + |
| 118 | +function anonymousPublicPageContext(cache: TestCache) { |
| 119 | + return { |
| 120 | + request: new Request("https://example.com/posts/hello"), |
| 121 | + url: new URL("https://example.com/posts/hello"), |
| 122 | + cookies: { get: vi.fn(() => undefined), set: vi.fn() }, |
| 123 | + locals: {} as Record<string, unknown>, |
| 124 | + redirect: vi.fn(), |
| 125 | + isPrerendered: false, |
| 126 | + session: { get: vi.fn(async () => null) }, |
| 127 | + cache, |
| 128 | + } as Record<string, unknown>; |
| 129 | +} |
| 130 | + |
| 131 | +/** A page rendering with `Astro.cache.set(cacheHint)`, as the demos do. */ |
| 132 | +function pageSetting(cache: TestCache, hint: { lastModified?: Date; tags?: string[] } | false) { |
| 133 | + return async () => { |
| 134 | + cache.set(hint); |
| 135 | + return new Response("<html></html>", { headers: { "content-type": "text/html" } }); |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +describe("astro middleware cache validator", () => { |
| 140 | + beforeEach(() => { |
| 141 | + vi.clearAllMocks(); |
| 142 | + }); |
| 143 | + |
| 144 | + it("raises a content-only validator to the build time", async () => { |
| 145 | + const cache = createCache(); |
| 146 | + const contentModified = new Date(BUILD_TIME - 6 * 60 * 60 * 1000); |
| 147 | + |
| 148 | + await onRequest( |
| 149 | + anonymousPublicPageContext(cache) as Parameters<typeof onRequest>[0], |
| 150 | + pageSetting(cache, { lastModified: contentModified, tags: ["posts"] }), |
| 151 | + ); |
| 152 | + |
| 153 | + expect(cache.options.lastModified?.getTime()).toBe(BUILD_TIME); |
| 154 | + }); |
| 155 | + |
| 156 | + it("keeps a content validator newer than the build", async () => { |
| 157 | + const cache = createCache(); |
| 158 | + const contentModified = new Date(BUILD_TIME + 60 * 60 * 1000); |
| 159 | + |
| 160 | + await onRequest( |
| 161 | + anonymousPublicPageContext(cache) as Parameters<typeof onRequest>[0], |
| 162 | + pageSetting(cache, { lastModified: contentModified, tags: ["posts"] }), |
| 163 | + ); |
| 164 | + |
| 165 | + expect(cache.options.lastModified?.getTime()).toBe(contentModified.getTime()); |
| 166 | + }); |
| 167 | + |
| 168 | + it("leaves a route that opts out of caching opted out", async () => { |
| 169 | + const cache = createCache(); |
| 170 | + |
| 171 | + await onRequest( |
| 172 | + anonymousPublicPageContext(cache) as Parameters<typeof onRequest>[0], |
| 173 | + pageSetting(cache, false), |
| 174 | + ); |
| 175 | + |
| 176 | + expect(cache.disabled).toBe(true); |
| 177 | + expect(cache.options.lastModified).toBeUndefined(); |
| 178 | + }); |
| 179 | + |
| 180 | + it("leaves prerendered requests to the host's static layer", async () => { |
| 181 | + const cache = createCache(); |
| 182 | + const context = anonymousPublicPageContext(cache); |
| 183 | + context.isPrerendered = true; |
| 184 | + |
| 185 | + await onRequest( |
| 186 | + context as Parameters<typeof onRequest>[0], |
| 187 | + async () => new Response("<html></html>", { headers: { "content-type": "text/html" } }), |
| 188 | + ); |
| 189 | + |
| 190 | + expect(cache.options.lastModified).toBeUndefined(); |
| 191 | + }); |
| 192 | + |
| 193 | + it("does not touch the cache when no provider is configured", async () => { |
| 194 | + const cache = createCache(false); |
| 195 | + |
| 196 | + await onRequest( |
| 197 | + anonymousPublicPageContext(cache) as Parameters<typeof onRequest>[0], |
| 198 | + async () => new Response("<html></html>", { headers: { "content-type": "text/html" } }), |
| 199 | + ); |
| 200 | + |
| 201 | + expect(cache.options.lastModified).toBeUndefined(); |
| 202 | + }); |
| 203 | +}); |
0 commit comments