diff --git a/packages/cloudflare/src/cli/build/utils/extract-cache-assets-manifest.ts b/packages/cloudflare/src/cli/build/utils/extract-cache-assets-manifest.ts index a1a79f37..d08b8d77 100644 --- a/packages/cloudflare/src/cli/build/utils/extract-cache-assets-manifest.ts +++ b/packages/cloudflare/src/cli/build/utils/extract-cache-assets-manifest.ts @@ -1,18 +1,13 @@ -import { readFileSync } from "node:fs"; +import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; -import { getBuildId } from "@opennextjs/aws/build/helper.js"; -import { globSync } from "glob"; - -type CacheInfo = { - type: string; - meta?: { - headers?: { - "x-next-cache-tags"?: string; - }; - }; -}; + +type RawManifest = { + tag: { S: string }; + path: { S: string }; + revalidatedAt: { N: string }; +}[]; export type CacheAssetsManifest = { tags: { [tag: string]: string[] }; @@ -24,25 +19,28 @@ export type CacheAssetsManifest = { * * The mapping creates an index of each tags pointing to its paths, and each path pointing to its tags. */ -export function extractCacheAssetsManifest(buildOpts: BuildOptions) { - const cachePath = path.join(buildOpts.outputDir, "cache"); - const buildId = getBuildId(buildOpts); - - const manifest = globSync(path.join(cachePath, buildId, "**/*.cache")).reduce( - (acc, p) => { - const { meta } = JSON.parse(readFileSync(p, "utf-8")) as CacheInfo; - const tags = meta?.headers?.["x-next-cache-tags"]?.split(",")?.map((tag) => `${buildId}/${tag}`) ?? []; - - const routePath = path.relative(cachePath, p).replace(/\.cache$/, ""); - - acc.paths[routePath] = tags; +export function extractCacheAssetsManifest(options: BuildOptions): CacheAssetsManifest { + // TODO: Expose the function for getting this data as an adapter-agnostic utility in AWS. + const rawManifestPath = path.join(options.outputDir, "dynamodb-provider", "dynamodb-cache.json"); + + if (!existsSync(rawManifestPath)) { + return { tags: {}, paths: {} }; + } + + const rawManifest: RawManifest = JSON.parse(readFileSync(rawManifestPath, "utf-8")); + + const manifest = rawManifest.reduce( + (acc, { tag: { S: tag }, path: { S: path } }) => { + if (!acc.paths[path]) { + acc.paths[path] = [tag]; + } else { + acc.paths[path].push(tag); + } - for (const tag of tags) { - if (!acc.tags[tag]) { - acc.tags[tag] = [routePath]; - } else { - acc.tags[tag].push(routePath); - } + if (!acc.tags[tag]) { + acc.tags[tag] = [path]; + } else { + acc.tags[tag].push(path); } return acc;