Serve JavaScript SDK on Custom Domains

Use your own domain to serve the RudderStack JavaScript SDK.

RudderStack serves the JavaScript SDK through Amazon S3, delivered via the Amazon CloudFront CDN.

This guide covers the steps to use your own domain instead of the RudderStack domains for serving the JavaScript SDK.

Setup overview

Create a new distribution by following these steps:

  1. Log in to your AWS console.
  2. Click Services and go to Network & Content Delivery > CloudFront.
  3. Click Create a CloudFront distribution.

The following table gives a high-level overview of the required cache policy, origin request policy, and response headers policy for serving the JavaScript SDK:

Cache policyOrigin request policyResponse headers policy (optional)
Cache policy settingsOrigin request policy settingsNone

Step 1: Configure distribution

The following sections highlight the required distribution settings for serving the SDK.

Origin

Custom domains distribution settings

The following table summarizes the settings:

FieldSetting
Origin domaincdn.rudderlabs.com
ProtocolHTTPS Only
HTTPS port443
Minimum origin SSL protocolTLSv1.2
Namecdn.rudderlabs.com
Enable Origin ShieldNo

Default cache behavior settings

Custom domains distribution settings

The following table summarizes the settings:

FieldSetting
Compress objects automaticallyYes
Viewer protocol policyRedirect HTTP to HTTPS
Allowed HTTP methodsGET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE
Restrict viewer accessNo

Cache key and origin requests

Select Cache policy and origin request policy (recommended) and configure the following settings:

Custom domains distribution settings

The following table summarizes the settings:

FieldSetting
Cache policySee Cache policy settings
Origin request policySee Origin request policy settings.
Response headers policyNone

Cache policy settings

Cache policy settings for serving JS SDK

The following table summarizes the settings:

Details
FieldSetting
NameName of your cache policy, for example,jssdk-caching-policy.
DescriptionAdd your policy description here.
TTL settings
FieldSettingNotes
Minimum TTL0 (in seconds)Ensures the no-store header is respected.
Maximum TTL31536000 (in seconds)Allows CloudFront to use the Cache-Control value from origin if it is greater than 0.
Default TTL300 (in seconds)Used when origin does not include Cache-Control headers.

For the RudderStack CDN, CloudFront is configured with a long default TTL (one year) to maximize cache efficiency, and rely on invalidations during releases to refresh content.

RudderStack recommends configuring the prescribed TTLs settings when setting up a custom domain. For the reasoning behind these recommendations, see the AWS documentation.

This ensures that the custom domain’s CloudFront distribution always respects the origin Cache-Control and Age headers as the TTLs defined by RudderStack. Since these headers are managed by RudderStack, they may be updated in the future to improve reliability and correctness.

In cases where you need to control browser-side caching policies instead of relying solely on RudderStack’s origin settings, you can configure a CloudFront response headers policy with the Override origin option enabled for Cache-Control headers.

warning
If you are using a different cloud provider for your custom domain, make sure to set the maximum TTL value to 300, that is, 5 minutes.
Cache key settings
FieldSetting
HeadersNone
Query stringsAll
CookiesNone
Compression support

Select both Gzip and Brotli.

Then, click Create to generate the cache policy.

Origin request policy settings

Create a new origin request policy with the following settings:

Origin request policy settings
FieldSetting
NameEnter the origin request policy name, for example, jssdk-origin-request-policy
HeadersInclude the following headers
Add header
  • Access-Control-Request-Headers
  • Access-Control-Request-Method
  • Content-Encoding
  • Origin
Query stringsAll
CookiesNone

Additional distribution settings

The following table summarizes the settings:

FieldSetting
Price classUse all edge locations(best performance)
Alternate domain name(CNAME)<YOUR_CUSTOM_DOMAIN>
SSL CertificateAdd your custom SSL Certificate.

See Use custom SSL certificates for more information.
Supported HTTP versionsHTTP/2
Standard loggingOff
IPv6On
Custom domains distribution settings

Step 2: Deploy distribution

Click Create distribution and wait for CloudFront to be deployed:

CloudFront deployment

Add CNAME Record to DNS

Once your distribution is deployed, create a CNAME record for the subdomain you wish to use along with the distribution URL.

NameValue
Subdomain you wish to use (used in the creation of the distribution).

Note: This will vary based on your DNS provider but will typically be just the subdomain. For example: for cdn.yourdomain.com you would use cdn.
The CDN URL for the created distribution. Example: <prefix>.cloudfront.net
CDN distribution URL

Step 3: Serve SDK

See the following sections to serve the SDK depending on your installation method:

CDN

NPM

Since you have used the NPM module for integrating the SDK directly into your application, there is no configuration required for serving the core SDK.

Additional configuration

info

Note that this section is only applicable for:

  • npm installations
  • Customized CDN installations not following the default directory structure that the SDK uses to automatically fetch the plugins and device mode destinations.

This section covers some additional configuration required for loading the device mode destinations and plugins:

Use custom SSL certificates

To use a custom domain for your use case, you can request or import an SSL certificate with your CDN provider. Note that this is an optional setting.

To use the AWS Certificate Manager with CloudFront, choose the relevant ACM/IAM certificate in the Custom SSL certificate field:

Custom SSL certificate
info
You can choose your subdomain or use a wildcard domain *.yourdomain.com to set up multiple subdomains.

The AWS Certificate Manager will guide you through the verification by email or DNS TXT records. You will be able to choose your own domain for SSL certificates once verified.

Custom request header for GCP external load balancer

If you’re setting up a custom domain using the GCP External HTTPS Load Balancer, make sure to add a custom request header in your backend service configuration:

HeaderValue
Hostcdn.rudderlabs.com

FAQ

Can I overcome ad blockers by serving the JavaScript SDK on my domain?

Many popular ad blockers block specific SDK-related downloads and API calls based on the domain name and URL. You can serve the SDK on your CDN to circumvent this issue.



Questions? Contact us by email or on Slack