Skip to content
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

Show Rails error page for a morph reload #56

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion app/assets/javascripts/hotwire_spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -1578,13 +1578,34 @@ var HotwireSpark = (function () {
});
}
#visitCurrentPage() {
this.#hideProgressbar();
return new Promise(resolve => {
document.addEventListener("turbo:load", () => resolve(document), {
document.addEventListener("turbo:load", () => {
resolve(document);
}, {
once: true
});
this.#stopVisitOnError();
window.Turbo.visit(window.location);
});
}
#hideProgressbar() {
const style = document.createElement('style');
style.type = 'text/css';
style.textContent = '.turbo-progress-bar { display: none; }';
document.head.appendChild(style);
return style;
}
#stopVisitOnError() {
document.addEventListener("turbo:before-fetch-response", event => {
const responseStatus = event.detail.fetchResponse.response.status;
if (responseStatus >= 400) {
event.preventDefault();
}
}, {
once: true
});
}
}

consumer.subscriptions.create({
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/hotwire_spark.min.js.map

Large diffs are not rendered by default.

23 changes: 22 additions & 1 deletion app/javascript/hotwire/spark/reloaders/replace_html_reloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,30 @@ export class ReplaceHtmlReloader {
}

#visitCurrentPage() {
this.#hideProgressbar()
return new Promise(resolve => {
document.addEventListener("turbo:load", () => resolve(document), { once: true })
document.addEventListener("turbo:load", () => {
resolve(document)
}, { once: true })
this.#stopVisitOnError()
window.Turbo.visit(window.location)
})
}

#hideProgressbar() {
const style = document.createElement('style');
style.type = 'text/css';
style.textContent = '.turbo-progress-bar { display: none; }';
document.head.appendChild(style);
return style
}

#stopVisitOnError() {
document.addEventListener("turbo:before-fetch-response", (event) => {
const responseStatus = event.detail.fetchResponse.response.status
if(responseStatus >= 400) {
event.preventDefault()
}
}, { once: true })
}
}
2 changes: 1 addition & 1 deletion lib/hotwire/spark/change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def reload_message

def canonical_changed_path
changed_path.to_s.tap do |changed_path|
monitored_paths.each { |path| changed_path.gsub!(/^#{path}/, "")}
monitored_paths.each { |path| changed_path.gsub!(/^#{path}/, "") }
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
config.cache_store = :null_store

# Render exception templates for rescuable exceptions and raise for other exceptions.
config.action_dispatch.show_exceptions = :rescuable
config.action_dispatch.show_exceptions = true

# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
Expand Down
10 changes: 10 additions & 0 deletions test/replace_html_reload_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ class ReplaceHtmlReloadTest < ApplicationSystemTestCase
assert_text "This is pretty amazing"
assert_css "[data-turbo-navigated]"
end

test "errors don't show the error page" do
visit root_path
assert_no_text "ZeroDivisionError"

edit_file "app/views/home/show.html.erb", replace: "_REPLACE_HTML_", with: "<%= 1 / 0 %>"

assert_css "[data-turbo-navigated]"
assert_no_text "ZeroDivisionError"
end
end
Loading