Skip to content

middleware name and parameters #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
//...
});
```
30 changes: 16 additions & 14 deletions config/server-push.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<?php

return [

// Include assets you want to link on every page load here.
'default_links' => [
'styles' => [

],

'scripts' => [

],

'images' => [

'styles' => [],
'scripts' => [],
'images' => [],
'fonts' => [],
],
'groups' => [
'backed' => [
'styles' => [],
'scripts' => [],
'images' => [],
'fonts' => [],
],

'fonts' => [

'frontend' => [
'styles' => [],
'scripts' => [],
'images' => [],
'fonts' => [],
],
],

Expand Down
11 changes: 8 additions & 3 deletions src/Http2ServerPushMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}
}

Expand Down
26 changes: 24 additions & 2 deletions src/HttpPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
}
}
9 changes: 1 addition & 8 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', []));
}

/**
Expand Down