-
Notifications
You must be signed in to change notification settings - Fork 336
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
JS error thrown when Turbo-rails initialized twice on 4xx/5xx #249
Comments
Digging into this a little bit it looks like hotwired/turbo#483 is on to something. In addition to those conditionals,
if (customElements.get("turbo-cable-stream-source") === undefined) {
customElements.define("turbo-cable-stream-source", TurboCableStreamSourceElement)
} With all of these conditionals in place no JS errors are encountered, however I'm not yet familiar enough with the libraries to know what the downsides are with this path. Perhaps it's better to be made aware of when this case is encountered as it's a code smell, but perhaps it's not avoidable in some circumstances. |
@seanabrahams Seems like it would be better to change things in Turbo so that error responses don't blindly replace the head & body, instead doing the same snapshot comparisons to decide which new script elements need adding. Seems like ErrorRenderer ignores turbo-visit-control=reload, too, which makes this quite tricky to fix cleanly. It does seem to behave ok if I naively hack the source - diff --git a/src/core/drive/visit.ts b/src/core/drive/visit.ts
--- src/core/drive/visit.ts
+++ src/core/drive/visit.ts
@@ -262,9 +262,9 @@
this.performScroll()
this.adapter.visitRendered(this)
this.complete()
} else {
- await this.view.renderError(PageSnapshot.fromHTMLString(responseHTML), this)
+ await this.view.renderPage(PageSnapshot.fromHTMLString(responseHTML), false, this.willRender, this)
this.adapter.visitRendered(this)
this.fail()
}
}) but I can see that it might need more thought ... maybe error responses need to opt into this behaviour (eg in header or metatags), so that unsuspecting pages still get a full page replacement (eg for gateway errors from a service that knows nothing of turbo) |
Hi there! Any news? |
In the meantime, for those who don't know (like me, until then) you can use : https://yarnpkg.com/cli/patch yarn patch @hotwired/turbo
subl /private/var/folders/90/31t09ry509sdf_8vy_psjm5w0000gn/T/xfs-35500cf6/user
yarn patch-commit -s /private/var/folders/90/31t09ry509sdf_8vy_psjm5w0000gn/T/xfs-35500cf6/user
yarn install Be sure to patch the right file ( |
We're seeing the following error
from these lines:
turbo-rails/app/assets/javascripts/turbo.js
Lines 3229 to 3233 in f3293d2
Related to hotwired/turbo#188, this is happening because using a custom error handler using the method here: https://web-crunch.com/posts/custom-error-page-ruby-on-rails, basically:
config.exceptions_app = self.routes
ErrorsController
ErrorsController
renders a view which uses our normal application layout, which includes the Turbo initializationturbo-rails
is imported before Turbo, so while Turbo looks to protect itself from an attempt to start twice,turbo-rails
does not because that code gets executed first.It's not clear the best way to handle this case if you want to reuse your page layout rather than having a hardcoded 4xx/5xx page. Turbo uses the ErrorRenderer for 4xx/5xx responses, which replaces both the body and head, rather than just the body that PageRenderer uses, which leads to the head being replaced and reinitialized rather than just the body.
The text was updated successfully, but these errors were encountered: