Skip to content

Commit 08d37b4

Browse files
committed
fix: Improve handling of Vercel and AWS Lambda environments in getFileBySlug function
- Enhanced the logic to detect Vercel and AWS Lambda environments, ensuring proper usage of the /tmp directory to avoid read-only file system errors. - Updated comments for clarity regarding the deployment scenarios and the handling of output directories.
1 parent 8329d6e commit 08d37b4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/mdx.ts

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

534-
// During Vercel deployment, use /tmp to avoid read-only file system errors
534+
// During Vercel deployment (which uses AWS Lambda), use /tmp to avoid read-only file system errors
535535
// The actual images are already built and in public/mdx-images from the build step
536-
const isVercelRuntime = process.env.VERCEL || process.env.VERCEL_ENV;
536+
// Check for both Vercel and Lambda environment indicators
537+
const isVercelRuntime =
538+
process.env.VERCEL ||
539+
process.env.VERCEL_ENV ||
540+
process.env.AWS_LAMBDA_FUNCTION_NAME ||
541+
process.env.LAMBDA_TASK_ROOT;
537542
const outdir = isVercelRuntime
538543
? path.join('/tmp', 'mdx-images-' + md5(sourcePath).slice(0, 8))
539544
: path.join(root, 'public', 'mdx-images');
@@ -690,7 +695,8 @@ export async function getFileBySlug(slug: string): Promise<SlugFile> {
690695
// enabled is because mdx-images is a dumping ground
691696
// for all images, so we cannot filter it out only
692697
// for this specific slug easily
693-
options.outdir = assetsCacheDir || outdir;
698+
// On Vercel runtime, always use /tmp (writable) instead of cache or public dirs
699+
options.outdir = isVercelRuntime ? outdir : assetsCacheDir || outdir;
694700

695701
// Set write to true so that esbuild will output the files.
696702
options.write = true;

0 commit comments

Comments
 (0)