Krishna Deepak
Krishna Deepak

Reputation: 1765

understanding caching js file on server side using php

I am trying to cache js files on server side and came across this link.

The whole idea is to get contents of all js files to be cached and write to a separate file which is also having a .js extension.

What I am not able to get is that, the cache file created is just another js file. How is it different from normal js files. To me it was like combing all js files into one big js files and output as a buffer. Also is it not increasing the code/text ratio because of this. Please help me understand and also to avoid the increase in code/text ratio.

I searched a lot on google and this site for answers and not able to get anything even close. So please downvote.

Upvotes: 2

Views: 1579

Answers (2)

bfavaretto
bfavaretto

Reputation: 71918

The article you linked to is part of a series that proposes the following steps for more efficient JavaScript server to client delivery:

  1. Link a single js file from the HTML, and setup the server to let PHP (or other server-side technology handle it. The server will combine your multiple files into one, to avoid additional, time-consuming HTTP calls.

  2. Compress the output with gzip

  3. Minify the code. This can mean several things, but at a minimum removing line-breaks, tabs and spaces to reduce file size.

  4. Caching on the server: instead of processing your js files on every request to combine them, and minify the result each time, save your processed js file on disk, and serve it directly to the clients. You can write a script that updates that file when needed (when any of your source files has been modified).

  5. Caching on the client: set the proper response headers to have the browsers cache your js file locally, and only request a new one from the server when the cache expired.

I think you were reading #4 out of context, it only makes sense after #1 and #3.

Upvotes: 1

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

the purpose of this is to reduce the number of connections to the server. In this approach there is one large .js file download, rather than a bunch of smaller ones. Presumably you will adjust your php to include only the js files that you actually need.

Upvotes: 1

Related Questions