My main goal is to allow for the loading of several pages to be as fast as possible. For this I want to take advantage of both, the cache and one "special technique" that, as a fallback, relies on the standard cache.
Structure
On the backend I have the following structure. There's a main page in the public_html and several subpages, each with specific css rules different from each other. The creation of all the minimized files is done by a script, so no extra complexity there. For simplicity, let's assume that this is the structure, although it's more complex:
/public_html
/index.php
/style.css ~50kb
/min.css ~100kb
/subjects
/index.php
/style.css ~20kb
/min.css ~10kb
/books
/index.php
/style.css ~20kb
/min.css ~10kb
...
First request
So when the user enters first time on a subpage, they will receive this html code:
<!DOCTYPE html>
<html>
<head>
<link href="/subjects/min.css" rel="stylesheet" type="text/css">
</head>
<body>
All the body here
<link href="/min.css" rel="stylesheet" type="text/css">
</body>
As you can see, the user loads all the css code needed for that page in the header, in a small file. Note that /subjects/min.css
is MUCH smaller than /min.css
which would make this first request to load faster. Then, after the full html and css has correctly loaded, the /min.css
will start loading. This file contains all of the subpages style.
Note that it's appropriate to put the <link>
within the <body>
tag, and even if it didn't work, there's no problem since the page-specific style is already loaded. Why am I loading this here? Keep reading:
Following requests
For the second and subsequent requests on that session, the user will receive this html code:
<!DOCTYPE html>
<html>
<head>
<link href="/min.css" rel="stylesheet" type="text/css">
</head>
<body>
All the body here
</body>
The /min.css
should be already cached from the first request. However, if for any reason it's not, it will load now the full minimized style, as in any normal website. This would be the fallback case.
Is this a valid scheme? Why haven't I seen anything like this before? Does it contain any logic error?
These are the main problems I can see, not strong enough in comparison to the benefits:
- It adds some extra complexity to the code.
- An extra request, after everything is already loaded, needs to be made. This would add a slight overhead on the server, however it's a static file.
Notes about the comments:
The browser will make less requests. This is true, in this way the browser does one extra request. However, it's after loading the html and css, so this will not affect in a great manner the html.
Cache. Yes, I'm doing my best to catch the file. A point could be made against cache of the
<link>
if it's inside the<body>
, though, because I don't know if it behaves differently about the cache, I only assumed yes in the question.