Skip to content

Server Push

Starlette includes support for HTTP/2 and HTTP/3 server push, enabling resources to be pushed to clients to speed up page load times.

Request.send_push_promise

Used to initiate server push for resources. If server push is not available, this method does nothing.

Signed by: send_push_promise(path)

  • path - A string representing the path to the resource.

    from starlette.applications import Starlette from starlette.responses import HTMLResponse from starlette.routing import Route, Mount from starlette.staticfiles import StaticFiles

    async def homepage(request): """ Homepage which uses server push to deliver the stylesheet. """ await request.send_push_promise("/static/style.css") return HTMLResponse( '' )

    routes = [ Route("/", endpoint=homepage), Mount("/static", StaticFiles(directory="static"), name="static") ]

    app = Starlette(routes=routes)