From 11f45b9a3216f60317e1c54bb3e6c4e9e0add342 Mon Sep 17 00:00:00 2001 From: Rishi Raj Jain Date: Tue, 17 Oct 2023 15:42:01 +0530 Subject: [PATCH] fix: _image endpoint returning a 404 for local images (#8828) --- .changeset/heavy-lies-cover.md | 5 +++++ packages/astro/src/assets/endpoint/node.ts | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .changeset/heavy-lies-cover.md diff --git a/.changeset/heavy-lies-cover.md b/.changeset/heavy-lies-cover.md new file mode 100644 index 000000000000..589c10e26d06 --- /dev/null +++ b/.changeset/heavy-lies-cover.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +fix file system path references diff --git a/packages/astro/src/assets/endpoint/node.ts b/packages/astro/src/assets/endpoint/node.ts index 1e96162648ab..7542b5eafd45 100644 --- a/packages/astro/src/assets/endpoint/node.ts +++ b/packages/astro/src/assets/endpoint/node.ts @@ -1,15 +1,20 @@ import { isRemotePath, removeQueryString } from '@astrojs/internal-helpers/path'; import { readFile } from 'fs/promises'; import mime from 'mime/lite.js'; +import os from 'os'; import type { APIRoute } from '../../@types/astro.js'; import { getConfiguredImageService, isRemoteAllowed } from '../internal.js'; import { etag } from '../utils/etag.js'; // @ts-expect-error import { assetsDir, imageConfig } from 'astro:assets'; +function replaceFileSystemReferences(src: string) { + return os.platform().includes('win32') ? src.replace(/^\/@fs\//, '') : src.replace(/^\/@fs/, ''); +} + async function loadLocalImage(src: string, url: URL) { const filePath = import.meta.env.DEV - ? removeQueryString(src.slice('/@fs'.length)) + ? removeQueryString(replaceFileSystemReferences(src)) : new URL('.' + src, assetsDir); let buffer: Buffer | undefined = undefined;