Product update · · 2 min read

Self-host the dependencies TalkJS chat loads at runtime

Serve the emoji picker, emoji data and fonts from your own servers, or load them all from the TalkJS CDN.

Self-hosted dependencies for TalkJS chat components

If your app sends a strict Content Security Policy (CSP), or your security review only allows scripts and fonts from servers you control, every third-party host in your chat UI is one more exception to approve. Until recently, the TalkJS React components and Web Components fetched some of their optional files from jsDelivr, a public CDN.

TalkJS Components SDK 0.3.15 adds setDependencyUrls and disableFontLoading, so you choose where the React components and Web Components fetch their runtime dependencies: the emoji picker, emoji data, fonts and media libraries. Components SDK 0.3.17 serves every default from the TalkJS CDN at cdn.talkjs.com, so jsDelivr is no longer one of the hosts.

How to self-host TalkJS dependencies

To keep the main bundle small, the TalkJS components load some extras only when a user needs them. Examples are the emoji picker library when someone opens the picker, emoji data in the user's language, and the encoder that records voice messages. setDependencyUrls points any of these at your own URLs. Call it once at startup, before the first TalkJS component mounts. A call after the components have started loading files throws an error.

This example serves the emoji picker and its English emoji data from your own host, and turns off TalkJS font loading:

import { disableFontLoading, setDependencyUrls } from "@talkjs/react-components";

// Run once, before any TalkJS component mounts
disableFontLoading();
setDependencyUrls({
  libs: {
    "emoji-picker-element": "https://cdn.example.com/talkjs/emoji-picker-element/index.js",
  },
  emojiData: {
    en: "https://cdn.example.com/talkjs/emoji-data/en/data.json",
  },
});

setDependencyUrls merges your URLs with the defaults, so you can self-host one file or all of them. Anything you don't override, such as the voice message encoder or emoji data for other languages, still loads from cdn.talkjs.com. When a user's language has no entry, TalkJS falls back to the two-letter language code and then to English. For Web Components, import both functions from @talkjs/web-components instead.

With disableFontLoading, the TalkJS components stop loading the Inter font and the font that shows country flag emojis on Windows. You then load Inter from your own servers in CSS, or set a different font. You may not need it at all: the components skip any Inter weight your page already loads. The Self-hosting dependencies guide lists every default URL and gives the @font-face rules.

TalkJS dependencies now load from cdn.talkjs.com

From Components SDK 0.3.17, every runtime dependency the React components and Web Components fetch by default comes from cdn.talkjs.com. In 0.3.16 and earlier, some of them came from jsDelivr. If you install the components from npm, your CSP needs only TalkJS hosts for these files, as the Content Security Policy guide shows.

If you use domain masking, the components fetch these dependencies through your own domain instead of cdn.talkjs.com, like the rest of their TalkJS traffic. Domain masking is part of the Enterprise plan. See pricing.

Self-hosting dependencies FAQ

Do I need to change my CSP when I upgrade to Components SDK 0.3.17?

Not if your policy already allows *.talkjs.com. You can drop cdn.jsdelivr.net from it, unless you load the TalkJS component bundle itself from jsDelivr.

Does setDependencyUrls work with the Classic JavaScript SDK?

No. setDependencyUrls and disableFontLoading are part of the React components and Web Components. The Classic SDKs run chat in an iframe served by TalkJS.

Get started with self-hosted dependencies

Upgrade @talkjs/react-components or @talkjs/web-components to 0.3.17 or later to load every default from cdn.talkjs.com. To serve the files yourself, follow the Self-hosting dependencies guide.