Self-host JavaScript SDK in your CDN

Steps on self-hosting and setting up the JavaScript SDK in your CDN.

This guide walks you through the steps to self-host the RudderStack JavaScript SDK and also covers different scenarios based on your directory structure.

Prerequisites

For self-hosting the JavaScript SDK, following the below directory structure (as present in RudderStack’s CDN) to place your SDK and the associated files is recommended:

Recommended directory structure for self-hosting RudderStack JavaScript SDK v3

In this structure, there should be two directories - modern and legacy - under the base directory. Place the following files and folders within these directories:

  • rsa.min.js and rsa.min.js.map: Core SDK file.
  • js-integrations: Device mode destination SDKs are located in this directory.
  • plugins: Plugins are located in this directory. Note that this is applicable only to the modern folder.
info
The SDK bundle in the modern directory is built for ES2015, while the legacy bundle is for ES5. Moreover, in the legacy version, all the plugins are bundled into the core SDK.

Setup

  1. Download the JavaScript SDK and save it as rsa.min.js. Also, download the corresponding source map file, and save it as rsa.min.js.map. Place these files in the modern folder as shown above.
  2. Download the required device mode destination SDK file along with the corresponding map file from the RudderStack CDN. Place these files in the js-integrations folder:
  • <dest_name>.min.js
  • <dest_name>.min.js.map
  1. Download the following plugin files along with the corresponding map files from the RudderStack CDN. Place these files in the plugins folder:
  • rsa-plugin-remote-<plugin_name>.min.js
  • rsa-plugin-remote-<plugin_name>.min.js.map
  • rsa-plugins-<helper_name>.min.js
  • rsa-plugins-<helper_name>.min.js.map
info
Downloading and hosting all the destination SDKs, plugins, and source map files (to help with troubleshooting) is recommended.
  1. Update sdkBaseUrl in the loading snippet to:
var sdkBaseUrl = "https://<subdomain>.<yourdomain>.com/<base-directory-path>";
  1. If your JavaScript SDK file name is not rsa.min.js, then update sdkName in the loading snippet to:
var sdkName = "<custom-file-name>.min.js";
Updating sdkBaseURL

Load device mode destinations and plugins

If you’ve installed the SDK via CDN and have retained the recommended directory and file structure, the SDK automatically determines the location of the device mode destinations and plugins.

Otherwise, provide location of the destination SDKs via the destSDKBaseURL load API option:

rudderanalytics.load(WRITE_KEY, DATA_PLANE_URL, {
  destSDKBaseURL: "https://<subdomain>.<yourdomain>.com/<integration-sdks-directory-path>"
});

Similarly, you can provide the location of the plugins via the pluginsSDKBaseURL load API option:

rudderanalytics.load(WRITE_KEY, DATA_PLANE_URL, {
  pluginsSDKBaseURL: "https://<subdomain>.<yourdomain>.com/<plugins-directory-path>"
});


Questions? Contact us by email or on Slack