diff --git a/README.md b/README.md index 8b6f460..b7f387c 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,17 @@ Then add the middleware to your Http Kernel (`app/Http/Kernel.php`). Do so towar protected $middleware = [ \TomSchlick\ServerPush\Http2ServerPushMiddleware::class, ]; +protected $routeMiddleware = [ + 'http2' => \TomSchlick\ServerPush\Http2ServerPushMiddleware::class, +]; ``` ## Usage - Now when you enable it on a route it will automatically include the resources in your elixir `/build/rev-manifest.json` file. To add a resource manually you may use `pushStyle($pathOfCssFile)`, `pushScript($pathOfJsFile)`, `pushFont($pathOfFontFile)` or `pushImage($pathOfImageFile)` from anywhere in your project. + +``` +Route::group(['middleware' => ['http2:frontend']], function () { + //... +}); +``` diff --git a/config/server-push.php b/config/server-push.php index d204d0a..fdc8afa 100644 --- a/config/server-push.php +++ b/config/server-push.php @@ -1,23 +1,25 @@ [ - 'styles' => [ - - ], - - 'scripts' => [ - - ], - - 'images' => [ - + 'styles' => [], + 'scripts' => [], + 'images' => [], + 'fonts' => [], + ], + 'groups' => [ + 'backed' => [ + 'styles' => [], + 'scripts' => [], + 'images' => [], + 'fonts' => [], ], - - 'fonts' => [ - + 'frontend' => [ + 'styles' => [], + 'scripts' => [], + 'images' => [], + 'fonts' => [], ], ], diff --git a/src/Http2ServerPushMiddleware.php b/src/Http2ServerPushMiddleware.php index 83de42c..3cd9779 100644 --- a/src/Http2ServerPushMiddleware.php +++ b/src/Http2ServerPushMiddleware.php @@ -13,16 +13,19 @@ class Http2ServerPushMiddleware protected $response; /** - * @param Request $request - * @param Closure $next + * @param Request $request + * @param Closure $next + * @param null|string $group * * @return mixed */ - public function handle(Request $request, Closure $next) + public function handle(Request $request, Closure $next, string $group = null) { $this->response = $next($request); if ($this->shouldUseServerPush($request)) { + app('server-push')->massAssign(config('server-push.groups.'.$group, [])); + $this->addServerPushHeaders(); } @@ -34,6 +37,8 @@ protected function addServerPushHeaders() if (app('server-push')->hasLinks()) { $link = implode(',', app('server-push')->generateLinks()); $this->response->headers->set('Link', $link, false); + + app('server-push')->clear(); } } diff --git a/src/HttpPush.php b/src/HttpPush.php index 73dda41..2203f6d 100644 --- a/src/HttpPush.php +++ b/src/HttpPush.php @@ -12,11 +12,22 @@ class HttpPush */ public $resources = []; + /** + * @param array $paths + * @param string|null $type + */ + public function queueResources(array $paths = [], string $type = null) + { + foreach ($paths as $path) { + $this->queueResource($path, $type); + } + } + /** * Push a resource onto the queue for the middleware. * - * @param string $resourcePath - * @param string $type + * @param string $resourcePath + * @param null|string $type */ public function queueResource(string $resourcePath, string $type = null) { @@ -81,4 +92,15 @@ public static function getTypeByExtension(string $resourcePath) : string default: return 'image'; } } + + /** + * @param array $data + */ + public function massAssign(array $data = []) + { + foreach ($data as $type => $paths) { + $type = rtrim($type, 's'); + $this->queueResources($paths, $type); + } + } } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index b26d758..4673221 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -59,14 +59,7 @@ public function register() */ protected function registerDefaultLinks() { - $instance = app('server-push'); - - foreach (config('server-push.default_links', []) as $type => $paths) { - $type = rtrim($type, 's'); - foreach ($paths as $path) { - $instance->queueResource($path, $type); - } - } + app('server-push')->massAssign(config('server-push.default_links', [])); } /**