Skip to content

Commit f0c73a6

Browse files
committed
refactor: extract compileTagCacheProvider
1 parent 612f09f commit f0c73a6

File tree

4 files changed

+60
-45
lines changed

4 files changed

+60
-45
lines changed

packages/open-next/src/build.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "./build/buildNextApp.js";
77
import { compileCache } from "./build/compileCache.js";
88
import { compileOpenNextConfig } from "./build/compileConfig.js";
9+
import { compileTagCacheProvider } from "./build/compileTagCacheProvider.js";
910
import { createCacheAssets, createStaticAssets } from "./build/createAssets.js";
1011
import { createImageOptimizationBundle } from "./build/createImageOptimizationBundle.js";
1112
import { createMiddleware } from "./build/createMiddleware.js";
@@ -65,7 +66,13 @@ export async function build(
6566
await createMiddleware(options);
6667

6768
createStaticAssets(options);
68-
await createCacheAssets(options);
69+
70+
if (config.dangerous?.disableIncrementalCache !== true) {
71+
const { useTagCache } = createCacheAssets(options);
72+
if (useTagCache) {
73+
await compileTagCacheProvider(options);
74+
}
75+
}
6976

7077
await createServerBundle(options);
7178
await createRevalidationBundle(options);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import path from "node:path";
2+
3+
import { openNextResolvePlugin } from "../plugins/resolve.js";
4+
import * as buildHelper from "./helper.js";
5+
6+
export async function compileTagCacheProvider(
7+
options: buildHelper.BuildOptions,
8+
) {
9+
const providerPath = path.join(options.outputDir, "dynamodb-provider");
10+
11+
const overrides = options.config.initializationFunction?.override;
12+
13+
await buildHelper.esbuildAsync(
14+
{
15+
external: ["@aws-sdk/client-dynamodb"],
16+
entryPoints: [
17+
path.join(options.openNextDistDir, "adapters", "dynamo-provider.js"),
18+
],
19+
outfile: path.join(providerPath, "index.mjs"),
20+
target: ["node18"],
21+
plugins: [
22+
openNextResolvePlugin({
23+
fnName: "initializationFunction",
24+
overrides: {
25+
converter: overrides?.converter ?? "dummy",
26+
wrapper: overrides?.wrapper,
27+
},
28+
}),
29+
],
30+
},
31+
options,
32+
);
33+
}

packages/open-next/src/build/createAssets.ts

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import path from "node:path";
33

44
import { isBinaryContentType } from "../adapters/binary.js";
55
import logger from "../logger.js";
6-
import { openNextResolvePlugin } from "../plugins/resolve.js";
76
import * as buildHelper from "./helper.js";
87

98
export function createStaticAssets(options: buildHelper.BuildOptions) {
@@ -45,15 +44,19 @@ export function createStaticAssets(options: buildHelper.BuildOptions) {
4544
}
4645
}
4746

48-
export async function createCacheAssets(options: buildHelper.BuildOptions) {
49-
const { config } = options;
50-
if (config.dangerous?.disableIncrementalCache) return;
51-
47+
/**
48+
* Create the cache assets.
49+
*
50+
* @param options Build options.
51+
* @returns Wether tag cache is used.
52+
*/
53+
export function createCacheAssets(options: buildHelper.BuildOptions) {
5254
logger.info(`Bundling cache assets...`);
5355

5456
const { appBuildOutputPath, outputDir } = options;
5557
const packagePath = path.relative(options.monorepoRoot, appBuildOutputPath);
5658
const buildId = buildHelper.getBuildId(appBuildOutputPath);
59+
let useTagCache = false;
5760

5861
// Copy pages to cache folder
5962
const dotNextPath = path.join(
@@ -77,7 +80,7 @@ export async function createCacheAssets(options: buildHelper.BuildOptions) {
7780
(file.endsWith(".html") && htmlPages.has(file)),
7881
);
7982

80-
//merge cache files into a single file
83+
// Merge cache files into a single file
8184
const cacheFilesPath: Record<
8285
string,
8386
{
@@ -94,18 +97,17 @@ export async function createCacheAssets(options: buildHelper.BuildOptions) {
9497
() => true,
9598
(filepath) => {
9699
const ext = path.extname(filepath);
97-
let newFilePath =
98-
ext !== "" ? filepath.replace(ext, ".cache") : `${filepath}.cache`;
99-
// Handle prefetch cache files for partial prerendering
100-
if (newFilePath.endsWith(".prefetch.cache")) {
101-
newFilePath = newFilePath.replace(".prefetch.cache", ".cache");
102-
}
103100
switch (ext) {
104101
case ".meta":
105102
case ".html":
106103
case ".json":
107104
case ".body":
108105
case ".rsc":
106+
const newFilePath =
107+
filepath
108+
.substring(0, filepath.length - ext.length)
109+
.replace(/\.prefetch$/, "") + ".cache";
110+
109111
cacheFilesPath[newFilePath] = {
110112
[ext.slice(1)]: filepath,
111113
...cacheFilesPath[newFilePath],
@@ -146,7 +148,7 @@ export async function createCacheAssets(options: buildHelper.BuildOptions) {
146148
fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent));
147149
});
148150

149-
if (!config.dangerous?.disableTagCache) {
151+
if (!options.config.dangerous?.disableTagCache) {
150152
// Generate dynamodb data
151153
// We need to traverse the cache to find every .meta file
152154
const metaFiles: {
@@ -216,36 +218,10 @@ export async function createCacheAssets(options: buildHelper.BuildOptions) {
216218
);
217219
}
218220

219-
// TODO: Extract the code below to a compileTagCacheProvider function
220221
if (metaFiles.length > 0) {
222+
useTagCache = true;
221223
const providerPath = path.join(outputDir, "dynamodb-provider");
222224

223-
await buildHelper.esbuildAsync(
224-
{
225-
external: ["@aws-sdk/client-dynamodb"],
226-
entryPoints: [
227-
path.join(
228-
options.openNextDistDir,
229-
"adapters",
230-
"dynamo-provider.js",
231-
),
232-
],
233-
outfile: path.join(providerPath, "index.mjs"),
234-
target: ["node18"],
235-
plugins: [
236-
openNextResolvePlugin({
237-
fnName: "initializationFunction",
238-
overrides: {
239-
converter:
240-
config.initializationFunction?.override?.converter ?? "dummy",
241-
wrapper: config.initializationFunction?.override?.wrapper,
242-
},
243-
}),
244-
],
245-
},
246-
options,
247-
);
248-
249225
//Copy open-next.config.mjs into the bundle
250226
buildHelper.copyOpenNextConfig(options.buildDir, providerPath);
251227

@@ -259,4 +235,6 @@ export async function createCacheAssets(options: buildHelper.BuildOptions) {
259235

260236
// We need to remove files later because we need the metafiles for dynamodb tags cache
261237
buildHelper.removeFiles(outputPath, (file) => !file.endsWith(".cache"));
238+
239+
return { useTagCache };
262240
}

packages/open-next/src/build/helper.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,7 @@ export function getHtmlPages(dotNextPath: string) {
232232
return Object.entries(JSON.parse(manifest))
233233
.filter(([_, value]) => (value as string).endsWith(".html"))
234234
.map(([_, value]) => (value as string).replace(/^pages\//, ""))
235-
.reduce((acc, page) => {
236-
acc.add(page);
237-
return acc;
238-
}, new Set<string>());
235+
.reduce((acc, page) => acc.add(page), new Set<string>());
239236
}
240237

241238
export function getBuildId(dotNextPath: string) {

0 commit comments

Comments
 (0)