├── .craftplugin ├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config.php ├── resources └── img │ └── icon.png └── src ├── Http2ServerPush.php ├── config.php ├── icon.svg ├── models └── Settings.php ├── services └── Http2ServerPushService.php └── translations └── en └── http2-server-push.php /.craftplugin: -------------------------------------------------------------------------------- 1 | {"pluginName":"HTTP2 Server Push","pluginDescription":"Automatically add HTTP2 Link headers for CSS, JS and image assets.","pluginVersion":"1.0.0","pluginAuthorName":"Superbig","pluginVendorName":"superbig","pluginAuthorUrl":"https://superbig.co","pluginAuthorGithub":"sjelfull","codeComments":"","pluginComponents":["services","settings"],"consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # CRAFT ENVIRONMENT 2 | .env.php 3 | .env.sh 4 | .env 5 | 6 | # COMPOSER 7 | /vendor 8 | 9 | # BUILD FILES 10 | /bower_components/* 11 | /node_modules/* 12 | /build/* 13 | /yarn-error.log 14 | 15 | # MISC FILES 16 | .cache 17 | .DS_Store 18 | .idea 19 | .project 20 | .settings 21 | *.esproj 22 | *.sublime-workspace 23 | *.sublime-project 24 | *.tmproj 25 | *.tmproject 26 | .vscode/* 27 | !.vscode/settings.json 28 | !.vscode/tasks.json 29 | !.vscode/launch.json 30 | !.vscode/extensions.json 31 | config.codekit3 32 | prepros-6.config 33 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # HTTP2 Server Push Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). 6 | 7 | ## 1.0.1 - 2019-01-17 8 | ### Changed 9 | - Changed craftcms dependency to work with 3.1 10 | 11 | ## 1.0.0 - 2017-11-14 12 | ### Added 13 | - Initial release 14 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Superbig 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HTTP2 Server Push plugin for Craft CMS 3.x 2 | 3 | Automatically add HTTP2 Link headers for CSS, JS and image assets. 4 | 5 | ![Screenshot](resources/img/icon.png) 6 | 7 | ## Requirements 8 | 9 | This plugin requires Craft CMS 3.0.0-beta.23 or later. 10 | 11 | ## Installation 12 | 13 | To install the plugin, follow these instructions. 14 | 15 | 1. Open your terminal and go to your Craft project: 16 | 17 | cd /path/to/project 18 | 19 | 2. Then tell Composer to load the plugin: 20 | 21 | composer require superbig/craft3-http2serverpush 22 | 23 | 3. In the Control Panel, go to Settings → Plugins and click the “Install” button for HTTP2 Server Push. 24 | 25 | ## HTTP2 Server Push Overview 26 | 27 | From [The Go Blog](https://blog.golang.org/h2push): 28 | > To improve latency, HTTP/2 introduced server push, which allows the server to push resources to the browser before they are explicitly requested. A server often knows many of the additional resources a page will need and can start pushing those resources as it responds to the initial request. This allows the server to fully utilize an otherwise idle network and improve page load times._ 29 | 30 | Also: [Read what CloudFlare has to say about it](https://blog.cloudflare.com/announcing-support-for-http-2-server-push-2/). 31 | 32 | ## Configuring HTTP2 Server Push 33 | 34 | ```php 35 | null, 39 | 40 | // Include images 41 | 'includeImages' => false, 42 | ]; 43 | ``` 44 | 45 | ## Using HTTP2 Server Push 46 | 47 | After the plugin is installed, Link headers will be added automatically to page template responses. 48 | 49 | Brought to you by [Superbig](https://superbig.co) 50 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "superbig/craft3-http2serverpush", 3 | "description": "Automatically add HTTP2 Link headers for CSS, JS and image assets.", 4 | "type": "craft-plugin", 5 | "version": "1.0.1", 6 | "keywords": [ 7 | "craft", 8 | "cms", 9 | "craftcms", 10 | "craft-plugin", 11 | "http2 server push" 12 | ], 13 | "support": { 14 | "docs": "https://github.com/sjelfull/craft3-http2serverpush/blob/master/README.md", 15 | "issues": "https://github.com/sjelfull/craft3-http2serverpush/issues" 16 | }, 17 | "license": "MIT", 18 | "authors": [ 19 | { 20 | "name": "Superbig", 21 | "homepage": "https://superbig.co" 22 | } 23 | ], 24 | "require": { 25 | "craftcms/cms": "^3.0.0", 26 | "symfony/dom-crawler": "^2.7|^3.0", 27 | "symfony/css-selector": "^2.7|^3.0", 28 | "tightenco/collect": "^5.5" 29 | }, 30 | "repositories": [ 31 | { 32 | "type": "composer", 33 | "url": "https://asset-packagist.org" 34 | } 35 | ], 36 | "autoload": { 37 | "psr-4": { 38 | "superbig\\http2serverpush\\": "src/" 39 | } 40 | }, 41 | "extra": { 42 | "name": "HTTP2 Server Push", 43 | "handle": "http2-server-push", 44 | "schemaVersion": "1.0.0", 45 | "hasCpSettings": false, 46 | "hasCpSection": false, 47 | "changelogUrl": "https://raw.githubusercontent.com/sjelfull/craft3-http2serverpush/master/CHANGELOG.md", 48 | "components": { 49 | "http2ServerPushService": "superbig\\http2serverpush\\services\\Http2ServerPushService" 50 | }, 51 | "class": "superbig\\http2serverpush\\Http2ServerPush" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /config.php: -------------------------------------------------------------------------------- 1 | null, 34 | 35 | // Include images 36 | 'includeImages' => false, 37 | ]; -------------------------------------------------------------------------------- /resources/img/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sjelfull/craft3-http2serverpush/8acc02a5d56fae7dd39556c2a92253ef81617742/resources/img/icon.png -------------------------------------------------------------------------------- /src/Http2ServerPush.php: -------------------------------------------------------------------------------- 1 | output = $this->http2ServerPushService->output($event->output); 60 | } 61 | ); 62 | 63 | Craft::info( 64 | Craft::t( 65 | 'http2-server-push', 66 | '{name} plugin loaded', 67 | [ 'name' => $this->name ] 68 | ), 69 | __METHOD__ 70 | ); 71 | } 72 | 73 | // Protected Methods 74 | // ========================================================================= 75 | 76 | /** 77 | * @inheritdoc 78 | */ 79 | protected function createSettingsModel () 80 | { 81 | return new Settings(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/config.php: -------------------------------------------------------------------------------- 1 | null, 29 | 30 | // Don't parse image tags by default 31 | "includeImages" => false, 32 | 33 | ]; 34 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | icon 5 | Created with Sketch. 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/models/Settings.php: -------------------------------------------------------------------------------- 1 | false ], 49 | ]; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/services/Http2ServerPushService.php: -------------------------------------------------------------------------------- 1 | getRequest(); 47 | 48 | if ( $request->isCpRequest || $request->isLivePreview || $request->getAcceptsJson() ) { 49 | return $output; 50 | } 51 | 52 | return $this->generateAndAttachLinkHeaders($output); 53 | } 54 | 55 | /** 56 | * @param string $output 57 | * 58 | * @return string 59 | */ 60 | protected function generateAndAttachLinkHeaders ($output) 61 | { 62 | $settings = Http2ServerPush::$plugin->getSettings(); 63 | 64 | $headers = $this->fetchLinkableNodes($output, $settings->includeImages) 65 | ->flatten(1) 66 | ->map(function ($url) { 67 | return $this->buildLinkHeaderString($url); 68 | }) 69 | ->filter() 70 | ->take($settings->limit) 71 | ->implode(','); 72 | 73 | if ( !empty(trim($headers)) ) { 74 | $this->addLinkHeader($headers); 75 | } 76 | 77 | return $output; 78 | } 79 | 80 | /** 81 | * Get the DomCrawler instance. 82 | * 83 | * @param string $output 84 | * 85 | * @return Crawler 86 | */ 87 | protected function getCrawler (string $output) 88 | { 89 | if ( $this->crawler ) { 90 | return $this->crawler; 91 | } 92 | 93 | return $this->crawler = new Crawler($output); 94 | } 95 | 96 | /** 97 | * Get all nodes we are interested in pushing. 98 | * 99 | * @param string $output 100 | * 101 | * @return Collection 102 | */ 103 | protected function fetchLinkableNodes ($output, $includeImages = false) 104 | { 105 | $crawler = $this->getCrawler($output); 106 | $filter = 'link, script[src]'; 107 | 108 | if ( $includeImages ) { 109 | $filter .= ', img[src]'; 110 | } 111 | 112 | return new Collection($crawler->filter($filter)->extract([ 'src', 'href' ])); 113 | } 114 | 115 | /** 116 | * Build out header string based on asset extension. 117 | * 118 | * @param string $url 119 | * 120 | * @return string 121 | */ 122 | private function buildLinkHeaderString ($url) 123 | { 124 | $linkTypeMap = [ 125 | '.CSS' => 'style', 126 | '.JS' => 'script', 127 | '.BMP' => 'image', 128 | '.GIF' => 'image', 129 | '.JPG' => 'image', 130 | '.JPEG' => 'image', 131 | '.PNG' => 'image', 132 | '.TIFF' => 'image', 133 | ]; 134 | $type = (new Collection($linkTypeMap))->first(function ($type, $extension) use ($url) { 135 | return $this->contains(strtoupper($url), $extension); 136 | }); 137 | 138 | return is_null($type) ? null : "<{$url}>; rel=preload; as={$type}"; 139 | } 140 | 141 | /** 142 | * Add Link Header 143 | * 144 | * @param $link 145 | */ 146 | private function addLinkHeader ($link) 147 | { 148 | $headers = Craft::$app->getResponse()->getHeaders(); 149 | 150 | if ( $headers->get('Link') ) { 151 | $link = $headers->get('Link') . ',' . $link; 152 | } 153 | 154 | $headers->set('Link', $link); 155 | } 156 | 157 | /** 158 | * @param $haystack 159 | * @param $needles 160 | * 161 | * @return bool 162 | */ 163 | private function contains ($haystack, $needles) 164 | { 165 | foreach ((array)$needles as $needle) { 166 | if ( $needle !== '' && mb_strpos($haystack, $needle) !== false ) { 167 | return true; 168 | } 169 | } 170 | 171 | return false; 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /src/translations/en/http2-server-push.php: -------------------------------------------------------------------------------- 1 | 'HTTP2 Server Push plugin loaded', 18 | ]; 19 | --------------------------------------------------------------------------------