Skip to content

Commit db0ff1f

Browse files
Assets: add cache + package version (#82)
* Assets cache. * Assets: add cache + package version
1 parent fb32f7b commit db0ff1f

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

routes/web.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Http\Request;
4+
use Illuminate\Support\Facades\Cache;
45
use Illuminate\Support\Facades\File;
56
use Illuminate\Support\Facades\Route;
67
use Illuminate\Support\Str;
@@ -34,7 +35,9 @@
3435
default => 'text/html'
3536
};
3637

37-
return response(File::get(__DIR__ . "/../libs/{$file}"))->withHeaders([
38-
'Content-Type' => $type,
39-
]);
38+
return Cache::rememberForever($request->name, function () use ($file, $type, $request) {
39+
return response(File::get(__DIR__ . "/../libs/{$file}"))->withHeaders([
40+
'Content-Type' => $type,
41+
]);
42+
});
4043
});

src/MaryServiceProvider.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444

4545
class MaryServiceProvider extends ServiceProvider
4646
{
47+
public static $third_party = [
48+
'calendar' => '2.7.0',
49+
'currency' => '1.0.0',
50+
'diff' => '3.4.24',
51+
'flatpickr' => '4.6.13',
52+
];
53+
4754
/**
4855
* Perform post-registration booting of services.
4956
*/
@@ -146,21 +153,25 @@ public function registerMaryJSDirective(): void
146153
Blade::directive('maryJS', function ($expression) {
147154
$parts = Str::of($expression)->explode(',');
148155

149-
$name = Str::of($parts->first())->replace("'", "")->replace('"', "");
156+
$file = Str::of($parts->first())->replace("'", "")->replace('"', "");
157+
$package = Str::of($file)->before('/')->toString();
158+
$version = self::$third_party[$package] ?? 'x';
150159

151160
$extra = $parts->count() == 2 ? $parts->last() : '';
152161
$extra = Str::of($extra)->replace("'", "")->replace('"', "");
153162

154-
return "<script src='/mary/asset?name=$name' $extra></script>";
163+
return "<script src='/mary/asset?name=$file?$version' $extra></script>";
155164
});
156165
}
157166

158167
public function registerMaryCSSDirective(): void
159168
{
160169
Blade::directive('maryCSS', function ($expression) {
161-
$expression = Str::of($expression)->replace("'", "")->replace('"', "");
170+
$file = Str::of($expression)->replace("'", "")->replace('"', "");
171+
$package = Str::of($file)->before('/')->toString();
172+
$version = self::$third_party[$package] ?? 'x';
162173

163-
return "<link rel='stylesheet' type='text/css' href='/mary/asset?name=$expression' />";
174+
return "<link rel='stylesheet' type='text/css' href='/mary/asset?name=$file?$version' />";
164175
});
165176
}
166177

0 commit comments

Comments
 (0)