Skip to content

Commit

Permalink
re-use aws manifest output to create our manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx committed Feb 5, 2025
1 parent 1cce4af commit 6aaa774
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
1 change: 0 additions & 1 deletion packages/cloudflare/src/cli/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export async function build(projectOpts: ProjectOptions): Promise<void> {
const { config, buildDir } = await compileOpenNextConfig(baseDir);

ensureCloudflareConfig(config);

// Initialize options
const options = buildHelper.normalizeOptions(config, openNextDistDir, buildDir);
logger.setLevel(options.debug ? "debug" : "info");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ import { 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[] };
Expand All @@ -24,25 +19,24 @@ 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<CacheAssetsManifest>(
(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$/, "");
export function extractCacheAssetsManifest(options: BuildOptions) {
// TODO: Expose the function for getting this data as an adapter-agnostic utility in AWS.
const rawManifest: RawManifest = JSON.parse(
readFileSync(path.join(options.outputDir, "dynamodb-provider", "dynamodb-cache.json"), "utf-8")
);

acc.paths[routePath] = tags;
const manifest = rawManifest.reduce<CacheAssetsManifest>(
(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;
Expand Down

0 comments on commit 6aaa774

Please sign in to comment.