Skip to content

Commit c495dc3

Browse files
committed
Allow multiple index files in dev server
1 parent 4077508 commit c495dc3

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

packages/dev-server/src/Internal/HttpHandler.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Ratchet\Http\HttpServerInterface;
2424
use Throwable;
2525

26+
use function is_array;
2627
use function str_replace;
2728
use function strlen;
2829
use function trim;
@@ -33,9 +34,10 @@ final class HttpHandler implements HttpServerInterface
3334

3435
private ExtensionMimeTypeDetector $detector;
3536

37+
/** @param string|string[] $indexFile */
3638
public function __construct(
3739
private FlySystemAdapter $files,
38-
private string $indexFile = 'index.html',
40+
private string|array $indexFile = 'index.html',
3941
) {
4042
$this->detector = new ExtensionMimeTypeDetector();
4143
}
@@ -53,13 +55,17 @@ public function onOpen(ConnectionInterface $conn, RequestInterface|null $request
5355
// Remove leading slash and any route parameters
5456
$requestPath = trim($path, '/');
5557

56-
// For empty path (root) serve index.html
57-
if ($requestPath === '') {
58-
$requestPath = $this->indexFile;
59-
}
60-
61-
if ($this->files->isDirectory($requestPath)) {
62-
$requestPath .= '/' . $this->indexFile;
58+
if ($requestPath === '' || $this->files->isDirectory($requestPath)) {
59+
if (is_array($this->indexFile)) {
60+
foreach ($this->indexFile as $indexFile) {
61+
if ($this->files->has(trim($requestPath . '/' . $indexFile, '/'))) {
62+
$requestPath = trim($requestPath . '/' . $indexFile, '/');
63+
break;
64+
}
65+
}
66+
} else {
67+
$requestPath .= '/' . $this->indexFile;
68+
}
6369
}
6470

6571
if ($this->files->has($requestPath)) {
@@ -81,7 +87,9 @@ public function onOpen(ConnectionInterface $conn, RequestInterface|null $request
8187
return;
8288
}
8389

84-
$content = '<!DOCTYPE html><html><body><h1>404 - Page Not Found</h1></body></html>';
90+
$content = '<!DOCTYPE html><html><body><h1>404 - Page Not Found</h1>
91+
<p>Path ' . $requestPath . ' does not exist</p>
92+
</body></html>';
8593
$headers = [
8694
'Content-Type' => 'text/html',
8795
'Content-Length' => strlen($content),
@@ -95,7 +103,7 @@ private function injectWebSocketClient(string $html): string
95103
{
96104
//Read html and inject script before closing body tag
97105
$injection = <<<'EOT'
98-
<script>
106+
<script>
99107
const socket = new WebSocket('ws://' + window.location.host + '/ws');
100108
socket.addEventListener('message', function (event) {
101109
if (event.data === 'update') {

packages/dev-server/src/ServerFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ public function __construct(
2929
) {
3030
}
3131

32+
/** @param string|string[] $indexFile */
3233
public function createDevServer(
3334
string $soureDirectory,
3435
FlySystemAdapter $files,
3536
string $host,
3637
string $listen,
3738
int $port,
38-
string $indexFile = 'index.html',
39+
string|array $indexFile = 'index.html',
3940
): Server {
4041
$httpHandler = new HttpHandler($files, $indexFile);
4142
$wsServer = new WebSocketHandler($this->logger);

0 commit comments

Comments
 (0)