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

docs(endpoints): added new example for returning buffers from endpoints #3154

Merged
merged 6 commits into from
Jun 4, 2023
Merged
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/content/docs/en/core-concepts/endpoints.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ export async function get({ params, request }) {
}
```

This method of returning a buffer as a response might not work for certain providers like vercel because the `Content-Type` header cannot be specified. For returning images prefer the following way (i.e. return a `Response`-Object):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This method of returning a buffer as a response might not work for certain providers like vercel because the `Content-Type` header cannot be specified. For returning images prefer the following way (i.e. return a `Response`-Object):
Certain providers require the `Content-Type` header to return an image. In this case, return a `Response` object instead which allows you to specify a `headers` property.
For example, to produce a `.png` image on Vercel:


```ts title="src/pages/astro-logo.png.ts"
export async function get({ params, request }) {
const response = await fetch("https://docs.astro.build/assets/full-logo-light.png");
const buffer = Buffer.from(await response.arrayBuffer());
return new Response(buffer, {
headers: { "Content-Type": "image/png" },
});
}
```

You can also type your endpoint functions using the `APIRoute` type:

```ts
Expand Down