Closed nathanpalmer closed 5 years ago
Thanks for the details in the report.
Yeah, have played around with this a lot before.
It has to do with how API Gateway and binary content work together. API Gateway, will only serve the image correctly if certain Accept
headers are sent.
Does not work:
curl URL -H 'Accept: image/webp,image/*, **/** ;q=0.8' https://api-gateway-dns/image.png # responds with base64 text while
Works:
curl URL -H 'Accept: image/webp' https://api-gateway-dns/image.png # responds with image correclty
This is important because with web browsers, we don't control the Accept header when the URL is being hit directly. IE: An HTML img
tag. So they don't get served correctly.
This is not great behavior of API Gateway.
Some discussion is also in here with links to this known issue: https://community.rubyonjets.com/t/no-send-data-to-send-files-as-response/144/2
It's not ideal, but the recommendation is to upload assets to s3 and serve from there.
This is also a reason why files in the public
folder are automatically uploaded to s3 and jets serves them out of s3 as part of the jets deploy
process: Jets Assets Serving
Looks like this app generates dynamic images and they are not meant to be kept around. If you end up taking the s3 approach, you may want to use an S3 Lifecycle Policy to automatically remove the images in the bucket.
Note, just had an idea. It'll be nice if there was some logic, maybe middleware, that throws an error or puts out warnings telling the user that it won't work locally before deploying. Though, unsure if it's worth it because API Gateway might provide a solution eventually.
Got it! Thank you for the very detailed response. We're not able to send custom headers for this project unfortunately. But good to know where the actual issue lies.
Expected Behaviour
I'm expecting to be able to render binary content dynamically and serve it as a file.
Current Behavior
Locally this works just fine. I get this
However, once deployed instead of returning binary data it's returning base64 encoded data.
The differences are a little more clear when I run a console command
Local
Amazon
Step-by-step reproduction instructions
Then add this code to the controller
and update the
routes.rb
Solution Suggestion
I'm not familiar enough with the differences between the deployed environment to know what might be going wrong here.