Skip to content

Commit a9ad07a

Browse files
Fix watch hot-reload WebSocket to use window.location.host instead of hardcoded localhost
The injected WebSocket URI was hardcoded as ws://localhost:<port>/websocket, which breaks in GitHub Codespaces, behind reverse proxies, and over HTTPS. Now uses window.location.host (which includes the correct hostname and port) with the appropriate ws:/wss: protocol to match the page's scheme. Removes the now-unused port parameter from generateWatchScript. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e8d7a4e commit a9ad07a

2 files changed

Lines changed: 6 additions & 7 deletions

File tree

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Added
66
* Add `--root` option to `fsdocs watch` to override the root URL for generated pages. Useful for serving docs via GitHub Codespaces, reverse proxies, or other remote hosting where `localhost` URLs are inaccessible. E.g. `fsdocs watch --root /` or `fsdocs watch --root https://example.com/docs/`. When not set, defaults to `http://localhost:<port>/` as before. [#924](https://github.com/fsprojects/FSharp.Formatting/issues/924)
7+
* Fix `fsdocs watch` hot-reload WebSocket to connect using the page's actual host (`window.location.host`) instead of a hardcoded `localhost:<port>`, so hot-reload works correctly in GitHub Codespaces, behind reverse proxies, and over HTTPS. [#924](https://github.com/fsprojects/FSharp.Formatting/issues/924)
78
* Search dialog now auto-focuses the search input when opened, clears on close, and can be triggered with `Ctrl+K` / `Cmd+K` in addition to `/`.
89
* Add `dotnet fsdocs convert` command to convert a single `.md`, `.fsx`, or `.ipynb` file to HTML (or another output format) without building a full documentation site. [#811](https://github.com/fsprojects/FSharp.Formatting/issues/811)
910
* `fsdocs convert` now accepts the input file as a positional argument (e.g. `fsdocs convert notebook.ipynb -o notebook.html`). [#1019](https://github.com/fsprojects/FSharp.Formatting/pull/1019)

src/fsdocs-tool/BuildCommand.fs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -819,11 +819,11 @@ module Serve =
819819
let refreshEvent = FSharp.Control.Event<string>()
820820

821821
/// generate the script to inject into html to enable hot reload during development
822-
let generateWatchScript (port: int) =
823-
let tag =
824-
"""
822+
let generateWatchScript () =
823+
"""
825824
<script type="text/javascript">
826-
var wsUri = "ws://localhost:{{PORT}}/websocket";
825+
var wsProtocol = window.location.protocol === "https:" ? "wss:" : "ws:";
826+
var wsUri = wsProtocol + "//" + window.location.host + "/websocket";
827827
function init()
828828
{
829829
websocket = new WebSocket(wsUri);
@@ -850,8 +850,6 @@ module Serve =
850850
</script>
851851
"""
852852

853-
tag.Replace("{{PORT}}", string<int> port)
854-
855853
let connectedClients = ConcurrentDictionary<WebSocket, unit>()
856854

857855
let socketHandler (webSocket: WebSocket) (context: HttpContext) =
@@ -1879,7 +1877,7 @@ type CoreBuildOptions(watch) =
18791877
let getLatestWatchScript () =
18801878
if watch then
18811879
// if running in watch mode, inject hot reload script
1882-
[ ParamKeys.``fsdocs-watch-script``, Serve.generateWatchScript this.port_option ]
1880+
[ ParamKeys.``fsdocs-watch-script``, Serve.generateWatchScript () ]
18831881
else
18841882
// otherwise, inject empty replacement string
18851883
[ ParamKeys.``fsdocs-watch-script``, "" ]

0 commit comments

Comments
 (0)