Skip to content

Commit

Permalink
optimisation: crawler (almost*) skips all routes that do not have the…
Browse files Browse the repository at this point in the history
… `static` middleware.
  • Loading branch information
david-d-h committed Oct 25, 2023
1 parent 0859ef7 commit 48ea4e1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/LaravelStaticServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Vormkracht10\LaravelStatic;

use Illuminate\Contracts\Http\Kernel;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Vormkracht10\LaravelStatic\Commands\StaticBuildCommand;
use Vormkracht10\LaravelStatic\Commands\StaticClearCommand;
use Vormkracht10\LaravelStatic\Middleware\PreventStaticResponseMiddleware;

class LaravelStaticServiceProvider extends PackageServiceProvider
{
Expand All @@ -19,4 +21,13 @@ public function configurePackage(Package $package): void
StaticBuildCommand::class,
]);
}

public function packageBooted()
{
$kernel = $this->app->make(Kernel::class);

$kernel->prependMiddlewareToGroup('web',
PreventStaticResponseMiddleware::class
);
}
}
36 changes: 36 additions & 0 deletions src/Middleware/PreventStaticResponseMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Vormkracht10\LaravelStatic\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

class PreventStaticResponseMiddleware
{
public function handle(Request $request, Closure $next)
{
$bypass = config('static.build.bypass_header');

$key = array_key_first($bypass);

$value = $bypass[$key];

if (! $request->header($key) === $value) {
return $next($request);
}

$route = Route::current();

$shouldHandle = in_array(
StaticResponse::class,
Route::gatherRouteMiddleware($route),
);

if (! $shouldHandle) {
return response(status: 200);
}

return $next($request);
}
}

0 comments on commit 48ea4e1

Please sign in to comment.