Skip to content

Instantly share code, notes, and snippets.

@pch
Created February 2, 2024 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pch/fe276b29ba037bdaeaa525932478ca18 to your computer and use it in GitHub Desktop.
Save pch/fe276b29ba037bdaeaa525932478ca18 to your computer and use it in GitHub Desktop.
Rails, Turbo, esbuild: TypeError: map.get is not a function

(This applies to Rails 7.1, Turbo 8.0 with esbuild and propshaft)

If you're getting a "TypeError: map.get is not a function" when navigating between pages:

Uncaught (in promise) TypeError: map.get is not a function
    at fetch (application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:6485:20)
    at fetchWithTurboHeaders (application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:1153:10)
    at FetchRequest.perform (application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:1263:25)
fetch @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:6485
fetchWithTurboHeaders @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:1153
perform @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:1263
await in perform (async)
issueRequest @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:2587
visitStarted @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:2839
start @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:2543
startVisit @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:3260
visitProposedToLocation @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:2831
visitProposedToLocation @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:4751
proposeVisit @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:3251
visit @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:4654
followedLinkToLocation @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:4743
clickBubbled @ application-df30cd29ded7227ed5c827c81fa4fc9b51db518a.js:1919

The issue seems to be that, for some reason, Turbo/Stimulus is using a wrong fetch():

https://github.com/hotwired/stimulus/blob/bafb739401f54fed1b70869410979b5acd43f487/src/multimap/set_operations.ts#L10

I did not dig deep into the bundling process to understand what's happening, but the following change fixed it for me:

diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index fddc2f5..9b2bb31 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -6,8 +6,8 @@
   <%= csrf_meta_tags %>
   <%= csp_meta_tag %>
 
-  <%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
+  <%= javascript_include_tag "application", "data-turbo-track": "reload", type: "module" %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment