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 6, 2025
1 parent 4a28bba commit 3566d04
Showing 1 changed file with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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[] };
Expand All @@ -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<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$/, "");

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<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 3566d04

Please sign in to comment.