Skip to content
Merged
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
31 changes: 27 additions & 4 deletions packages/dev-server/src/Internal/HttpHandler.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in packages/dev-server/src/Internal/HttpHandler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

An error occurred during processing; checking has been aborted. The error message was: Undefined array key "scope_opener" in /home/runner/work/guides/guides/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/ReferencedNameHelper.php on line 333 The error originated in the SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly sniff on line 56.

declare(strict_types=1);

Expand Down Expand Up @@ -32,32 +32,32 @@
{
use CloseResponseTrait;

private ExtensionMimeTypeDetector $detector;

Check failure on line 35 in packages/dev-server/src/Internal/HttpHandler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Line indented incorrectly; expected 0 spaces, found 4

/** @param string|string[] $indexFile */
public function __construct(

Check failure on line 38 in packages/dev-server/src/Internal/HttpHandler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Line indented incorrectly; expected 0 spaces, found 4
private FlySystemAdapter $files,
private string|array $indexFile = 'index.html',
) {
$this->detector = new ExtensionMimeTypeDetector();
}

Check failure on line 43 in packages/dev-server/src/Internal/HttpHandler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Line indented incorrectly; expected 0 spaces, found 4

public function onOpen(ConnectionInterface $conn, RequestInterface|null $request = null): void

Check failure on line 45 in packages/dev-server/src/Internal/HttpHandler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Line indented incorrectly; expected 0 spaces, found 4
{
if ($request === null) {

Check failure on line 47 in packages/dev-server/src/Internal/HttpHandler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Line indented incorrectly; expected 4 spaces, found 8
$conn->close();

return;
}

Check failure on line 51 in packages/dev-server/src/Internal/HttpHandler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Line indented incorrectly; expected 4 spaces, found 8

$path = $request->getUri()->getPath();

// Remove leading slash and any route parameters
$requestPath = trim($path, '/');

if ($requestPath === '' || $this->files->isDirectory($requestPath)) {

Check failure on line 58 in packages/dev-server/src/Internal/HttpHandler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Line indented incorrectly; expected 4 spaces, found 8
if (is_array($this->indexFile)) {

Check failure on line 59 in packages/dev-server/src/Internal/HttpHandler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Line indented incorrectly; expected 8 spaces, found 12
foreach ($this->indexFile as $indexFile) {

Check failure on line 60 in packages/dev-server/src/Internal/HttpHandler.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Line indented incorrectly; expected 12 spaces, found 16
if ($this->files->has(trim($requestPath . '/' . $indexFile, '/'))) {
$requestPath = trim($requestPath . '/' . $indexFile, '/');
break;
Expand Down Expand Up @@ -104,15 +104,38 @@
//Read html and inject script before closing body tag
$injection = <<<'EOT'
<script>
const socket = new WebSocket('ws://' + window.location.host + '/ws');
const socket = new WebSocket((window.location.protocol === 'https:' ? 'wss://' : 'ws://') + window.location.host + '/ws');
socket.addEventListener('message', function (event) {
if (event.data === 'update') {
console.log('Reloading page due to server change...');
console.log('Reloading page due to server change... Stored scrollPosition: ' + window.scrollY);
sessionStorage.setItem('scrollPosition', window.scrollY);
sessionStorage.setItem('scrollURL', window.location.href);
window.location.reload();
}
});
</script>
EOT;

// Restore scroll position after page loads. Note that sessionStorage is
// browser-tab specific, so multiple instances should not affect each other.
window.addEventListener('load', function() {
const scrollPosition = sessionStorage.getItem('scrollPosition');
const scrollURL = sessionStorage.getItem('scrollURL');

// Only restore if we're on the same URL (hot reload, not navigation)
if (scrollPosition !== null && scrollURL === window.location.href) {
console.log('Prepare to restore scrollPosition to: ' + scrollPosition);

// Use setTimeout to override hash scrolling that happens after load
setTimeout(function() {
console.log('Restoring scrollPosition to: ' + scrollPosition);
window.scrollTo(0, parseInt(scrollPosition));
}, 10);
}

// Ensure local scroll position is reset, so other reloads to not carry state.
sessionStorage.removeItem('scrollPosition');
sessionStorage.removeItem('scrollURL');
});
</script>EOT;

return str_replace('</body>', $injection . '</body>', $html);
}
Expand All @@ -134,3 +157,3 @@
// TODO: Implement onMessage() method.
}
}
Loading