sprockets: Document how to generate and host source maps for production bug tracking
Goal: Give bug trackers like bugsnag access to sprockets generated source maps.
Hi all, I’m trying to have my bug tracker use source maps in production and staging. This references the discussion happening in https://github.com/rails/sprockets/issues/410#issuecomment-270474542.
I have successfully generated the source maps using the link
directive in the manifest.js:
//= link source.js
//= link source.js.map
I am ok including sourceMapURL=
in the final minified js file (despite it not being best practice). However, I’ve been unable to get this to happen for production builds, only debug. Maybe this is straightforward and I’m missing an option. Is it a simple configuration in an initializer? There is already this configuration for debug:
https://github.com/rails/sprockets/blob/master/lib/sprockets.rb#L106-L108
register_pipeline :debug do
[SourceMapCommentProcessor]
end
I would prefer to just host the source map file and have my bug tracker pick it up from a url.
That being said, @vincentwoo mentioned that he POSTs it up to sentry.io. How do you know how to pair the source file with the map file if they have different digests? Do you have your own processor?
Thanks for any help.
System configuration
- Sprockets version 4 beta 5
- Ruby version 2.4.1 on rails 5.0.6
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 15
- Comments: 17 (1 by maintainers)
@emiellohr I imagine you don’t need help anymore, but I just worked on this today. Hoping this will help others trying to do this too.
To
config/environments/production.rb
, add:The second part you need to specify because by default, Rails (
sprockets
?) only tries:manifest
ifdebug = false
, which it’s clearly not in our case. The other value that can be in that array isenvironment
but that doesn’t work for me, even as a backup, and I’m using:manifest
anyway. If you don’t do this, Rails will generate non-digested pathnames, which won’t work, since sprockets still digests them when they’re compiled.debug = true
for production feels weird, but it’s not so bad. Even in development, it still concatenates the file (this is different than the normal behavior, wheredebug = true
typically means that the files are unconcatenated and unminified) no matter what. (I’m guessing this ismanifest.js
behavior.)Also in
config/environments/development.rb
,config.assets.debug
is probably already true (if it’s not, make it true). surprisingly here, you need to add a JS compressor in order to get the source maps to work in development:( I guess you could set this in
config/initializers/assets.rb
or elsewhere, if you wanted, but that means the :test environment would compress JS, which will probably not handle sourcemaps as elegantly as the browser).Then, in your
app/assets/config/manifest.js
, add a corresponding.map
file for each.js
file you have. For example:Now, when you deploy, the map will be compiled too. Letting people see your JS is a minor security problem, but, crucially, there’s no link to it in your JS files, so it’s very hard to find.
The reason development knows where your source map is, is that the following line is added to the bottom of your minified JS. Thankfully, this does not happen in production.
If you’re concerned about it still being available (though hard to find), you could manually delete the compiled
.map
file from your compiled assets as a part of your build process.The above notes might be wrong, it’s just my experience from working on this today. Hopefully it helps someone 😃
(I still need to work on modifying my build process to send the source map to my error logging service, Rollbar. That should be relatively simple, since they’re being generated already.)
Sorry to bump this but we’re also in the same situation, we want source maps in production (like DHH). After waiting years for sprockets to support this we were very happy to see that
sprockets 4
officially added support (thanks 🙇), but then when trying to upgrade we noticed there’s actually no way to use it in production… (without brittle hacks mentioned above).I totally understand that there may be a majority still considering this a bad practice and thus keeping it disabled by default in production seem ok. But there could at least be an option to enable it for people who want to, no?
Thanks!
For those with similar issues, I was able to resolve it using the following steps
upgrade
sprockets
to v4 beta 7 .gem 'sprockets', '~> 4.0.0.beta7'
create a manifest file
The above should generate source map when you run this command.
bundle exec rake assets:precompile RAILS_ENV=production
From Rollbar’s documentation, the preferred method is via their API
sourcemap.rake
Note: The above rake task executes after the default
rake assets:precompile
runs, it doesn’t replace the default rails taskAlternately, If you don’t mind putting the sourcemap url in the minified JS —>
Any update on this?
I’m trying to get source maps working in
production
as well. I was able to get them generated by following @cllns’s instructions above. But I’m also not able to get it to add asourceMappingURL
comment.Now that source maps in production by default seems to be the official Rails position, can we expect some movement on this?
@framky007 I was able to send the source maps to Rollbar, but not in the way Rollbar expected (so, they didn’t work in Rollbar’s interface)
I don’t get it. Can someone please explain? I’ve upgraded my Rails project to Sprockets 4, just to get source maps in production. Instead I got sourcemaps in development? How does this work? Do I need an additional initializer or config setting?
@cllns Thanks for sharing that info!