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

The path is not recognized when using importmap #5

Closed
brunoprietog opened this issue Jan 18, 2022 · 26 comments · Fixed by #6
Closed

The path is not recognized when using importmap #5

brunoprietog opened this issue Jan 18, 2022 · 26 comments · Fixed by #6

Comments

@brunoprietog
Copy link

Hello!

Using Rails 7 and importmap, I added this gem and ran the installation steps.

However, it only adds the @rails/request.js import in application.js, without adding it to importmap.rb.

I tried to add it manually using any of these options:

pin '@rails/request.js', to: 'rails-requestjs'
pin '@rails/request.js', to: 'request.js'

And I get the following, as appropriate:

Importmap skipped missing path: rails-requestjs
Importmap skipped missing path: request.js

Then, I executed:

bin/importmap pin @rails/request.js

And I got the same result, even though the URL is associated in importmap.rb.

I removed the gem and everything was solved. Now I am using the CDN.

What could be happening?

Something extra I didn't mention is that I'm using propshaft, will it affect anything?

Thanks!

@xirukitepe
Copy link

I'm having almost the same issue. But, mine wasn't working on production environment.

I'm getting:

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

It works on development environment though. I'm using CDN too atm.

@ellesgaard
Copy link

I'm having almost the same issue. But, mine wasn't working on production environment.

I'm getting:

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

It works on development environment though. I'm using CDN too atm.

I have the same issue in production

@GGrassiant
Copy link

Same issue on my end.
Works perfectly on my machine but deployment to Heroku failed.

I documented the issue here:
https://discuss.hotwired.dev/t/importmap-error-rails-request-js-in-production-heroku-disables-turbolinks/3717/3

@marcelolx
Copy link
Collaborator

I'm gonna take a look into this as soon as I have some free time.

@jantunes
Copy link

jantunes commented Jan 20, 2022

This issue is the js file is not being placed in the correct path to be served in rails7. I would just remove the gem entirely and pin it yourself:

pin "@rails/request.js", to: "https://ga.jspm.io/npm:@rails/request.js@0.0.6/src/index.js"

@GGrassiant
Copy link

GGrassiant commented Jan 21, 2022

Indeed, when removing the gem, running bundle install and pinning manually pin "@rails/request.js", to: "https://ga.jspm.io/npm:@rails/request.js@0.0.6/src/index.js" to importamp.rb and then redeploying, it does work as expected.

Edit: Thanks @jantunes

@marcelolx
Copy link
Collaborator

marcelolx commented Jan 26, 2022

Hey, v0.0.7 was released with the fix from #6, let me know if that did fix your problems! https://rubygems.org/gems/requestjs-rails/versions/0.0.7

Huge thanks to @d6rkaiz!

@GGrassiant
Copy link

Thanks a lot for taking the time to check it out so quickly. Really appreciated!

I created a new Rails 7 app and ran the normal install (gemfile + bundle install + ./bin/rails requestjs:install).

My Gemfile.lock is requestjs-rails (0.0.7) but for some reason when I start the server it is asking me to //= link requestjs.js in the manifest.js file. And if I do so I then get a 500 error.

The normal install did add import "@rails/requestjs" to the application.js file but nothing in the import map or elsewhere.
Maybe I messed something up so I'll take another look tonight after work and will update you.

@GGrassiant
Copy link

Hi!

So I tried again from a brand new Rails 7 app, created a scaffold for Post.
I followed the requestjs-rails normal install mentioned in the README and when restarting the server I get the error mentioned above (Asset requestjs.js was not declared to be precompiled in production etc.)

Let me know if need to do a different install.
The way to make it work is to manually pin the version like mention by jantunes.

@marcelolx
Copy link
Collaborator

@GGrassiant Could you test with the main branch with changes made in #7?

gem "requestjs-rails", github: "rails/requestjs-rails", branch: "main"

I tested it here and it seems that it is working now but I want to make sure before I release a new version that everything is OK.

Thanks for your patience!

@marcelolx
Copy link
Collaborator

@GGrassiant @kirillplatonov Just a small detail, did change the import back to import @rails/request.js in #8 to keep consistent with how we import it using npm/yarn.

@coder2000
Copy link

coder2000 commented Jan 31, 2022

Just updated to 0.0.7 and it broke my app saying the same as below. It was working fine until the update. The original issue sounds like a user configuration error and not an issue with the package. In regards to the second issue brought up it is expected with importmaps and Firefox etc according to the importmaps-rails readme: https://github.com/rails/importmap-rails#expected-errors-from-using-the-es-module-shim.

Hi!

So I tried again from a brand new Rails 7 app, created a scaffold for Post. I followed the requestjs-rails normal install mentioned in the README and when restarting the server I get the error mentioned above (Asset requestjs.js was not declared to be precompiled in production etc.)

Let me know if need to do a different install. The way to make it work is to manually pin the version like mention by jantunes.

@marcelolx
Copy link
Collaborator

marcelolx commented Jan 31, 2022

@coder2000 Can you point the gem to the github repo and see if it works again?

@GGrassiant
Copy link

@marcelolx Awesome!

I just

  • Created a brand new rails 7 app
  • Added rails/requestjs-rails as per the instructions in the README
  • Created a scaffold for Post (with only a title attribute)
  • in the index view, a dropdown of all the posts is linked to a Stimulus controller that on change calls a method on the Posts controller with get from @rails/requestjs to display the number of posts in a different dom element

