hotwired / turbo-rails

Use Turbo in your Ruby on Rails app
https://turbo.hotwired.dev
MIT License
2.09k stars 323 forks source link

ActiveStorage uses example.org as host when replaced by a stream #54

Closed jankeesvw closed 3 years ago

jankeesvw commented 3 years ago

While I am porting my application to Turbo, I run into an issue.

This is the code I've got so far:

The .erb template:

<%= turbo_frame_tag dom_id(vehicle) do %>
  <%= turbo_stream_from vehicle %>
  ... some template code ...
  <%= image_tag vehicle.image.variant(resize_to_fill: [424, 220]) %>
  <% end %>
<% end %>

In my model:

  after_update_commit :broadcast_later

  def broadcast_later
    broadcast_replace partial: "customer/dashboard/vehicle"
  end

This all seems to work great, except for one thing, the image I had in the DOM uses the wrong host after it's being replaced. The initial load uses the right URL, but when it's replaced by a broadcast it changes to example.org.

Before After
image image

I also created this initializer:

ActiveSupport::Reloader.to_prepare do
  ApplicationController.renderer.defaults.merge!(
    http_host: Rails.application.config.action_controller.asset_host
  )
end

I hope you have any idea what the solution could be, thanks!

jankeesvw commented 3 years ago

I found a workaround. When I use the url_for helper it gets the right URL. This issue is probably not caused by Turbo so I'm going to close it.

This works:

<%= image_tag url_for(vehicle.image_with_fallback.variant(resize_to_fill: [424, 220])) %>

This yields:

http://stekker.test/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBKQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--cb6db5fb76e9c8fc3e226d7d8eff69917a35d5ee/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lKYW5CbFp3WTZCa1ZVT2hOeVpYTnBlbVZmZEc5ZlptbHNiRnNIYVFLb0FXa0IzQT09IiwiZXhwIjpudWxsLCJwdXIiOiJ2YXJpYXRpb24ifX0=--b0a6e29b924216f0f5dcb59d63681f10c84e9994/ev-database.jpeg

This doesn't work

<%= image_tag vehicle.image_with_fallback.variant(resize_to_fill: [424, 220]) %>

This yields:

http://example.org/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBKQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--cb6db5fb76e9c8fc3e226d7d8eff69917a35d5ee/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lKYW5CbFp3WTZCa1ZVT2hOeVpYTnBlbVZmZEc5ZlptbHNiRnNIYVFLb0FXa0IzQT09IiwiZXhwIjpudWxsLCJwdXIiOiJ2YXJpYXRpb24ifX0=--b0a6e29b924216f0f5dcb59d63681f10c84e9994/ev-database.jpeg

unRARed commented 3 years ago

@jankeesvw I can confirm the workaround for direct attachments, but the problem is compounded with the use of ActionText. You don't have control over building the URL in that scenario... or at least not in any obvious way that I could find. Did you determine the culprit if not Turbo?

jankeesvw commented 3 years ago

No, sorry. I'm just using url_for right now.

brendon commented 2 years ago

I suspect this is caused when an asset_host is defined. In that case an image_tag would link to an absolute url with the asset host by default. When an attachment is passed instead it seems to want to still build an absolute url and looks for that at config.action_controller.default_url_options or uses example.com if that's not set. Ideally because it's a local route it should be a relative url. Perhaps this is a bug?

fa11enangel commented 1 year ago

Setting config.action_controller.default_url_options may have side effects to your project, which you don't expect. url_for is a solution for me too without changing global configuration, which change routing behaviour in some unexpected way for me.

As all related issues and pull-requests were closed due to inactivity, there is no good solution for this issue except these two workarounds.

I've written a blog post regarding Default URL Options to better understand the different settings, which might be the reason tool.

The analysis of my problems with ActiveStorage are documented here: Define Default URL for ActiveStorage to fix Mixed Content Error

hdaniel commented 1 year ago

Confirming this is still an issue that can cause active storage to redirect to example.org.

wwvuillemot commented 7 months ago

Same; having this issue with ActsAsTenant, ActiveJob, ActiveStorage ....

fa11enangel commented 7 months ago

Same; having this issue with ActsAsTenant, ActiveJob, ActiveStorage ....

Try to check the different settings you have in your project. If you set the wrong configuration/param, they will have side effects on other parts of your application like ActiveJob, ActiveStorage, etc. Check my blog post, where the setup for ActiveStorage etc is explained: Default URL Options.

I had the same problems with ActiveStorage and tried to explain it here: Define Default URL for ActiveStorage to fix Mixed Content Error