Skip to content
Merged
Changes from all commits
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
53 changes: 34 additions & 19 deletions src/mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,26 +531,41 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
const outdir = path.join(root, 'public', 'mdx-images');
await mkdir(outdir, {recursive: true});

// If the file contains content that depends on the Release Registry (such as an SDK's latest version), avoid using the cache for that file, i.e. always rebuild it.
// This is because the content from the registry might have changed since the last time the file was cached.
// If a new component that injects content from the registry is introduced, it should be added to the patterns below.
const skipCache =
source.includes('@inject') ||
source.includes('<PlatformSDKPackageName') ||
source.includes('<LambdaLayerDetail');

if (process.env.CI) {
cacheKey = md5(source);
cacheFile = path.join(CACHE_DIR, `${cacheKey}.br`);
assetsCacheDir = path.join(CACHE_DIR, cacheKey);
if (skipCache) {
// eslint-disable-next-line no-console
console.info(
`Not using cached version of ${sourcePath}, as its content depends on the Release Registry`
);
} else {
cacheKey = md5(source);
cacheFile = path.join(CACHE_DIR, `${cacheKey}.br`);
assetsCacheDir = path.join(CACHE_DIR, cacheKey);

try {
const [cached, _] = await Promise.all([
readCacheFile<SlugFile>(cacheFile),
cp(assetsCacheDir, outdir, {recursive: true}),
]);
return cached;
} catch (err) {
if (
err.code !== 'ENOENT' &&
err.code !== 'ABORT_ERR' &&
err.code !== 'Z_BUF_ERROR'
) {
// If cache is corrupted, ignore and proceed
// eslint-disable-next-line no-console
console.warn(`Failed to read MDX cache: ${cacheFile}`, err);
try {
const [cached, _] = await Promise.all([
readCacheFile<SlugFile>(cacheFile),
cp(assetsCacheDir, outdir, {recursive: true}),
]);
return cached;
} catch (err) {
if (
err.code !== 'ENOENT' &&
err.code !== 'ABORT_ERR' &&
err.code !== 'Z_BUF_ERROR'
) {
// If cache is corrupted, ignore and proceed
// eslint-disable-next-line no-console
console.warn(`Failed to read MDX cache: ${cacheFile}`, err);
}
}
}
}
Expand Down Expand Up @@ -685,7 +700,7 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
},
};

if (assetsCacheDir && cacheFile) {
if (assetsCacheDir && cacheFile && !skipCache) {
await cp(assetsCacheDir, outdir, {recursive: true});
writeCacheFile(cacheFile, JSON.stringify(resultObj)).catch(e => {
// eslint-disable-next-line no-console
Expand Down
Loading