Works perfectly locally. I also deployed to Heroku and it works flawlessly as well! No console error, no issue whatsoever.

Thanks a lot for taking the time to look into it and fix the issue. This is really appreciated. 🚀

@marcelolx
Copy link
Collaborator

Nice, will drop a new release soon! And sorry for those that have been affected with the latest broken release, shouldn't have released it.

@brunoprietog
Copy link
Author

Thanks for your time!

It works great for me too.

I have a question just out of curiosity. Why is it added to importmap automatically from the engine, instead of adding to the main importmap.rb file of the application? This differs from the behavior that turbo-rails or stimulus-rails has, where we have the imports manually added by the gem installer into our application's importmap.rb file.

Thanks!

@coder2000
Copy link

@coder2000 Can you point the gem to the github repo and see if it works again?

That works, thanks.

@marcelolx
Copy link
Collaborator

@brunoprietog There's no specific reason, it was this way since the beginning (in the early days turbo/stimulus-rails also did it this way hotwired/turbo-rails@36477e2#diff-a6c9ea9b91a99345ff04b9597b41369bc2552724c7c00135db376ff782aaf7ab). From what I can see here https://github.com/rails/importmap-rails#composing-import-maps in some ways it is encouraged to do it from the engine.

@marcelolx
Copy link
Collaborator

Release v0.0.8 https://rubygems.org/gems/requestjs-rails/versions/0.0.8

I did also yank v0.0.7 since it was broken

@mithos-pro
Copy link

I use rails/requestjs-rails with rails/request.js with esbuild.

v0.0.7 worked fine and now it broke something:

ruby-3.1.0/gems/railties-7.0.1/lib/rails/railtie/configuration.rb:96:in method_missing': undefined method importmap' for #<Rails::Application::Configuration

had to roll back to 0.0.6

Is it me or...?

@marcelolx
Copy link
Collaborator

@mithos99 With esbuild do you don't have a package.json? If you have, why don't you install request.js through yarn/npm?

It's not you, I did remove that check but seems that it is required in your case. I was just not expecting someone using this gem with esbuild when you can install it directly with npm/yarn

@mithos-pro
Copy link

mithos-pro commented Feb 4, 2022

@mithos99 With esbuild do you don't have a package.json? If you have, why don't you install request.js through yarn/npm?

Sure I did.
At https://github.com/rails/request.js instructions are:
Install the requestjs-rails gem and follow the step described there.

Then I added
yarn add @rails/request.js

It's not you, I did remove that check but seems that it is required in your case. I was just not expecting someone using this gem with esbuild when you can install it directly with npm/yarn

So do I need requestjs-rails gem for my config or yarn add is enough?

I'm so confused...

@marcelolx
Copy link
Collaborator

marcelolx commented Feb 4, 2022

@mithos99 If you want to stick with the gem, please try using the main branch to see if it works again (if it is working again I'll release a new version)

gem "requestjs-rails", github: "rails/requestjs-rails", branch: "main"

I have to update the README of the request.js repo (and add probably a new task in here to support esbuild), there have been so many changes that I forgot to update the description there. But basically, if you have a package.json you don't need this gem, since you can install it directly which is what we do when you install it with webpacker https://github.com/rails/requestjs-rails/blob/main/lib/install/requestjs_with_webpacker.rb

So do I need requestjs-rails gem for my config or yarn add is enough?

Yarn add should be enough.

@mithos-pro
Copy link

With just "yarn add @rails/request.js" and no gem works fine.
Thank you!

@wilsoncelyCUC
Copy link

Apologies for asking (new dev :) ), but I've done "yarn add @rails/request.js" and I am still not able to load the JS.
in a server heroku, work fine in my dev environment.

I wonder if somehow, this issue is related.

CAMPANAZZO git:(master) heroku logs | grep c4d5d71c-acc5-4270-b5d9-9330c2a4328e
2022-12-01T11:18:45.632819+00:00 app[web.1]: I, [2022-12-01T11:18:45.632743 #4] INFO -- : [c4d5d71c-acc5-4270-b5d9-9330c2a4328e] Started GET "/requestProvider.js.map" for 62.84.196.154 at 2022-12-01 11:18:45 +0000
2022-12-01T11:18:45.633446+00:00 app[web.1]: F, [2022-12-01T11:18:45.633392 #4] FATAL -- : [c4d5d71c-acc5-4270-b5d9-9330c2a4328e]
2022-12-01T11:18:45.633448+00:00 app[web.1]: [c4d5d71c-acc5-4270-b5d9-9330c2a4328e] ActionController::RoutingError (No route matches [GET] "/requestProvider.js.map"):
2022-12-01T11:18:45.633448+00:00 app[web.1]: [c4d5d71c-acc5-4270-b5d9-9330c2a4328e]
2022-12-01T11:18:45.635484+00:00 heroku[router]: at=info method=GET path="/requestProvider.js.map" host=campanazzo.herokuapp.com request_id=c4d5d71c-acc5-4270-b5d9-9330c2a4328e fwd="62.84.196.154" dyno=web.1 connect=0ms service=3ms status=404 bytes=1902 protocol=https
➜ CAMPANAZZO git:(master) yarn add @rails/request.js
yarn add v1.22.17

@marcelolx
Copy link
Collaborator

@wilsoncelyCUC can you give more context? Are you using esbuild? Is this app in a public repo or could you create a sample app that I could reproduce?

If it works in your dev environment but does not in production, I suppose you're missing something in the build step?

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

Successfully merging a pull request may close this issue.

9 participants