Support

Find answers, guides, and tutorials to supercharge your content delivery.

Using a Moment.js CDN Combination

Updated on October 4, 2018
Using a Moment.js CDN Combination

What is Moment.js?

Moment.js is an open source library that allows you to parse, validate, manipulate and display dates in JavaScript. This library can be run from within a browser or in a Node.js application. For Node.js users, the following command can be used to install Moment.js.

npm install moment

It can then be used in your application as shown below.

var moment = require('moment');
moment().format();

There are a wide range of possibilities when using the Moment.js library for date-specific tasks. The projects provides users with guides including: introductory concepts, a parsing guide, and a warnings and errors guide, as well as detailed documentation. This article will discuss how to further accelerate the deliver of the Moment.js library through using a Moment.js CDN combination.

How to use a Moment.js CDN combination

If you're using Moment.js in the browser, there are a couple of options available when wanting to accelerate Moment.js via using a CDN. If you already have an existing CDN account you can easily reference the files using your CDN URL. The other option is to use a third party CDN service that is already hosting the files across a network of POPs. Both options are discussed in the following section.

Option 1 - Using your own CDN account

The first option is to use your own CDN account to accelerate the Moment.js library. In the case of KeyCDN, this can be done either by using a Pull Zone or Push Zone depending on your integration setup. If using a Pull Zone, upon successful integration, the static Moment.js file will be automatically pulled from your origin server and delivered from the CDN edge servers once it is cached. Therefore there is no URL configuration required at the origin in order to accelerate the Moment.js library via your CDN.

Alternatively, if using a Push Zone, you can simply upload the latest version of Moment.js and reference it in your project using your Zone URL or Zone Alias:

<script src="https://example-hexid.kxcdn.com/js/moment.min.js"></script>

Option 2 - Using an open source CDN

If you do not have a CDN account, another option for using an Moment.js CDN combination is to reference the file using an open source CDN service such as cdnjs.com. The cdnjs Moment.js file can be referenced using the following snippet (ensure that you first check for the latest version x.y.z).

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/x.y.z/moment.min.js"></script>

Additionally, Moment.js locales can also be referenced using cdnjs, for example:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/x.y.z/locale/ar.js"></script>

Benefits of a Moment.js CDN combination

Websites using a Moment.js CDN combination will be able to deliver assets faster to users as there will be less distance traveled between the client and the server. Using a CDN for JS libraries, CSS frameworks, and other static assets not only increases their delivery speed but also helps improve redundancy and increase security. There also exist many other benefits to users who accelerate their Moment.js file and other static content with KeyCDN such as:

Supercharge your content delivery 🚀

Try KeyCDN with a free 14 day trial, no credit card required.

Get started
KeyCDN uses cookies to make its website easier to use. Learn more