UNPKG

@aziontech/opennextjs-azion

Version:
77 lines (76 loc) 3.24 kB
/** * This code was originally copied and modified from the @opennextjs/cloudflare repository. * Significant changes have been made to adapt it for use with Azion. */ import { cpSync, existsSync, rmSync } from "node:fs"; import path from "node:path"; import logger from "@opennextjs/aws/logger.js"; import { globSync } from "glob"; import { CACHE_DIR as STATIC_ASSETS_CACHE_DIR, NAME as STATIC_ASSETS_CACHE_NAME, } from "../../core/overrides/incremental-cache/storage-incremental-cache.js"; async function resolveCacheName(value) { return typeof value === "function" ? (await value()).name : value; } export function getCacheAssets(opts) { const allFiles = globSync(path.join(opts.outputDir, "cache/**/*"), { withFileTypes: true, windowsPathsNoEscape: true, }).filter((f) => f.isFile()); const assets = []; for (const file of allFiles) { const fullPath = file.fullpathPosix(); const relativePath = path.relative(path.join(opts.outputDir, "cache"), fullPath); if (relativePath.startsWith("__fetch")) { const [__fetch, buildId, ...keyParts] = relativePath.split("/"); if (__fetch !== "__fetch" || buildId === undefined || keyParts.length === 0) { throw new Error(`Invalid path for a Cache Asset file: ${relativePath}`); } assets.push({ isFetch: true, fullPath, key: `/${keyParts.join("/")}`, buildId, }); } else { const [buildId, ...keyParts] = relativePath.slice(0, -".cache".length).split("/"); if (!relativePath.endsWith(".cache") || buildId === undefined || keyParts.length === 0) { throw new Error(`Invalid path for a Cache Asset file: ${relativePath}`); } assets.push({ isFetch: false, fullPath, key: `/${keyParts.join("/")}`, buildId, }); } } return assets; } function populateStaticAssetsIncrementalCache(options, cacheDir) { logger.info("\nPopulating cache..."); const storageCacheDir = path.join(cacheDir, STATIC_ASSETS_CACHE_DIR); if (existsSync(storageCacheDir)) { rmSync(storageCacheDir, { recursive: true, force: true }); } cpSync(path.join(options.outputDir, "cache"), path.join(cacheDir, STATIC_ASSETS_CACHE_DIR), { recursive: true, }); logger.info(`Successfully populated cache`); } export async function populateCache(options, config, populateCacheOptions) { const { incrementalCache } = config.default.override ?? {}; if (!existsSync(options.outputDir)) { logger.error("Unable to populate cache: Open Next build not found"); process.exit(1); } if (!config.dangerous?.disableIncrementalCache && incrementalCache) { const name = await resolveCacheName(incrementalCache); switch (name) { case STATIC_ASSETS_CACHE_NAME: populateStaticAssetsIncrementalCache(options, populateCacheOptions.cacheDir); break; default: logger.info("Incremental cache does not need populating"); } } }