Skip to content

[RF-DOCS] Update the JavaScript in Rails guide - #58165

Open
ayushn21 wants to merge 1 commit into
rails:mainfrom
ayushn21:ayush/javascript-on-rails
Open

[RF-DOCS] Update the JavaScript in Rails guide#58165
ayushn21 wants to merge 1 commit into
rails:mainfrom
ayushn21:ayush/javascript-on-rails

Conversation

@ayushn21

Copy link
Copy Markdown
Contributor

Motivation / Background

Update the JavaScript in Rails guide.

Detail

A summary of the key changes:

  • Improve the narrative flow regarding import-map and JavaScript bundlers.
  • Simplify and compress the section comparing the usage of a bundler to an import map.
  • Add a detailed section on Hotwire and information regarding the integration with Rails using turbo-rails and stimulus-rails.
  • Add a section about request.js.

Additional information

Internal Audit

The heading 'Adding npm Packages to Javascript bundlers' -> could change this to 'Using Javascript Bundlers' to make it a bit more general.

"The version of your Node.js runtime should be printed out. Make sure it’s greater than 8.16.0." -> Node.js 8 reached end of life a while ago it looks like - maybe we should bump this a bit to 'Make sure it's greater than 18'?

Section 4.1 -> maybe this Turbo Drive part could be put into a NOTE block somewhere else rather than being its own section?

Section 5.1 -> I think the heading 'method' is potentially too vague. What if we changed this to 'Overriding HTTP Methods'?

We could tidy up the AJAX request example - the function call has a space before the () which we can remove and I think it's worth potentially showing that after the
const body = await response.text
line the function would continue to go on further, e.g. "// do something with body" or just "...."

Some extra content that we could include in this guide:

The first three sections: "Import maps", "Adding npm Packages with JavaScript Bundlers" and "Choosing Between Import Maps and a JavaScript Bundler" are all about adding JavaScript to an application. Maybe combine these into a single "Adding npm Packages" section. Then it's also clear to skip this section if you already have JavaScript packages installed in your application.

Turbo

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.

@rocket-turtle

Copy link
Copy Markdown

Thank you for putting this together. I think the Stimulus section is a good addition.

Some small things I noticed:

Not sure if this belongs here, but should the turbo section mention that Inline scripts in turbo loaded content run even when CSP should block them - see hotwired/turbo-rails#757

So before you integrate turbo you should know that CSP won't protect you the way you'd expect, and right now there is nothing in the docs that tells you that.

@ayushn21

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback @rocket-turtle, nice catches. Fixed everything.

So before you integrate turbo you should know that CSP won't protect you the way you'd expect, and right now there is nothing in the docs that tells you that.

Did a quick check of your repo on Ruby 4.0.2 and I couldn't reproduce the issue. I get a message on both Safari and Firefox that a script was blocked from running by the CSP. Haven't got the time to look deeper so I didn't comment on the issue, but for now I'll leave it out of the guide. Worth revisiting if we get more info from the Hotwire team.

@rocket-turtle

Copy link
Copy Markdown

@ayushn21 I rechecked the issue with rails 8.1 and the latest turbo version and its still there.

I added a video in the issue. Maybe you just opend the page and did not click the link. On first page load the CSP is working because the page is not loaded via turbo.

Noone from Hotwire Teams respondet (issue is from 2025) so I think there could be a hint in the documentation that users know the issue and take care them self.

@ayushn21
ayushn21 force-pushed the ayush/javascript-on-rails branch from 98bb14b to c0c976f Compare August 6, 2026 09:39
@ayushn21

ayushn21 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

@rocket-turtle I can reproduce it now, not sure why it didn't happen earlier ... I didn't do anything different.

Anyway, I'm not sure this issue fits in with this guide, it's really more of a bug report so hopefully the Hotwire team will address it soon. That said, a Hotwire with Rails guide is in the works and a lot of material from this guide will be moved to that, so I'll might mention it in that guide if I feel it fits.

Thanks!

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

Labels

docs rails foundation Rails Foundation PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants