Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
FrameElement.reload()
without an initial [src]
attribute
The problem --- If a `<turbo-frame>` element is rendered without a `[src]` attribute, calls to `.reload()` will have no effect. If a `<turbo-frame>` is to be its own browsing context, it should be able to apply its current location (that is, it's owning document's current location) to its browsing context. For example, if a page has a `<turbo-frame>` element that contains text that's typically updated by a Web Socket-delivered `<turbo-stream>`, it might be useful to [gracefully degrade][] to periodic long-polling if that Web Socket connection were to fail. That might involve something like a `reload` Stimulus controller with a delay: ```html <script type="module"> import { Application, Controller } from "@hotwired/stimulus" const application = // boot up a Stimulus application application.register("reload", class extends Controller { static values = { frequency: Number } disconnect() { this.#clearInterval() } frequencyValueChanged(frequencyInMilliseconds) { this.#clearInterval() this.intervalID = setInterval(() => this.element.reload(), frequencyInMilliseconds) } #clearInterval() { if (this.intervalID) clearInterval(this.intervalID) } }) </script> <turbo-frame id="dynamic-data" data-controller="reload" data-reload-frequency-value="30000"> <h1>This data will refresh every 30 seconds</h1> </turbo-frame> ``` The solution --- When `FrameElement.reload()` is invoked, it delegates to its delegate instance's `sourceURLReloaded()` method. In all cases, `FrameElement.delegate` is an instance of `FrameController`. This commit extends the `FrameController.sourceURLReloaded()` implementation to set the element's `[src]` attribute to the element's [baseURI][] value, which sets off the usual attribute change listeners and `<turbo-frame>` navigation logic. [baseURI]: https://developer.mozilla.org/en-US/docs/Web/API/Node/baseURI [gracefully degrade]: https://developer.mozilla.org/en-US/docs/Glossary/Graceful_degradation
- Loading branch information