fasthtml
fasthtml copied to clipboard
[BUG] Adding BackgroundTask to Redirect results in 200 response.
Describe the bug
Adding BackgroundTask to Redirect response results in 200 status code.
Minimal Reproducible Example
app = FastHTML()
cli = TestClient(app, follow_redirects=False)
@app.get("/background")
def background_task():
def long_running_task():
time.sleep(0.1)
print("Background task completed!")
return Redirect("/"), BackgroundTask(long_running_task)
r = cli.get("/background")
print(r)
print(r.text)
Output:
Background task completed!
<Response [200 OK]>
<!doctype html>
<html>
<head>
<title>FastHTML page</title>
<meta charset="utf-8">
...
<body><fasthtml.core.Redirect object at 0x103863cb0></body>
</html>
Expected behavior Expect response. with 303 status code
Environment Information Please provide the following version information:
- fastlite version: 0.1.3
- fastcore version: 1.8.1
- fasthtml version: 0.12.5
Confirmation Please confirm the following:
- [x] I have read the FAQ (https://docs.fastht.ml/explains/faq.html)
- [x] I have provided a minimal reproducible example
- [x] I have included the versions of fastlite, fastcore, and fasthtml
- [x] I understand that this is a volunteer open source project with no commercial support.
A workaround I use instead of FastHTML Redirect return Starlette responses:
def redirect(req: Request, loc: str, background: BackgroundTask | None = None):
if "hx-request" in req.headers:
return Response(headers={"HX-Redirect": loc}, background=background)
else:
return RedirectResponse(loc, status_code=303, background=background)
e.g.
@app.get("/background")
def background_task(req: Request):
...
return redirect(req, "/", background=long_running_task)