Skip to content

Commit 420205b

Browse files
committed
Merge pull request #21 from talyssonoc/use_cache
Use cache for store components and React source when in production
2 parents e09991a + 340b7f9 commit 420205b

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

lib/ReactServiceProvider.php

+25-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Illuminate\Support\Facades\App;
55
use Illuminate\Support\Facades\Blade;
66
use Illuminate\Support\Facades\Config;
7+
use Illuminate\Support\Facades\Cache;
78

89
class ReactServiceProvider extends ServiceProvider {
910

@@ -24,17 +25,33 @@ public function register() {
2425

2526
$this->app->bind('React', function() {
2627

27-
$defaultReactPath = implode(DIRECTORY_SEPARATOR,
28-
[App::publicPath(), 'vendor', 'react-laravel', 'react.js']);
28+
if(App::environment('production')
29+
&& Cache::has('reactSource')
30+
&& Cache::has('componentsSource')) {
2931

30-
$defaultComponentsPath = implode(DIRECTORY_SEPARATOR,
31-
[App::publicPath(), 'js', 'components.js']);
32+
$reactSource = Cache::get('reactSource');
33+
$componentsSource = Cache::get('componentsSource');
3234

33-
$reactPath = Config::get('app.react.source', $defaultReactPath);
34-
$componentsPath = Config::get('app.react.components', $defaultComponentsPath);
35+
}
36+
else {
37+
$defaultReactPath = implode(DIRECTORY_SEPARATOR,
38+
[App::publicPath(), 'vendor', 'react-laravel', 'react.js']);
3539

36-
$reactSource = file_get_contents($reactPath);
37-
$componentsSource = file_get_contents($componentsPath);
40+
$defaultComponentsPath = implode(DIRECTORY_SEPARATOR,
41+
[App::publicPath(), 'js', 'components.js']);
42+
43+
$reactPath = Config::get('app.react.source', $defaultReactPath);
44+
$componentsPath = Config::get('app.react.components', $defaultComponentsPath);
45+
46+
$reactSource = file_get_contents($reactPath);
47+
$componentsSource = file_get_contents($componentsPath);
48+
49+
if(App::environment('production')) {
50+
Cache::forever('reactSource', $reactSource);
51+
Cache::forever('componentsSource', $componentsSource);
52+
}
53+
54+
}
3855

3956
return new React($reactSource, $componentsSource);
4057
});

0 commit comments

Comments
 (0)