-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add support for custom non-html route encoding #4549
Conversation
🦋 Changeset detectedLatest commit: 0e49bf2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 14 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Thanks! Change looks good overall. Instead of having an example which I don't think people would use, could you move that code into a test at packages/astro/test/fixtures/? |
Code updated:
|
@altano thank you! Everything looks good, this will be merged when we are ready to do our next minor release. |
Changes
See #4545 for further discussion of the issue, but the summary is that currently non-html routes have their buffers encoded as
utf-8
when being written to disk during an SSG build, which works for some streams but not others. The main use case that isn't working is a PNG file route. The binary stream gets mangled by encoding asutf-8
, destroying the PNG image. By allowing abinary
encoding, the PNG image is written to disk correctly instead.NOTE: By causing the API surface of endpoints (which return
Response
) and ssg routes (which return this custom object) we're making the life of whoever tackles withastro/docs#760 a little harder.Testing
I have added a
placeholder.png.ts
route to theexamples/non-html-routes
example project. This route was previously broken (since there was no way to specify abinary
encoding) but now works perfectly. Just runpnpm build
in this example dir and observe that thedist/placeholder.png
file is valid and can be previewed.Docs
I've submitted a PR to update the docs: withastro/docs#1434
Alternative
Let the
body
bestring | Buffer
instead ofstring
. When writing aBuffer
,fs.writeFile
ignores the encoding option.Why I chose current implementation instead: the ergonomics of allowing both
string
andBuffer
might be easier on the user, but we shouldn't force people to create aBuffer
(if they don't already have one) to override the encoding.