Fix asset loading in dev mode (#6466)

* Fix asset loading in dev mode

* Proper windows support
This commit is contained in:
Matthew Phillips 2023-03-08 17:32:39 -05:00 committed by GitHub
parent 401b97ad0e
commit ec04553525
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
In dev, load assets relative to the root

View file

@ -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);

View file

@ -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');