Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline vs included js and css?

In an environment with at least 500ms latency over 2G mobile connections (~0.1mbps), what's the fastest and most efficient way of sending a about 10kb of css and js, in around 5-10 files on the server, to the client?

I can think of three options:

  1. combining all js to one file and all css to one file
  2. linking all css and js files one by one
  3. inline everything

I know google uses inline, but that's probably just to save server sockets. They are even saving ram by running in stateless mode - they trust the clients to remember the sessions for them. Server power isn't an issue at all.

On the other hand, facebook seem to autogenerate their css (their names are base64 encoded), but into over 10 different files sent to the user, and they don't even seem to optimize it that heavily; only some whitespace removal.

I'm already passing all the files through a function that compresses everything, so any one of these are feasible. I don't want to choose the first alternative because it's easier.

The first two takes advantage of caching (the second one a bit less than the first one) but the second only requires three requests to the server, and the third only requires one get request from the server (ignoring the few images we might have on some of the pages).

Does Android / iOS cache js and css across restarts of the browser? If not, then inline sounds better.

The only goal is to minimize the average load time of the user. Each user will be spending about 100 page loads on the site per day, seeing about 40 css and js files per day. The css and js is basically static content. It's set to cache 30 days, and we change the url if the file changes using /path/to/file.ext?md5-hash-of-file. Also, everything is gzipped wherever possible.

EDIT:

I think i should clarify the two options I found for number two. Is it a good idea to use a single file for css and js across the whole site? It would only use two requests and remove any double (or septuple) caching because a single function is in two or more different combined js files, but a download of up to 1MB doesn't sound that good.

Today it's basically one combined css per view, so every time you view the same page again the content is cached. However, some js and css is used on more than one page.

like image 325
Filip Haglund Avatar asked Jul 15 '13 17:07

Filip Haglund


People also ask

What is the difference between an inline and embedded CSS?

Embedded styles are included in a <style> block in the <head> of the file. Inline styles are attached to an HTML element using the style attribute.

Is it better to use inline CSS?

Inline CSS is considered useful as it reduces the number of files that the browser needs to download before displaying the web page. With an external CSS, the browser first loads an HTML file and then downloads the CSS file.

Which is more better to use JavaScript or CSS?

CSS parses and renders faster. For things like animations, it more easily hooks into the browser's refresh rate cycle to provide silky smooth animations (this can be done in JS, too, but CSS just makes it so damn easy). And it fails gracefully. A JavaScript error can bring all of the JS on a page to screeching halt.

What is inline JS and CSS?

What is inline CSS and JavaScript? # Inlining CSS and JavaScript both refer to simply including the CSS and JS within your HTML file. Inline small CSS should be included within the <head> tags of an HTML file while inline small JavaScript can be included either within the <head> tag or the <body> tag.


1 Answers

It really depends on the usage. For a page with only one time visitors , I would recommend inlining everything. This makes for a faster initial load (a single request vs multiple requests) and easier to work with. This is the case for landing pages, help pages, wizards and similar one-use pages.

However, if you are expecting recurring visitors, I'd recommend using an external file. While the first load will be slower, you make it up with near-zero load time afterwards for these assets. This is the case for most websites.

like image 126
Francisco Presencia Avatar answered Nov 15 '22 00:11

Francisco Presencia