From 89a0f50f5da1eb85794e39408a683ecb60b47b58 Mon Sep 17 00:00:00 2001 From: Sander van Hooft <7265703+sandervanhooft@users.noreply.github.com> Date: Tue, 3 Nov 2026 12:59:05 +0100 Subject: [PATCH] Start a session on not found pages so Livewire gets a CSRF token A request to an unknown URL never matches a route, so it is rendered outside the web middleware group. Without StartSession, csrf_token() is empty and the layout's livewireScriptConfig ships "csrf":"". Since Livewire v4 this throws "Livewire: No CSRF token detected" on every 404 page, and the Livewire components on it (such as the spotlight search) can't make requests. A fallback route that aborts with a 404 runs inside the web group, so the 404 page gets a session and CSRF token, plus the same localization and roadmap password protection as every other page. --- routes/web.php | 2 ++ tests/Feature/NotFoundPageTest.php | 8 ++++++++ 2 files changed, 10 insertions(+) create mode 100644 tests/Feature/NotFoundPageTest.php diff --git a/routes/web.php b/routes/web.php index 604d0888..601bbb8a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -89,3 +89,5 @@ Route::get('/sitemap.xml', [SitemapController::class, 'index'])->name('sitemap'); Route::get('/sitemap-projects.xml', [SitemapController::class, 'projects'])->name('sitemap.projects'); Route::get('/sitemap-items.xml', [SitemapController::class, 'items'])->name('sitemap.items'); + +Route::fallback(fn () => abort(404)); diff --git a/tests/Feature/NotFoundPageTest.php b/tests/Feature/NotFoundPageTest.php new file mode 100644 index 00000000..c4673913 --- /dev/null +++ b/tests/Feature/NotFoundPageTest.php @@ -0,0 +1,8 @@ +get('/this-page-does-not-exist'); + + $response->assertNotFound(); + expect($response->getContent())->toMatch('/"csrf":"[A-Za-z0-9]{40}"/'); +});