Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Pass the translations to the modal script to ensure it loads without …
…latency and only pass the cookie script inside the head tag
  • Loading branch information
Debatty-Tom committed Oct 21, 2025
commit 2acf6708a6c56831e2eff517a478efa01aecae88
3 changes: 3 additions & 0 deletions resources/views/cookies.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
</aside>

{{-- STYLES & SCRIPT : feel free to remove them and add your own --}}
<script data-cookie-consent>
{!! $script !!}
</script>
<style data-cookie-consent>
{!! file_get_contents(LCC_ROOT . '/dist/style.css') !!}
</style>
7 changes: 2 additions & 5 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
'prefix' => config('cookieconsent.url.prefix'),
'middleware' => config('cookieconsent.url.middleware')
], function() {
Route::get('cookies.js', [ScriptController::class, 'getCookieScript'])
->name('script.cookie');

Route::get('modal.js', [ScriptController::class, 'getModalScript'])
->name('script.modal');
Route::get('script', ScriptController::class)
->name('script');

Route::post('accept-all', AcceptAllController::class)
->name('accept.all');
Expand Down
20 changes: 9 additions & 11 deletions src/CookiesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,9 @@ protected function getConsentedScripts(bool $withDefault): string

protected function getDefaultScriptTag(): string
{
$locale = app()->currentLocale();

return '<script '
. 'src="' . route('cookieconsent.script.cookie') . '?id='
. md5(\filemtime(LCC_ROOT . '/dist/cookies.js')) . '&locale=' . $locale . '" '
. 'defer'
. '></script>'
. '<script '
. 'src="' . route('cookieconsent.script.modal') . '?id='
. md5(\filemtime(LCC_ROOT . '/dist/modal.js')) . '&locale=' . $locale . '" '
. 'src="' . route('cookieconsent.script') . '?id='
. md5(\filemtime(LCC_ROOT . '/dist/cookies.js')) . '" '
. 'defer'
. '></script>';
}
Expand All @@ -228,14 +221,20 @@ public function getNoticeMarkup(): string
$policy = route($policy);
}

$isReset = request()->routeIs('cookieconsent.reset');
$script = str_replace('{translations:1}', $this->getNoticeTranslations(), file_get_contents(LCC_ROOT . '/dist/modal.js'));

return view('cookie-consent::cookies', [
'cookies' => $this->registrar,
'policy' => $policy,
'script' => $script,
])->render();
}

protected function getNoticeTranslations() : string
{
return json_encode(__('cookieConsent::cookies.details'));
}

/**
* Output a single cookie consent action button.
*/
Expand Down Expand Up @@ -266,7 +265,6 @@ public function renderButton(string $action, ?string $label = null, array $attri

$attributes = collect($attributes)
->map(fn($value, $attribute) => $attribute . '="' . $value . '"')
->add('data-cookie-button')
->implode(' ');

return view('cookie-consent::button', [
Expand Down
18 changes: 1 addition & 17 deletions src/Http/Controllers/ScriptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,12 @@

class ScriptController
{

public function getCookieScript()
public function __invoke()
{
$content = str_replace('{config:1}', $this->generateConfig(), file_get_contents(LCC_ROOT . '/dist/cookies.js'));
return response($content)->header('Content-Type', 'application/javascript');
}

public function getModalScript(Request $request)
{
$locale = $request->query('locale', app()->getLocale());
app()->setLocale($locale);

$content = str_replace('{translations:1}', $this->getTranslations(), file_get_contents(LCC_ROOT . '/dist/modal.js'));

return response($content)->header('Content-Type', 'application/javascript');
}

protected function generateConfig(): string
{
return json_encode([
Expand All @@ -33,9 +22,4 @@ protected function generateConfig(): string
'reset' => route('cookieconsent.reset'),
]);
}

protected function getTranslations(): string
{
return json_encode(__('cookieConsent::cookies.details'));
}
}