Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Two basic approaches to this, both require having your server inject a <script> tag into your page... The old-school "simple" script, which uses setInterval() to periodically poll your serv...
#1: Initial revision
Two basic approaches to this, both require having your server inject a `<script>` tag into your page... 1. The old-school "simple" script, which uses `setInterval()` to periodically poll your server using `XMLHttpRequest.open()` or `fetch()` to query for changes, either by ETag header or *304 Not Modified* status code (generation of which you must build into your HTTP server). If the response from the server is *not* the same ETag as the last time you requested it, or the server sends back something *other* than 304, call `window.location.reload()`. 2. The newer-style "complex" script, which establishes either a `WebSocket` or an `EventSource` to your HTTP server, and waits for messages -- the event handler function for that would call `window.location.reload()`. In your HTTP server code, you *send* a message on that channel whenever you want the client to reload (i.e. when you detect the change to your content). `EventSource` is *possibly* the cleanest approach (see https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events or https://dev.to/philip_zhang_854092d88473/mastering-server-sent-events-sse-with-python-and-go-for-real-time-data-streaming-38bf for ideas), but the old-school approach is probably *fine* for a local dev instance, where the load can be controlled. In any case, if you intend to expose your HTTP server to the public, you should make it configurable on the server-side, whether the client script is actually injected. You probably don't want hundreds of pings to your server from the outside world, every time someone browses your pages.
