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

Returning png with hex or base64 in APIRoute doesn't work in dev #5660

Closed
1 task
fflaten opened this issue Dec 21, 2022 · 3 comments · Fixed by #6163
Closed
1 task

Returning png with hex or base64 in APIRoute doesn't work in dev #5660

fflaten opened this issue Dec 21, 2022 · 3 comments · Fixed by #6163

Comments

@fflaten
Copy link
Contributor

fflaten commented Dec 21, 2022

What version of astro are you using?

1.7.2

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

npm

What operating system are you using?

Linux

Describe the Bug

Returning a png-image using base64 or hex encoding works in build, but not in dev. In dev the client gets base64/hex-string directly, which obviously doesn't render in the browser.

Code in repro is similar to https://docs.astro.build/en/core-concepts/endpoints/#static-file-endpoints but the example uses binary-encoding which seems to be deprecated in Node, so I'm trying to replace it.

Related withastro/docs#2202

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-dcmdb9

Participation

  • I am willing to submit a pull request for this issue.
@haasal
Copy link

haasal commented Jan 19, 2023

Any news on this? encoding: "binary" doesn't work anymore on vercel and netlify (ssr). I would like to help fixing this.
I opened an issue on this:

@haasal
Copy link

haasal commented Jan 19, 2023

Wait, is the encoding even checked by the dev server? I haven't seen anything that would suggest that the encoding matters at all in Resonse.ts

@HiDeoo
Copy link
Member

HiDeoo commented Feb 6, 2023

I've just hit this issue too. It looks like a workaround might be to convert back the body to a buffer in the vite-plugin-astro-server when handling the route with a hex or base64 encoding:

+ const response = new Response(result.body, {
+   status: 200,
+   headers: {
+     'Content-Type': `${contentType};charset=utf-8`,
+   },
+ });
- const response = new Response(
-   result.encoding === 'hex' || result.encoding === 'base64'
-     ? Buffer.from(result.body, result.encoding)
-     : result.body,
-   {
-     status: 200,
-     headers: {
-       'Content-Type': `${contentType};charset=utf-8`,
-     },
-   }
- );

I have yet to find the time to investigate more and see if there is a better fix and also to find a proper way to add tests for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants