Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions packages/open-next/src/build/compileTagCacheProvider.ts
Original file line number Diff line number Diff line change
@@ -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,
);
}
58 changes: 18 additions & 40 deletions packages/open-next/src/build/createAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 Wether 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(
Expand All @@ -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,
{
Expand All @@ -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],
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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);

Expand All @@ -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 };
}
5 changes: 1 addition & 4 deletions packages/open-next/src/build/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>());
.reduce((acc, page) => acc.add(page), new Set<string>());
}

export function getBuildId(dotNextPath: string) {
Expand Down
Loading