Skip to content

Use pre-compiled assets if available #288

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

Closed
mchristen opened this issue Jun 1, 2015 · 4 comments
Closed

Use pre-compiled assets if available #288

mchristen opened this issue Jun 1, 2015 · 4 comments

Comments

@mchristen
Copy link

I've been trying to track down the cause of some request latency that happens on the first request after a fresh boot.

I determined that with the recent refactor of the server rendering code simply deferred the slowness to the first request instead of happening on bootup.

The issue is that by calling

::Rails.application.assets[filename].to_s

you are essentially calling find_asset on the Sprockets::Environment object. This causes the first lookup of the asset file to trigger a compilation to load the file into Sprockets cache objects.

In some production environments all assets are precompiled and config.assets.compile is set to false. I have a patch here that reads the file from disk if asset compilation is turned off in Rails. I'm sure there is a better way, and perhaps this should be a different kind of ServerRenderer altogether.

Using this patch I was able to get my first request latency down from 45s to around 4s, subsequent requests are much faster, what I would expect from a production system.

@rmosolgo
Copy link
Member

rmosolgo commented Jun 1, 2015

Wow, nice find! I've noticed that same latency after deploy, but I didn't dig in. I wondered if it was JS instances starting or something. Looks like not!

I like your approach! Does it require you to compile each of filenames? IE compile react.js and components.js separately?

I guess you could just handle that in by passing filenames: ["components.js"] then //= require react in components js.

@mchristen
Copy link
Author

Correct, all files configured for react.server_rendering need to be precompiled. I tried including react.js in the assets.precompile list, but I couldn't seem to get it to ever compile. I ended up going with your solution, where my components.js files //=require all the necessary pieces, including react.js.

So as long as all your component files are listed in assets.precompile and each component file //= requires all necessary per-requisites it should work just fine.

@rmosolgo
Copy link
Member

Here's another try at this: #430

@rmosolgo
Copy link
Member

rmosolgo commented Dec 9, 2016

Addressed by #430

@rmosolgo rmosolgo closed this as completed Dec 9, 2016
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

No branches or pull requests

2 participants