0

How to remove comments and white spaces in html.erb files while deploying to production

System configuration

Rails version:
6.0.0
Ruby version:
2.6.1

B001ᛦ
  • 2,036
  • 6
  • 23
  • 31
  • 1
    Comments are never displayed or rendered, neither do whitespaces. If you want to compress your compiled output of views then I'd recommend using HAML or Slim instead of ERB if that's what your question is. – Surya Feb 04 '20 at 11:42

2 Answers2

1

Ruby comments (<%# comment %>) will not be rendered in erb files.

Not sure about removing whitespaces in .erb files. I suggest another approach. To configure gzip on web-server for example (nginx for example).

This way all your generated HTML responses will be compressed before sending to a browser.

Or you can use HAML or SLIM gem to generate HTML instead .erb

Sergey Andronov
  • 173
  • 1
  • 8
1

You don't.

If you compacted your .erb templates in production then the line numbers will no longer line up with the files in your "source code". This is essentially the same problem you can face when compacting javascript and there we have source maps that help aliviate the problem.

Its also completely pointless as you can use http compression to compress your responses before they are sent to the client. Whitespace compresses really well.

If you really want to reduce the amount of data your app sends do it by setting up HTTP compression. You can do it on the Rack layer with Rack::Deflate.

# add to config/application.rb
config.middleware.insert_after ActionDispatch::Static, Rack::Deflater

Or on the HTTP Server layer (NGinx, Apache). Which approach to use depends on the architecture you are deploying to.

max
  • 96,212
  • 14
  • 104
  • 165