JavaScript SDK Design Guide extracted from work and personal experience
JavaScript SDK Design Guide extracted from work and personal experience
This guide provides an introduction to develop a JavaScript SDK.
The best one sentence to describe an SDK is: "The SDK is the connection bridging the gap between users and the (browser) machine."
By using this guide, the SDK will be able to run in browsers, desktop, mobile web and various other platforms capable of running JavaScript.
The target audience of this writeup excludes non-browser environments such as hardware, embedded and Node.js.
Suggest improvements by editing, or drop suggestions on the issue list. I owe you a beer :beers:
READ IT
ONLINE
This question is pretty ubiquitous, but here it is again.
"Short for software development kit, a programming package that enables a programmer to develop applications for a specific platform. Typically an SDK includes one or more APIs, programming tools, and documentation." - webopedia
Depending on the purpose of SDK's service and usage — common shared traits are, but not limited to be native, short, fast, clean, readable and testable.
The widely adopted good practice, is to write SDK with vanilla JavaScript. Languages compiling to JavaScript such as LiveScript, CoffeeScript, TypeScript and others are not recommended.
It is also recommended not to use libraries such as jQuery in SDK development. The exception is of course when it is really important. There are also other jQuery-like libraries, zepto.js etc to choose from, for the DOM manipulation purposes.
In event of HTTP ajax request requirements — there are native equivalent such as window.fetch. It is light-weight, supported in ever growing platforms.
Backward compatibility is paramount. Every new SDK version released should be enabled with support of previous older versions. Likewise, current version should be designed to support future SDK versions. This is referred to as Forward compatibility.
Moreover, a good Documentation, well commented code, a healthy unit test coverage, as well as end-to-end (user) scenario are key to the success of SDK.
Based on the book Third-Party JavaScript
Three use cases worth considering while designing a JavaScript SDK:
Suggest a case in which the use of an SDK in JavaScript environment is deemed important.
To include the SDK in a user-facing environment, It is a good practice to use Asynchronous Syntax to load the scripts.
This helps to optimize the user experience on the website that are using the SDK. This approach reduces chances of the SDK library interfering with the hosting website.
<script>
(function () {
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'http://<DOMAIN>.com/sdk.js';
var x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
})();
</script>The async syntax is used when targetting modern browsers.
<script async src="http://<DOMAIN>.com/sdk.js"></script><script type="text/javascript" src="http://<DOMAIN>.com/sdk.js"></script>Here's the simple graph to show the difference between Asynchronous and Traditional Syntax.
Asynchronous:
|----A-----|
|-----B-----------|
|-------C------|Synchronous:
|----A-----||-----B-----------||-------C------|Asynchronous and deferred JavaScript execution explained
https://developers.google.com/speed/docs/insights/BlockingJS
It is good practice to avoid, or minimize, the use of blocking JavaScript, especially external scripts that must be fetched before they can be executed. Scripts that are necessary to render page content can be inlined to avoid extra network requests, however the inlined content needs to be small and must execute quickly (non-blocking fashion) to deliver good performance. Scripts that are not critical to initial render should be made asynchronous or deferred until after the first render.
When using an Asynchronous approach, It is ill-advised to execute SDK initialization functions before all libraries are loaded, parsed and executed in the hosting page.
Consider the following snippet as a visual example to the previous statement:
<script>
(function () {
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'http://<DOMAIN>.com/sdk.js';
var x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
})();
// execute your script immediately here
SDKName('some arguments');
</script>The end result of such initialization will lead to bugs.
The SDKName() function, undefined at this point, executes before it becomes available in the environment's global variable. The script is not loaded yet.
To make it work, some tricks are necessary to make sure the script executes successfully. The event will (need to) be stored in the SDKName.q queue array. The SDK should be able to handle and execute the SDKName.q event and initialize the SDKName namespace.
The following snippet depicts the statement in previous paragraph.
<script>
(function () {
// add a queue event here
SDKName = SDKName || function () {
(SDKName.q = SDKName.q || []).push(arguments);
};
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'http://<DOMAIN>.com/sdk.js';
var x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
})();
// execute your script immediately here
SDKName('some arguments');
</script>Or using [].push
<script>
(function () {
// add a queue event here
SDKName = window.SDKName || (window.SDKName = []);
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'http://<DOMAIN>.com/sdk.js';
var x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
})();
// execute your script immediately here
SDKName.push(['some arguments']);
</script>There are other different ways to include a script
import "your-sdk";There is full source code — and this awesome tutorial "Loading JavaScript Modules" may help for in-depth understanding of concepts discussed above.
…It is not a good practice to use one of the following versioning styles:
brand-v<timestamp>.js brand-v<datetime>.js brand-v1-v2.js,The reason is that it becomes confusing to track the lastest version. Therefore, previous styling does not help developers who use the SDK.
It is however a good practice to use Semantic Versioning, also known as SemVer, when versioning SDKs.
It has three main parts, each corresponding to importance of a release: "MAJOR.MINOR.PATCH".
Version in v1.0.0 v1.5.0 v2.0.0 is easier to trace and track in changelog documentation, for instance.
Depending on service design, some of the ways SDK can be distributed (or tracked) by version are the following:
http://<DOMAIN>.com/sdk.js?v=1.0.0http://<DOMAIN>.com/v1.0.0/sdk.jshttp://v1.<DOMAIN>.com/sdk.jsDepending on Use Case, there are other environment dependent forms that are commonly advised to use:
stable version http://<DOMAIN>.com/sdk-stable.js unstable version http://<DOMAIN>.com/sdk-unstable.js alpha version `http://.com/sNo open issues yet, or sync has not completed.