Redesign: switch to HotReloadMiddleware#31
Conversation
3daf50a to
70758dc
Compare
70758dc to
34dd51d
Compare
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #31 +/- ##
============================================
- Coverage 100.00% 32.47% -67.53%
============================================
Files 10 9 -1
Lines 196 271 +75
============================================
- Hits 196 88 -108
- Misses 0 183 +183 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
b166dd8 to
a22bcb6
Compare
a22bcb6 to
05a1f17
Compare
6ea8539 to
ad4aedb
Compare
|
django-browser-reload uses Server-sent events rather than WebSocket. It's true we don't need WebSocket, as we only do unidirectional server->client messaging (the browser never needs to send anything to the server). But WebSocket seems to have an even better browser support than EventSource. So let's stick to WS. |
|
In case this might be useful… |
|
How would that potentially be useful here? |
|
For one of the TODO items here…
|
Daphne doesn't have a reload option, and for some reason it doesn't work for Hypercorn. I still didn't find why... 👀 |
|
This middleware PR allowed me to get client side auto reloading working with Gradio's FastAPI mount_gradio_app function. I'm connected to the project through ssh in vscode which auto forwards the local port incrementing it by one. I manually changed the client.js to reflect that by const ws = new WebSocket("ws://127.0.0.1:8001/arel"); which got it connected. If I wanted to clean it up and make it available, how do you recommend I add arel? Right now I'm just using the loose files from this branch. Thanks. |
|
@florimondmanca How can I help to get this merged? |
|
@Kludex Hey, thanks, help is appreciated. We’d need to review the TODO items I left in the PR description. This should be some manual testing only, from what I recall. Then I would love a proper review of the PR, checking if things look correct, tested, documented, and functional, of course. Also, CI looks like it didn’t pass last time it ran… |
1 similar comment
|
@Kludex Hey, thanks, help is appreciated. We’d need to review the TODO items I left in the PR description. This should be some manual testing only, from what I recall. Then I would love a proper review of the PR, checking if things look correct, tested, documented, and functional, of course. Also, CI looks like it didn’t pass last time it ran… |
|
Perfect. I'll check it. Thanks 🙏 |
| try: | ||
| start = body.index(b"</body>") | ||
| except ValueError: | ||
| await send(message) |
There was a problem hiding this comment.
Content length is here updated with the script, but script is not attached, creating a RuntimeError in Starlette (RuntimeError: Response content shorter than content length).
This check should be moved to the conditional above: if message["type"] == "http.response.start": ..., e.g. like this on L185:
if b"</body>" not in message["body"]:
await send(message)
return
Without Arel, on Uvicorn I had to manually include css and html (jinja). With Arel I still want server restart for jinja templates but not on css. vs Trying to think through how this interplays with #37. Given that #37 takes reloading static assets down to milliseconds, I think you definitely don't want to just rely on letting "arel [...] just handle "reload on reconnect" Anecdotally, about a month ago, my team was really confused about the Arel reload message on the server side, thinking it was coming from |
This PR builds upon #25 and introduces a
HotReloadMiddleware.The main change is that most things are now fully transparent. The middleware responds to
lifespanto do its startup/shutdown on its own, and it responds towebsocketrequests on its own as well. No need to register any event handlers or routes externally anymore.As the middleware basically does all the work now,
arel.HotReloadbecomes obsolete.A revamp of the README ensues this redesign...
TODO:
uvicorn --reload --reload-dir server/templates --reload-dir custom_directory_etcandarelwould just handle "reload on reconnect" (if the above is correct).exampleapp. Ideally we'd setup ad-hoc Starlette apps and test against them. Unfortunately HTTPX doesn't do WebSocket yet. So I guess we'll have to spin up actual servers.