Skip to content

Commit

Permalink
Provide correct MIME type for dynamic endpoint routes in dev (#4356)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis authored Aug 17, 2022
1 parent 500332a commit beed20b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/six-jars-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Provide correct MIME type for dynamic endpoint routes in dev
6 changes: 5 additions & 1 deletion packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@ async function handleRequest(
await writeWebResponse(res, result.response);
} else {
let contentType = 'text/plain';
const computedMimeType = route.pathname ? mime.getType(route.pathname) : null;
// Dynamic routes don’t include `route.pathname`, so synthesise a path for these (e.g. 'src/pages/[slug].svg')
const filepath =
route.pathname ||
route.segments.map((segment) => segment.map((p) => p.content).join('')).join('/');
const computedMimeType = mime.getType(filepath);
if (computedMimeType) {
contentType = computedMimeType;
}
Expand Down
20 changes: 20 additions & 0 deletions packages/astro/test/dev-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,26 @@ describe('Development Routing', () => {
expect(body.slug).to.equal('thing4');
expect(body.title).to.equal('data [slug]');
});

it('correct MIME type when loading /home.json (static route)', async () => {
const response = await fixture.fetch('/home.json');
expect(response.headers.get('content-type')).to.match(/application\/json/);
});

it('correct MIME type when loading /thing1.json (dynamic route)', async () => {
const response = await fixture.fetch('/thing1.json');
expect(response.headers.get('content-type')).to.match(/application\/json/);
});

it('correct MIME type when loading /images/static.svg (static image)', async () => {
const response = await fixture.fetch('/images/static.svg');
expect(response.headers.get('content-type')).to.match(/image\/svg\+xml/);
});

it('correct MIME type when loading /images/1.svg (dynamic image)', async () => {
const response = await fixture.fetch('/images/1.svg');
expect(response.headers.get('content-type')).to.match(/image\/svg\+xml/);
});
});

describe('file format routing', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export async function getStaticPaths() {
return [
{ params: { image: 1 } },
{ params: { image: 2 } },
];
}

export async function get({ params }) {
return {
body: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 200">
<title>${params.image}</title>
</svg>`
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export async function get() {
return {
body: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 200">
<title>Static SVG</title>
</svg>`
};
}

0 comments on commit beed20b

Please sign in to comment.