Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function collectResourcesFromHtml($html) {
- preg_match_all(self::MATCH_SRC, $html, $matches);
- $result = array_filter(array_merge($matches[1], $matches[2], $matches[3], $matches[4]));
- if (count($result) >= 3) { // min 3 resources found
- foreach ($result as $resource) {
- $this->generateLinkHeader($resource);
- }
- header('Link: ' . implode(',', $this->links));
- }
- }
- public function generateLinkHeader($resource) {
- $resource = ltrim($resource, '.');
- if ($resource[0] != '/') {
- $resource = '/'.$resource;
- }
- $link = "<".$resource.">; rel=preload";
- $extension = substr($resource, strrpos($resource, '.')+1);
- if (strrpos($extension, '?')) {
- $extension = substr($extension, 0,strrpos($extension, '?'));
- }
- switch ($extension) {
- case 'css':
- $link.= '; as=style';
- break;
- case 'js':
- $link.= '; as=script';
- break;
- case 'png':
- case 'jpg':
- $link.= '; as=image';
- break;
- default:
- $link.= '';
- }
- $this->links[] = $link;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement