Skip to content

Commit fb32f7b

Browse files
Assets cache. (#81)
1 parent 21d03a2 commit fb32f7b

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

routes/web.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,30 @@
1111
}
1212
});
1313

14-
Route::get('/mary/asset', function (Request $request) {
15-
if ($request->name) {
16-
$extension = Str::of($request->name)->afterLast('.')->toString();
14+
Route::middleware('cache.headers:public;max_age=2628000;etag')->get('/mary/asset', function (Request $request) {
15+
if (! $request->name) {
16+
abort(404);
17+
}
18+
19+
if (Str::of($request->name)->contains('..')) {
20+
abort(404);
21+
}
1722

18-
$type = match ($extension) {
19-
'js' => 'application/javascript',
20-
'css' => 'text/css',
21-
default => 'text/html'
22-
};
23+
$file = Str::of($request->name)->before('?')->toString();
2324

24-
return response(File::get(__DIR__ . "/../libs/{$request->name}"))->header('Content-Type', $type);
25+
if (! File::exists(__DIR__ . "/../libs/{$file}")) {
26+
abort(404);
2527
}
2628

27-
abort(404);
29+
$extension = Str::of($file)->afterLast('.')->toString();
30+
31+
$type = match ($extension) {
32+
'js' => 'application/javascript',
33+
'css' => 'text/css',
34+
default => 'text/html'
35+
};
36+
37+
return response(File::get(__DIR__ . "/../libs/{$file}"))->withHeaders([
38+
'Content-Type' => $type,
39+
]);
2840
});

0 commit comments

Comments
 (0)