Search code examples
ruby-on-railsruby-on-rails-7import-mapspre-compilation

Precompilation in rails 7


As in older versions of rails, in production, we used to precompile our assets first so that performance can be better while serving assets from public. But in rails 7, as default configuration says that you should use all your css , fonts, and custom js files from asset pipeline and ecternal js libraries using importmaps, then what is the precompilation process of rails 7 in production.


Solution

  • Nothing changed. Asset pipeline aka sprockets or propshaft are used in development to serve any local assets, including import-maps. When you make changes, assets are compiled on the fly and new assets are served when you refresh the page. This process takes time, memory, and cpu cycles - something you don't want to waste in production.

    Solution is to compile everything ahead of time - precompile. Everything goes into public/assets directory. Then web server, like nginx, is configured to serve any requests to /assets/* from public/assets/*. This way assets are served fast and your app server doesn't need to care about them.

    use all your css , fonts, and custom js files from asset pipeline

    Before we had sprockets and webpacker, two asset piplines that did the same thing. Two asset urls /assets and /packs, two compilation processes, two public directories public/assets and public/packs.

    In rails 7 everything hooks into sprockets. Any new build tools, like tailwindcss, can process assets and put them in app/assets/builds where sprockets can do what it did before - compile and serve in development, precompile for production.