Add support for custom non-html route encoding#4549
Merged
matthewp merged 3 commits intowithastro:mainfrom Sep 9, 2022
altano:alan/non-html-route-encoding
Merged
Add support for custom non-html route encoding#4549matthewp merged 3 commits intowithastro:mainfrom altano:alan/non-html-route-encoding
matthewp merged 3 commits intowithastro:mainfrom
altano:alan/non-html-route-encoding
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 |
matthewp
reviewed
Aug 30, 2022
Contributor
|
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/? |
Contributor
Author
|
Code updated:
|
Contributor
|
@altano thank you! Everything looks good, this will be merged when we are ready to do our next minor release. |
natemoo-re
approved these changes
Sep 7, 2022
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
See #4545 for further discussion of the issue, but the summary is that currently non-html routes have their buffers encoded as
utf-8when 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 abinaryencoding, 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.tsroute to theexamples/non-html-routesexample project. This route was previously broken (since there was no way to specify abinaryencoding) but now works perfectly. Just runpnpm buildin this example dir and observe that thedist/placeholder.pngfile is valid and can be previewed.Docs
I've submitted a PR to update the docs: withastro/docs#1434
Alternative
Let the
bodybestring | Bufferinstead ofstring. When writing aBuffer,fs.writeFileignores the encoding option.Why I chose current implementation instead: the ergonomics of allowing both
stringandBuffermight 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.