AlexStack
AlexStack

Reputation: 17431

Sharing one JavaScript file among several domains

Problem definition:

Question: The JQuery library is too big for this slow connection but one-time download is acceptable. However, we don't want the clients to download it every time they connect to each new server. The JQuery on all these devices is the same. But apparently the browser cache is domain based. How can we cache the JQuery library so that the client doesn't have to download the JQuery every time it connects to a new server?

Upvotes: 2

Views: 170

Answers (4)

AlexStack
AlexStack

Reputation: 17431

Apparently there is no standard way to include JQuery in our project so we chose to replace JQuery with a smaller similar library. These are the options:

  • JQuery mobile is a better fit for our project since it has a smaller size and can do almost everything we need from JQuery.
  • Zepto.js is also another suitable replacement.
  • JQuip is a stripped down version of JQuery that can help our specific project.

Upvotes: 0

Aaron Digulla
Aaron Digulla

Reputation: 328760

Here is a simple solution: All your customers must edit their /etc/hosts file (Google for where you can find it on Windows) and put a line like this in their config:

1.2.3.4 jquery-from-closest-server.com

Each client must figure out the closest server which has jQuery and replace 1.2.3.4 with its IP address.

In your HTML code, always use the link http://jquery-from-closest-server.com/jquery.js

To be safe, you may want to register jquery-from-closest-server.com for those customers which do have Internet access.

When the browser asks for the file, it will use /etc/hosts to resolve the IP address. Since the domain name will be the same for all your embedded devices (changes in the IP address are ignored by the browser), the script will be downloaded once for all clients.

Note that this means you can never upgrade to a newer version of jQuery. The problem is that you'd have to replace it on all embedded devices at the same time because there is no telling from which server the customers are downloading it from.

If that bothers you (and it will in about four months after you discovered the first serious bug), here is another solution: Instead of serving the HTML from your embedded device, distribute a static web app (a set of HTML files and JavaScript) which customer can install in their desktop. Use AJAX and iframes to replace parts of the static web app with data from your embedded device.

Advantages:

  • very fast (no downloads at all)
  • only little code on the embedded device
  • easy upgrade strategy
  • No deadlocks when you need to upgrade either the embedded devices or your controller app.

[EDIT] PS: Consider to compress jQuery with gzip. That leaves you with a 33KB file. All HTML5 browsers can decode compressed files, you just have to tell them by setting the necessary HTTP headers.

Upvotes: 0

Flo
Flo

Reputation: 1671

you could link it to the ip in your link tag. The server does not have to have access to that access or anything, and so all the clients will geht the jquery from the same server all the time and so it is cached.

Upvotes: 1

Kae Verens
Kae Verens

Reputation: 4077

just link to it in one place...

for example, if you have a single server, http://1.2.3.4/ that you want to designate as your CDN, put jQuery on it, and link to it in your scripts using <script src="http://1.2.3.4/jquery.min.js"></script>

Upvotes: 1

Related Questions