If your application on Heroku has been having slow initial page load times, the most likely cause is a sleeping dyno.
Dynos are Heroku’s own lightweight virtualized containers that are used to house and serve your app. When using only one dyno, such as with Heroku’s free plan, Heroku will put your dyno to sleep when there has no been no activity on your app for over an hour. This often occurs when no visitors have been to your site in a while.
When Heroku puts your dyno to sleep, it takes its allotted resources, such as processors and memory, and re-allocates them elsewhere. While sleeping, your application is idle and is unable to do anything. However, once a new request comes back into your application, Heroku will give back the resources to your app. During this process, your app will go through the process of booting up and any visitor to your site will have to endure a very long initial page load time.
To prevent this from happening, all that has to be done is to automatically trigger a http request to your site at least once a hour. With such a scheduled request, there will be no opportunity for you app to go to sleep and for users to experience such slow page load times.
One of the simplest solutions out there is to use Heroku’s own Heroku Scheduler to set up a repeating curl request directed at your application’s url. This post will cover how to properly configure such a solution.
Setting up Heroku Scheduler
Assuming you already have an app set up on Heroku, you’ll need to get the Heroku Scheduler add-on.
To do so, from within your app’s page on heroku, click on the Get Add-ons button.
Scroll down the page to the Workers and Queueing section of the Add-Ons page and click on Heroku Scheduler.
On the Heroku Scheduler’s add-on page, select the app you’d like to use the scheduler with and click the add button.
Navigate to your App’s Page in Heroku and click on the Heroku Scheduler icon.
From within the Heroku Scheduler’s configuration page, click on the add job
button. Next, enter in the url of the app you’d like to prevent from sleeping
(in this example I’ve used
my-app.herokuapp.com). This should be a page that
is publicly accessible. Finally, set the frequency to ‘Every 10 minutes’
and click Save.
That’s it, you’re all set. Your Heroku app won’t go to sleep again.
If you’d like to explore other options on how to keep your app from going to sleep, check out this question on StackOverflow.