Skip to content
Draft
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
6 changes: 6 additions & 0 deletions config/rapidez/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@

// The path to redirect to after a failed checkout
'checkout_success_fail_redirect_path' => 'cart',

// This array should contain any turbo frames with their respective views.
// These should then be used by using @turboframe('frame-name')
'turbo-frames' => [
// 'frame-name' => 'view-path',
],
];
2 changes: 1 addition & 1 deletion resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<meta name="robots" content="@yield('robots', Rapidez::config('design/search_engine_robots/default_robots'))"/>
<link rel="canonical" href="@yield('canonical', url()->current())" />

@php($configPath = route('config') . '?v=' . Cache::rememberForever('cachekey', fn () => md5(Str::random())) . '&s=' . config('rapidez.store'))
@php($configPath = route('config') . '?v=' . Rapidez::getCacheKey() . '&s=' . config('rapidez.store'))
<link href="{{ $configPath }}" rel="preload" as="script">
<script defer src="{{ $configPath }}" onerror="window.app?.config ? window.$emit('configError') : window.configError = true"></script>

Expand Down
5 changes: 5 additions & 0 deletions resources/views/turbo-frame.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@props(['frame', 'viewPath', 'isRoute' => false)

<turbo-frame id="menu" @unless ($isRoute) src="/turbo/{{ $frame }}/{{ Rapidez::getCacheKey() }}" loading="lazy" target="_top" @endunless>
@includeCached($viewPath, ['deferred' => $isRoute])
</turbo-frame>
14 changes: 14 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,18 @@
Route::fallback(config('rapidez.routing.controllers.fallback'));

Route::get('heartbeat', fn () => response()->json(['alive' => true]));

// Register all turbo frame routes
if (count(config('rapidez.frontend.turbo-frames', []))) {
Route::middleware('cache.headers:public;max_age=3600;stale_while_revalidate=3600;etag')->group(function () {
Route::get('turbo/{frame}/{cachekey}', function (string $frame) {
$viewPath = config('rapidez.frontend.turbo-frames')[$frame] ?? null;
if (! $viewPath) {
abort(404);
}

return view('rapidez::turbo-frame', ['frame' => $frame, 'viewPath' => $viewPath, 'isRoute' => true]);
});
});
}
});
1 change: 1 addition & 0 deletions src/Facades/Rapidez.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @method static ?string config(string $path, $default = null, bool $sensitive = false)
* @method static ?string content($content)
* @method static object fancyMagentoSyntaxDecoder(string $encodedString)
* @method static string getCacheKey()
* @method static array getStores($storeId = null)
* @method static array getStore($storeId)
* @method static void setStore($store)
Expand Down
3 changes: 1 addition & 2 deletions src/Http/Controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Rapidez\Core\Facades\Rapidez;
use Rapidez\Core\Models\Attribute;
use Rapidez\Core\Models\Category;
Expand Down Expand Up @@ -44,7 +43,7 @@ public function getExposedConfigValues(): array
public function getConfig(): array
{
return [
'cachekey' => Cache::rememberForever('cachekey', fn () => md5(Str::random())),
'cachekey' => Rapidez::getCacheKey(),
'translations' => __('rapidez::frontend'),

'index' => $this->getIndexNames(),
Expand Down
7 changes: 7 additions & 0 deletions src/Rapidez.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Rapidez\Core\Exceptions\StoreNotFoundException;
use Rapidez\Core\Models\Config;
use Rapidez\Core\Models\ConfigScopes;
Expand Down Expand Up @@ -86,6 +88,11 @@ public function fancyMagentoSyntaxDecoder(string $encodedString): object
return json_decode(str_replace(array_values($mapping), array_keys($mapping), $encodedString));
}

public function getCacheKey(): string
{
return Cache::rememberForever('cachekey', fn () => md5(Str::random()));
}

public function getStores(callable|array|int|string|null $store = null): array
{
$storeModel = config('rapidez.models.store');
Expand Down
14 changes: 12 additions & 2 deletions src/RapidezServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rapidez\Core;

use BladeUI\Icons\Factory;
use Exception;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Http\Kernel;
Expand Down Expand Up @@ -133,7 +134,7 @@ protected function bootPublishables(): self

// We're so explicit here as otherwise the json translations will also be published.
// That will publish to /lang/vendor/rapidez/nl.json where it will not be loaded
// by default and; you should keep everyting in one place: /lang/nl.json
// by default and; you should keep everything in one place: /lang/nl.json
foreach (['en', 'nl'] as $lang) {
$this->publishes([
__DIR__ . '/../lang/' . $lang . '/frontend.php' => lang_path('vendor/rapidez/' . $lang . '/frontend.php'),
Expand Down Expand Up @@ -238,6 +239,15 @@ protected function registerBladeDirectives(): self
return "<?php echo Rapidez::config({$expression}) ?>";
});

Blade::directive('turboframe', function (string $frame) {
$viewPath = config('rapidez.frontend.turbo-frames')[$frame] ?? null;
if (! $viewPath) {
throw new Exception('Turbo frame ' . $frame . ' not found.');
}

return "<?php echo \$__env->make('rapidez::turbo-frame', [...\Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']), 'frame' => '{$frame}', 'viewPath' => '{$viewPath}', 'isRoute' => false])->render(); ?>";
});

Blade::if('storecode', function ($value) {
$value = is_array($value) ? $value : func_get_args();

Expand Down Expand Up @@ -412,7 +422,7 @@ function ($url) {

$this->setAppends(
Arr::reject(
$this->getAppends(), fn ($curentAppends) => in_array($curentAppends, $appendsToRemove)
$this->getAppends(), fn ($currentAppends) => in_array($currentAppends, $appendsToRemove)
)
);

Expand Down
Loading