From b594bab351ce02ac52cd0ba8d04d55333d5bae43 Mon Sep 17 00:00:00 2001 From: George King Date: Mon, 8 Sep 2025 13:52:08 +0800 Subject: [PATCH 1/3] fix: 404 when refreshing non-homepage routes --- .../Http/Handler/FrontendRequest.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/src/Interfaces/Http/Handler/FrontendRequest.php b/app/src/Interfaces/Http/Handler/FrontendRequest.php index 3321e97..3b10b36 100644 --- a/app/src/Interfaces/Http/Handler/FrontendRequest.php +++ b/app/src/Interfaces/Http/Handler/FrontendRequest.php @@ -9,7 +9,6 @@ use Nyholm\Psr7\Response; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Spiral\Http\Exception\ClientException\NotFoundException; final class FrontendRequest implements HandlerInterface { @@ -42,7 +41,8 @@ public function handle(ServerRequestInterface $request, \Closure $next): Respons if (!isset($this->fileContent[$path])) { if (!file_exists($path)) { - throw new NotFoundException(\sprintf('File "%s" not found', $path)); + // Similar to Nginx's retry-files functionality. + $path = $this->publicPath . '/index.html'; } $body = \file_get_contents($path); @@ -67,7 +67,20 @@ private function isValidRequest(ServerRequestInterface $request): bool { $path = $request->getUri()->getPath(); - return $path === '/' + $frontendRoutes = [ + '/', + '/ray', + '/smtp', + '/sentry', + '/monolog', + '/profiler', + '/settings', + '/var-dump', + '/http-dump', + '/inspector', + ]; + + return in_array($path, $frontendRoutes) || \str_starts_with($path, '/src/') || \str_starts_with($path, '/assets/') || $path === '/favicon/favicon.ico' From 932008dda9ab3e2390de47cdf99d128f815f34f5 Mon Sep 17 00:00:00 2001 From: George King Date: Thu, 11 Sep 2025 19:21:40 +0800 Subject: [PATCH 2/3] chore: simplify redundant logic --- .../Http/Handler/FrontendRequest.php | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/app/src/Interfaces/Http/Handler/FrontendRequest.php b/app/src/Interfaces/Http/Handler/FrontendRequest.php index 3b10b36..c78c609 100644 --- a/app/src/Interfaces/Http/Handler/FrontendRequest.php +++ b/app/src/Interfaces/Http/Handler/FrontendRequest.php @@ -9,6 +9,7 @@ use Nyholm\Psr7\Response; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Spiral\Http\Exception\ClientException\NotFoundException; final class FrontendRequest implements HandlerInterface { @@ -35,14 +36,19 @@ public function handle(ServerRequestInterface $request, \Closure $next): Respons if ($path === '/') { $path = '/index.html'; + } elseif ( + !\str_starts_with($path, '/api') + && !\str_starts_with($path, '/assets') + && !\str_contains($path, '/src') + ) { + $path = '/index.html'; } $path = $this->publicPath . $path; if (!isset($this->fileContent[$path])) { if (!file_exists($path)) { - // Similar to Nginx's retry-files functionality. - $path = $this->publicPath . '/index.html'; + throw new NotFoundException(\sprintf('File "%s" not found', $path)); } $body = \file_get_contents($path); @@ -67,23 +73,11 @@ private function isValidRequest(ServerRequestInterface $request): bool { $path = $request->getUri()->getPath(); - $frontendRoutes = [ - '/', - '/ray', - '/smtp', - '/sentry', - '/monolog', - '/profiler', - '/settings', - '/var-dump', - '/http-dump', - '/inspector', - ]; - - return in_array($path, $frontendRoutes) + return $path === '/' || \str_starts_with($path, '/src/') || \str_starts_with($path, '/assets/') || $path === '/favicon/favicon.ico' - || $path === '/bg.jpg'; + || $path === '/bg.jpg' + || !\str_starts_with($path, '/api'); } } From 72c61413a371825150f5711f7d40b333be891388 Mon Sep 17 00:00:00 2001 From: George King Date: Thu, 11 Sep 2025 21:34:01 +0800 Subject: [PATCH 3/3] fix: frontend page error due to route conflict with backend --- .../Inspector/Interfaces/Http/Handler/EventHandler.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/modules/Inspector/Interfaces/Http/Handler/EventHandler.php b/app/modules/Inspector/Interfaces/Http/Handler/EventHandler.php index d82b7c3..5e71c4b 100644 --- a/app/modules/Inspector/Interfaces/Http/Handler/EventHandler.php +++ b/app/modules/Inspector/Interfaces/Http/Handler/EventHandler.php @@ -72,7 +72,10 @@ private function listenEvent(ServerRequestInterface $request): ?EventType if ( $request->hasHeader('X-Inspector-Key') || $request->hasHeader('X-Inspector-Version') - || \str_ends_with((string) $request->getUri(), 'inspector') + || ( + !\str_starts_with($request->getUri()->getPath(), '/inspector') + && \str_ends_with((string) $request->getUri(), 'inspector') + ) ) { return new EventType(type: 'inspector'); }