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

Skip dispatching messages if Turbo is not loaded #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

trevorrjohn
Copy link

Spark raises errors on pages where Turbo is not loaded. We now check to see if Turbo is loaded before trying to action on the messages.

My Use Case

I have a Rails app where I am using Turbo on the majority of pages, but specific pages are using React. Spark raises errors in my JS console when I am on those React pages.

Considerations

I wasn't sure if this should be a console.log to warn people or use the internal log function. I went with the internal log so that it wouldn't be noisy for people who know what they are doing. However I could see this as being confusing for first time users who maybe don't have Turbo loaded and don't understand why it isn't working.

Copy link
Contributor

@codergeek121 codergeek121 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a quick look and added some suggestions 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have edited the build output of rollup instead of the original source files. To properly adapt your patch you should add your changes to the originalmonitoring_channel.js async received() method and run npm run build to compile the files. Your patch should contain all changes (the build output and your original changes).

@@ -1600,6 +1600,10 @@ var HotwireSpark = (function () {
document.body.setAttribute("data-hotwire-spark-ready", "");
},
async received(message) {
if (!window.Turbo) {
log(`Turbo not detected, skipping ${message.action}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Turbo is only required if you're using the :replace method. The :morph variant should not require window.Turbo to be available. Could you check if your app is using the :replace or :morph variant?
I think this check should live in the html_replace_reloader.js to avoid skipping messages for all other reloaders.

@@ -1600,6 +1600,10 @@ var HotwireSpark = (function () {
document.body.setAttribute("data-hotwire-spark-ready", "");
},
async received(message) {
if (!window.Turbo) {
log(`Turbo not detected, skipping ${message.action}`);
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would hide errors for apps without Turbo and silently not update the page by default. I think a better approach here would be to always log a descriptive error with an explanation on how to avoid the error, e.g. [hotwire-spark] Tried to replace the page with Turbo, but Turbo is not available on window.Turbo or sth. similar :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants