Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide correct MIME type for dynamic endpoint routes in dev #4356

Merged
merged 4 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>`
};
}