From ec0455352568ab3ea3c5ec1625f582aa54d15bb7 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 8 Mar 2023 17:32:39 -0500 Subject: [PATCH] Fix asset loading in dev mode (#6466) * Fix asset loading in dev mode * Proper windows support --- .changeset/red-candles-retire.md | 5 +++++ packages/astro/src/assets/vite-plugin-assets.ts | 6 +++--- packages/astro/test/core-image.test.js | 7 +++++++ 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 .changeset/red-candles-retire.md diff --git a/.changeset/red-candles-retire.md b/.changeset/red-candles-retire.md new file mode 100644 index 000000000..6614639f0 --- /dev/null +++ b/.changeset/red-candles-retire.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +In dev, load assets relative to the root diff --git a/packages/astro/src/assets/vite-plugin-assets.ts b/packages/astro/src/assets/vite-plugin-assets.ts index 020ac6758..9f8cbd53e 100644 --- a/packages/astro/src/assets/vite-plugin-assets.ts +++ b/packages/astro/src/assets/vite-plugin-assets.ts @@ -78,8 +78,8 @@ export default function assets({ return next(); } - const filePathURL = new URL(filePath, 'file:'); - const file = await fs.readFile(filePathURL.pathname); + const filePathURL = new URL('.' + filePath, settings.config.root); + const file = await fs.readFile(filePathURL); // Get the file's metadata from the URL let meta = getOrigQueryParams(filePathURL.searchParams); @@ -109,7 +109,7 @@ export default function assets({ format = result.format; } - res.setHeader('Content-Type', mime.getType(fileURLToPath(url)) || `image/${format}`); + res.setHeader('Content-Type', mime.getType(fileURLToPath(filePathURL)) || `image/${format}`); res.setHeader('Cache-Control', 'max-age=360000'); const stream = Readable.from(data); diff --git a/packages/astro/test/core-image.test.js b/packages/astro/test/core-image.test.js index d6b6642c3..e8fd1f423 100644 --- a/packages/astro/test/core-image.test.js +++ b/packages/astro/test/core-image.test.js @@ -72,6 +72,13 @@ describe('astro:image', () => { expect($img.attr('alt')).to.equal('a penguin'); }); + it('middleware loads the file', async() => { + let $img = $('#local img'); + let src = $img.attr('src'); + let res = await fixture.fetch(src); + expect(res.status).to.equal(200); + }); + it('errors on unsupported formats', async () => { logs.length = 0; let res = await fixture.fetch('/unsupported-format');