Asset::cached() is a one-stop method for all of your asset caching needs. This should be the first thing that you call. It checks to see if the page is looking for a cached asset. If it is, then it will return a response that you can $page->send()
. If not, then just continue on your merry way. When you $page->display()
your html, it will look for all of your assets, and convert them to cached urls.
- If an asset is found we give it a unique (5 character) id that then becomes the "folder", and we add the
basename()
to the end for reference / seo sakes.- http://example.com/page/dir/bootstrap.css will become http://example.com/...../bootstrap.css where 'bootstrap.css' means nothing, and ..... is the actual asset location.
- 60 alphanumeric characters (no 0's) ^ 5 (character length) gives 777,600,000 possible combinations.
- If a #fragment is located immediately after the asset, we'll remove the fragment and ...
- If it is a .css or .js file then we will combine them together so that http://example.com/page/dir/bootstrap.css#../default.css#user/custom.css will become http://example.com/.....0.....0...../bootstrap-default-custom.css and we'll minify and serve the /page/dir/bootstrap.css, /page/default.css, and /page/dir/user/custom.css files all at once.
- Otherwise we'll replace the name with it ie. http://example.com/page/dir/image.jpg#seo will become http://example.com/...../seo.jpg
- If you add a query string to images, we'll remove and save it with the filename ie. http://example.com/page/dir/image.jpg?w=150#seo will become http://example.com/...../seo.jpg only ..... will be different from the previous example, and the image.jpg's width will be 150 pixels.
- To see all of the options here, check out the Glide Quick Reference guide.
- The
filemtime()
is saved so that when an asset changes, we can give it a new unique filename that the browser will then come looking for and cache all over again.- This allows us to tell browsers to never come looking for the asset again, because it will never change.
- There is no better way to make your pages load any faster than this.
Add the following to your composer.json
file.
{
"require ": {
"bootpress/asset": "^1.0"
}
}
<?php
use BootPress\Page\Component as Page;
use BootPress\Asset\Component as Asset;
$page = Page::html();
if ($asset = Asset::cached('assets')) {
$page->send($asset);
}
$html = $page->display('<p>Content</p>');
$page->send(Asset::dispatch('html', $html));
The MIT License (MIT). Please see License File for more information.