Importmap error @rails/request.js in production (heroku) disables Turbolinks

Hi everyone!
Hope you are doing well.

I recently created a new project with Rails 7 and doing some Turbo_stream and Turbo_link as well as handling some requests via Stimulus before updating the UI.

For one of the Stimulus Controller, on change in a select tag, I make a request to my backend with @rails/request.js

The controller looks like this

import { Controller } from "@hotwired/stimulus";
import { get } from "@rails/request.js";

export default class extends Controller {
  static values = {
    url: String,
  };

  connect() {
    console.log("[Stimulus] successfully connected to the select_controller.js");
  }

  change(event) {
    const params = new URLSearchParams();
    params.append("target", event.target.dataset.id);
    params.append("selected_value", event.target.selectedOptions[0].text);

    get(`${this.urlValue}?${params}`, {
      responseKind: "turbo-stream"
    });
  }
}

I followed the instruction here to install @rails/request.js in my project.

Everything is fine locally (Turbolinks, Actioncable etc.) but when I deploy to Heroku (after provisioning a Redis instance) I get the following error:

Uncaught TypeError: Failed to resolve module specifier "@rails/request.js". Relative references must start with either "/", "./", or "../".

I understand as per the documentation that it has no user-facing consequences.

The main problem with that is Turbo is not working properly (clicking on a link in the pagination for example or submitting a form reloads the entire page).

I saw in the Heroku logs the following: Importmap skipped missing path: rails-requestjs.

I ran bin/importmap pin @rails/requestjs but funny enough if I run ./bin/importmap json locally, @rails/request.js is in the imports but the same command on Heroku returns:

 WARN -- : Importmap skipped missing path: rails-requestjs
{
  "imports": {
     [...]
  }
}

If I remove the gem and the parts of code related to @rails/requestjs, push to heroku again, Turbo_frames and link work correctly (without reloading the page).

I guess it’s a setting I haven’t handled properly when deploying but I am not sure exactly what to do. If you have any idea that would be awesome!

Have a nice day.

Looks like you aren’t the only person to notice this. See The path is not recognized when using importmap · Issue #5 · rails/requestjs-rails · GitHub

Oh wow! Thanks a lot, I subscribed to the issue.
Have a great day.

Maintainer will look into it but as a temporary fix, as suggested in the Github issue thread:

  • removing the gem
  • running bundle install
  • pinning manually pin "@rails/request.js", to: "https://ga.jspm.io/npm:@rails/request.js@0.0.6/src/index.js" to importamp.rb
  • redeploying

Solves the problem

Now fixed with this PR: