Skip to content

Commit 951b69d

Browse files
committed
refactor: Simplify output directory handling in getFileBySlug function
- Updated the logic to always use the public/mdx-images directory during build, removing the conditional checks for Vercel and AWS Lambda environments. - Improved comments for clarity regarding directory creation and asset copying, ensuring that images are assumed to exist from build time. - Streamlined cache checking logic by removing unnecessary runtime checks, enhancing maintainability.
1 parent 8ccec69 commit 951b69d

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

src/mdx.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -530,22 +530,14 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
530530
let cacheFile: string | null = null;
531531
let assetsCacheDir: string | null = null;
532532

533-
// During Vercel deployment (which uses AWS Lambda), use /tmp to avoid read-only file system errors
534-
// The actual images are already built and in public/mdx-images from the build step
535-
// Check for both Vercel and Lambda environment indicators
536-
const isVercelRuntime =
537-
process.env.VERCEL ||
538-
process.env.VERCEL_ENV ||
539-
process.env.AWS_LAMBDA_FUNCTION_NAME ||
540-
process.env.LAMBDA_TASK_ROOT;
541-
const outdir = isVercelRuntime
542-
? path.join('/tmp', 'mdx-images-' + md5(sourcePath).slice(0, 8))
543-
: path.join(root, 'public', 'mdx-images');
533+
// Always use public/mdx-images during build
534+
// During runtime (Lambda), this directory is read-only but images are already there from build
535+
const outdir = path.join(root, 'public', 'mdx-images');
544536

545537
try {
546538
await mkdir(outdir, {recursive: true});
547539
} catch (e) {
548-
// If we can't create the directory (e.g., read-only filesystem during static generation),
540+
// If we can't create the directory (e.g., read-only filesystem),
549541
// continue anyway - images should already exist from build time
550542
}
551543

@@ -557,8 +549,8 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
557549
source.includes('<PlatformSDKPackageName') ||
558550
source.includes('<LambdaLayerDetail');
559551

560-
// Check cache in CI or Vercel environments
561-
if (process.env.CI || isVercelRuntime) {
552+
// Check cache in CI environments
553+
if (process.env.CI) {
562554
if (skipCache) {
563555
// eslint-disable-next-line no-console
564556
console.info(
@@ -572,10 +564,7 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
572564
try {
573565
const [cached, _] = await Promise.all([
574566
readCacheFile<SlugFile>(cacheFile),
575-
// Only try to copy if the target is writable
576-
isVercelRuntime
577-
? Promise.resolve() // Skip copying on Vercel runtime - images already in public/
578-
: cp(assetsCacheDir, outdir, {recursive: true}),
567+
cp(assetsCacheDir, outdir, {recursive: true}),
579568
]);
580569
return cached;
581570
} catch (err) {
@@ -692,8 +681,7 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
692681
// enabled is because mdx-images is a dumping ground
693682
// for all images, so we cannot filter it out only
694683
// for this specific slug easily
695-
// On Vercel runtime, always use /tmp (writable) instead of cache or public dirs
696-
options.outdir = isVercelRuntime ? outdir : assetsCacheDir || outdir;
684+
options.outdir = assetsCacheDir || outdir;
697685

698686
// Set write to true so that esbuild will output the files.
699687
options.write = true;
@@ -724,14 +712,11 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
724712
};
725713

726714
if (assetsCacheDir && cacheFile && !skipCache) {
727-
// On Vercel runtime, skip copying since we're using /tmp which is ephemeral
728-
if (!isVercelRuntime) {
729-
try {
730-
await cp(assetsCacheDir, outdir, {recursive: true});
731-
} catch (e) {
732-
// eslint-disable-next-line no-console
733-
console.warn(`Failed to copy assets from cache to output dir:`, e);
734-
}
715+
try {
716+
await cp(assetsCacheDir, outdir, {recursive: true});
717+
} catch (e) {
718+
// If copy fails (e.g., on read-only filesystem), continue anyway
719+
// Images should already exist from build time
735720
}
736721
writeCacheFile(cacheFile, JSON.stringify(resultObj)).catch(e => {
737722
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)