From c495dc330f6196b91cb6d1535facccab2892133d Mon Sep 17 00:00:00 2001 From: Jaapio Date: Sun, 14 Sep 2025 15:37:51 +0200 Subject: [PATCH 1/2] Allow multiple index files in dev server --- .../dev-server/src/Internal/HttpHandler.php | 28 ++++++++++++------- packages/dev-server/src/ServerFactory.php | 3 +- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/dev-server/src/Internal/HttpHandler.php b/packages/dev-server/src/Internal/HttpHandler.php index 3c06de8e5..158f9a397 100644 --- a/packages/dev-server/src/Internal/HttpHandler.php +++ b/packages/dev-server/src/Internal/HttpHandler.php @@ -23,6 +23,7 @@ use Ratchet\Http\HttpServerInterface; use Throwable; +use function is_array; use function str_replace; use function strlen; use function trim; @@ -33,9 +34,10 @@ final class HttpHandler implements HttpServerInterface private ExtensionMimeTypeDetector $detector; + /** @param string|string[] $indexFile */ public function __construct( private FlySystemAdapter $files, - private string $indexFile = 'index.html', + private string|array $indexFile = 'index.html', ) { $this->detector = new ExtensionMimeTypeDetector(); } @@ -53,13 +55,17 @@ public function onOpen(ConnectionInterface $conn, RequestInterface|null $request // Remove leading slash and any route parameters $requestPath = trim($path, '/'); - // For empty path (root) serve index.html - if ($requestPath === '') { - $requestPath = $this->indexFile; - } - - if ($this->files->isDirectory($requestPath)) { - $requestPath .= '/' . $this->indexFile; + if ($requestPath === '' || $this->files->isDirectory($requestPath)) { + if (is_array($this->indexFile)) { + foreach ($this->indexFile as $indexFile) { + if ($this->files->has(trim($requestPath . '/' . $indexFile, '/'))) { + $requestPath = trim($requestPath . '/' . $indexFile, '/'); + break; + } + } + } else { + $requestPath .= '/' . $this->indexFile; + } } if ($this->files->has($requestPath)) { @@ -81,7 +87,9 @@ public function onOpen(ConnectionInterface $conn, RequestInterface|null $request return; } - $content = '

404 - Page Not Found

'; + $content = '

404 - Page Not Found

+

Path ' . $requestPath . ' does not exist

+'; $headers = [ 'Content-Type' => 'text/html', 'Content-Length' => strlen($content), @@ -95,7 +103,7 @@ private function injectWebSocketClient(string $html): string { //Read html and inject script before closing body tag $injection = <<<'EOT' -