How to Cache Content to Improve App Performance

App Platform is a Platform-as-a-Service (PaaS) offering that allows developers to publish code directly to DigitalOcean servers without worrying about the underlying infrastructure.

If your app contains cacheable content, such as images and website data, you can improve the performance of your app for end users by implementing the Cache-Control header in your app’s HTTP responses.

The Cache-Control header in an HTTP request or response instructs web browsers and CDN edge servers to cache the content of a request for a specified period of time. This keeps subsequent requests and responses for the same content closer to the end user by caching it on local CDN edge servers or web browsers, which reduces response times and traffic on your app.

By default, App Platform deploys static sites with Cache-Control headers set to cache for 24 hours on CDN edge servers and 10 seconds in web browsers. If you redeploy the app, any cached content is invalidated across the web and CDN servers and web browsers will cache the latest iteration of the website upon any new traffic to the app.

Responses from dynamic apps served by service resources are not cached by default. To enable caching, you must set the Cache-Control header in the app’s HTTP response functions.

For example, in this Go example, we have set the Cache-Control header in the HTTP handler function to cache responses for 86400 seconds (24 hours):

http.HandleFunc("/cached", func(w http.ResponseWriter, r *http.Request) {
  w.Header().Set("Cache-Control", "public, max-age=86400")
  // rest of the response logic

How to implement the Cache-Control header into your app depends on the language and framework your app is based on. Reference the language’s documentation on how to do this.