March 6
Cache-busting is a technique used in web development to ensure that the latest version of a web page is delivered to the user's browser. When a user visits a website, their browser caches some of the resources, like images, CSS files, and JavaScript files, to improve page load times. But this can cause issues when a new version of the resource is released, as the browser may continue to use the cached version. To solve this problem, cache-busting URLs are used to force the browser to fetch the latest version of the resource.
Cache-busted URLs are URLs that include a unique identifier, typically a version number or a hash of the resource's content, appended to the end of the URL. By changing the identifier every time a new version of the resource is released, the browser is forced to fetch the latest version of the resource, rather than using the cached version.
Cache-busted URLs are required to ensure that users are always served the latest version of a web page. Without cache-busting, users may see an outdated version of a web page, which could result in broken functionality or poor user experience.
Building cache-busted URLs in AWS and GCP is relatively straightforward. Both platforms provide options for adding version numbers or hashes to the URLs of your resources.
In AWS, you can use CloudFront, which is a content delivery network that integrates with Amazon S3, to add version numbers or hashes to your resource URLs. By default, CloudFront will use the origin URL as the cache key, which means that if the resource's URL changes, CloudFront will automatically invalidate the cache and fetch the latest version of the resource.
Here is an example of how to use a version number in a resource URL:
<img src="https://yourbucket.cloudfront.net/images/image.jpg?v=1.0">
Here is an example of how to use a hash in a resource URL:
<img src="https://yourbucket.cloudfront.net/images/image.jpg?v=9b82d12c">
In GCP, you can use the Google Cloud Storage (GCS) bucket versioning feature to add version numbers to your resource URLs. With versioning enabled, each object in the bucket will have a unique version ID, which can be appended to the end of the URL to create a cache-busted URL. GCP also provides options for using hashes in your URLs, such as the Content-Based Hashing feature in Cloud CDN.
Here is an example of how to use a version number in a resource URL:
<img src="https://storage.googleapis.com/yourbucket/images/image.jpg?version=1">
Here is an example of how to use a hash in a resource URL using Content-Based Hashing:
<img src="https://yourcdn.example.com/images/image.jpg?h=9b82d12c">
Here is an example of a Lambda@Edge function that appends a hash to the resource URLs:
const crypto = require('crypto'); exports.handler = (event, context, callback) => { const request = event.Records[0].cf.request; const hash = crypto.createHash('md5').update(request.uri).digest('hex'); request.uri += `?h=${hash}`; callback(null, request); };
To connect the Lambda@Edge function with the CloudFront distribution:
Once the changes have propagated, CloudFront will start appending the version number or hash to the resource URLs, and the Lambda@Edge function will ensure that the correct version or hash is used for each request. This will enable cache-busted URLs and ensure that users always see the latest version of your resources.
Cache-busted URLs are an essential technique for ensuring that users always see the latest version of a web page. By adding unique identifiers to the URLs of your resources, you can force the browser to fetch the latest version of the resource, rather than using the cached version. In AWS and GCP, you can easily build cache-busted URLs using features like CloudFront and GCS bucket versioning.