Skip to content

Commit

Permalink
Fix asset loading in dev mode (#6466)
Browse files Browse the repository at this point in the history
* Fix asset loading in dev mode

* Proper windows support
  • Loading branch information
matthewp committed Mar 8, 2023
1 parent 401b97a commit ec04553
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-candles-retire.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

In dev, load assets relative to the root
6 changes: 3 additions & 3 deletions packages/astro/src/assets/vite-plugin-assets.ts
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/test/core-image.test.js
Expand Up @@ -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');
Expand Down

0 comments on commit ec04553

Please sign in to comment.