diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts index c5dc9870a..c699dba06 100755 --- a/packages/open-next/src/build.ts +++ b/packages/open-next/src/build.ts @@ -6,6 +6,7 @@ import { } from "./build/buildNextApp.js"; import { compileCache } from "./build/compileCache.js"; import { compileOpenNextConfig } from "./build/compileConfig.js"; +import { compileTagCacheProvider } from "./build/compileTagCacheProvider.js"; import { createCacheAssets, createStaticAssets } from "./build/createAssets.js"; import { createImageOptimizationBundle } from "./build/createImageOptimizationBundle.js"; import { createMiddleware } from "./build/createMiddleware.js"; @@ -65,7 +66,13 @@ export async function build( await createMiddleware(options); createStaticAssets(options); - await createCacheAssets(options); + + if (config.dangerous?.disableIncrementalCache !== true) { + const { useTagCache } = createCacheAssets(options); + if (useTagCache) { + await compileTagCacheProvider(options); + } + } await createServerBundle(options); await createRevalidationBundle(options); diff --git a/packages/open-next/src/build/compileTagCacheProvider.ts b/packages/open-next/src/build/compileTagCacheProvider.ts new file mode 100644 index 000000000..545faea76 --- /dev/null +++ b/packages/open-next/src/build/compileTagCacheProvider.ts @@ -0,0 +1,33 @@ +import path from "node:path"; + +import { openNextResolvePlugin } from "../plugins/resolve.js"; +import * as buildHelper from "./helper.js"; + +export async function compileTagCacheProvider( + options: buildHelper.BuildOptions, +) { + const providerPath = path.join(options.outputDir, "dynamodb-provider"); + + const overrides = options.config.initializationFunction?.override; + + await buildHelper.esbuildAsync( + { + external: ["@aws-sdk/client-dynamodb"], + entryPoints: [ + path.join(options.openNextDistDir, "adapters", "dynamo-provider.js"), + ], + outfile: path.join(providerPath, "index.mjs"), + target: ["node18"], + plugins: [ + openNextResolvePlugin({ + fnName: "initializationFunction", + overrides: { + converter: overrides?.converter ?? "dummy", + wrapper: overrides?.wrapper, + }, + }), + ], + }, + options, + ); +} diff --git a/packages/open-next/src/build/createAssets.ts b/packages/open-next/src/build/createAssets.ts index 8e8d3c07f..359aa5df5 100644 --- a/packages/open-next/src/build/createAssets.ts +++ b/packages/open-next/src/build/createAssets.ts @@ -3,7 +3,6 @@ import path from "node:path"; import { isBinaryContentType } from "../adapters/binary.js"; import logger from "../logger.js"; -import { openNextResolvePlugin } from "../plugins/resolve.js"; import * as buildHelper from "./helper.js"; export function createStaticAssets(options: buildHelper.BuildOptions) { @@ -45,15 +44,19 @@ export function createStaticAssets(options: buildHelper.BuildOptions) { } } -export async function createCacheAssets(options: buildHelper.BuildOptions) { - const { config } = options; - if (config.dangerous?.disableIncrementalCache) return; - +/** + * Create the cache assets. + * + * @param options Build options. + * @returns Whether tag cache is used. + */ +export function createCacheAssets(options: buildHelper.BuildOptions) { logger.info(`Bundling cache assets...`); const { appBuildOutputPath, outputDir } = options; const packagePath = path.relative(options.monorepoRoot, appBuildOutputPath); const buildId = buildHelper.getBuildId(appBuildOutputPath); + let useTagCache = false; // Copy pages to cache folder const dotNextPath = path.join( @@ -77,7 +80,7 @@ export async function createCacheAssets(options: buildHelper.BuildOptions) { (file.endsWith(".html") && htmlPages.has(file)), ); - //merge cache files into a single file + // Merge cache files into a single file const cacheFilesPath: Record< string, { @@ -94,18 +97,17 @@ export async function createCacheAssets(options: buildHelper.BuildOptions) { () => true, (filepath) => { const ext = path.extname(filepath); - let newFilePath = - ext !== "" ? filepath.replace(ext, ".cache") : `${filepath}.cache`; - // Handle prefetch cache files for partial prerendering - if (newFilePath.endsWith(".prefetch.cache")) { - newFilePath = newFilePath.replace(".prefetch.cache", ".cache"); - } switch (ext) { case ".meta": case ".html": case ".json": case ".body": case ".rsc": + const newFilePath = + filepath + .substring(0, filepath.length - ext.length) + .replace(/\.prefetch$/, "") + ".cache"; + cacheFilesPath[newFilePath] = { [ext.slice(1)]: filepath, ...cacheFilesPath[newFilePath], @@ -146,7 +148,7 @@ export async function createCacheAssets(options: buildHelper.BuildOptions) { fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent)); }); - if (!config.dangerous?.disableTagCache) { + if (!options.config.dangerous?.disableTagCache) { // Generate dynamodb data // We need to traverse the cache to find every .meta file const metaFiles: { @@ -216,36 +218,10 @@ export async function createCacheAssets(options: buildHelper.BuildOptions) { ); } - // TODO: Extract the code below to a compileTagCacheProvider function if (metaFiles.length > 0) { + useTagCache = true; const providerPath = path.join(outputDir, "dynamodb-provider"); - await buildHelper.esbuildAsync( - { - external: ["@aws-sdk/client-dynamodb"], - entryPoints: [ - path.join( - options.openNextDistDir, - "adapters", - "dynamo-provider.js", - ), - ], - outfile: path.join(providerPath, "index.mjs"), - target: ["node18"], - plugins: [ - openNextResolvePlugin({ - fnName: "initializationFunction", - overrides: { - converter: - config.initializationFunction?.override?.converter ?? "dummy", - wrapper: config.initializationFunction?.override?.wrapper, - }, - }), - ], - }, - options, - ); - //Copy open-next.config.mjs into the bundle buildHelper.copyOpenNextConfig(options.buildDir, providerPath); @@ -259,4 +235,6 @@ export async function createCacheAssets(options: buildHelper.BuildOptions) { // We need to remove files later because we need the metafiles for dynamodb tags cache buildHelper.removeFiles(outputPath, (file) => !file.endsWith(".cache")); + + return { useTagCache }; } diff --git a/packages/open-next/src/build/helper.ts b/packages/open-next/src/build/helper.ts index 4948b7514..196081a1c 100644 --- a/packages/open-next/src/build/helper.ts +++ b/packages/open-next/src/build/helper.ts @@ -232,10 +232,7 @@ export function getHtmlPages(dotNextPath: string) { return Object.entries(JSON.parse(manifest)) .filter(([_, value]) => (value as string).endsWith(".html")) .map(([_, value]) => (value as string).replace(/^pages\//, "")) - .reduce((acc, page) => { - acc.add(page); - return acc; - }, new Set()); + .reduce((acc, page) => acc.add(page), new Set()); } export function getBuildId(dotNextPath: string) {