hotwired / turbo

The speed of a single-page web application without having to write any JavaScript
https://turbo.hotwired.dev
MIT License
6.54k stars 415 forks source link

Server-side redirect from turbo stream to file download fails in Turbo 8 #1253

Open asilano opened 2 months ago

asilano commented 2 months ago

Steps to reproduce

Have HTML containing:

<a href="/maybe_a_file" data-turbo-stream="true">Download</a>

Backed by server code:

def maybe_a_file
  if error?
    # File doesn't exist - render a turbo-stream to display an error message
    return render "error_message"
  end

  redirect_to "/actual_file.txt"
end

Click the link (when error? returns false)

Expected behaviour

The redirect is followed, and the browser downloads and saves actual_file.txt

Actual behaviour

The redirect is followed, the browser requests actual_file.txt, and the server responds 200 OK with the contents of the file; but the file is not saved by the browser

Cause

Via git bisect, determined to be caused by #968 (thanks to @pythonandchips for digging)

Workaround

Instead of server-side redirecting to the file, return a turbo-stream action to visit the actual file URL in JS (both Turbo.visit and window.location.assign work fine)