0

I'm trying to upgrade my Rails app (v6.1.7.4) from Turbolinks to Turbo. I use sprockets (4.2.0).

I've gone through a path of including importmap.json to my config folder and trying to utilise my already existing asset pipeline with "app/assets/javascripts".

importmap.json

{
    "imports": {
        "application": "/assets/application.js",
        "@hotwired/turbo-rails": "/assets/turbo.js"
    }
}

turbo.js

//= require turbo

application.html.erb contains

...
<%= javascript_importmap_tags %>
<%= javascript_include_tag 'turbo', 'data-turbo-track': true %>
<%= javascript_include_tag 'application', 'data-turbo-track': true %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbo-track': true %>
...

controllers > concerns > turbo > redirection.rb

module Turbo
  module Redirection
    extend ActiveSupport::Concern

    def redirect_to(url = {}, options = {})
      turbo = options.delete(:turbo)

      super.tap do
        if turbo != false && request.xhr? && !request.get?
          visit_location_with_turbo(location, turbo)
        end
      end
    end

    private
      def visit_location_with_turbo(location, action)
        visit_options = {
          action: action.to_s == "advance" ? action : "replace"
        }

        script = []
        script << "Turbo.cache.clear()"
        script << "Turbo.visit(#{location.to_json}, #{visit_options.to_json})"

        self.status = 200
        self.response_body = script.join("\n")
        response.content_type = "text/javascript"
        response.headers["X-Xhr-Redirect"] = location
      end
  end
end

On loading a page, I see in the console for the web browser (Chrome) the following error: "Uncaught TypeError: Failed to resolve module specifier "application". Relative references must start with either "/", "./", or "../"."

hybridspyda
  • 21
  • 1
  • 4

0 Answers0