Skip to content

Server Push

Lilya incorporates support for HTTP/2 and HTTP/3 server push, enabling the proactive delivery of resources to the client for accelerating page load times.

The method

This method is employed to initiate a server push for a resource. If server push functionality is not available, this method takes no action.

  • path: A string specifying the path of the resource.
from lilya.apps import Lilya
from lilya.requests import Request
from lilya.responses import HTMLResponse
from lilya.routing import Include, Path
from lilya.staticfiles import StaticFiles


async def homepage(request: Request):
    """
    Handler featuring server push for delivering the stylesheet.
    """
    await request.send_push_promise("/static/app.css")
    return HTMLResponse(
        '<html><head><link rel="stylesheet" href="/static/app.css"/></head></html>'
    )


app = Lilya(
    routes=[
        Path("/", homepage),
        Include("/static", StaticFiles(directory="static"), name="static"),
    ]
)

Availability and checks

Server push depends on ASGI server support for the http.response.push extension.

You can check support from request/connection scope data with:

  • request.is_server_push

If support is unavailable, await request.send_push_promise(...) is safely ignored.

Practical notes

  • Use push only for assets that are very likely needed immediately.
  • Avoid pushing large assets blindly.
  • Measure real browser behavior before relying on push for performance gains.

See also

  • Request for send_push_promise() and request extension checks.
  • StaticFiles for static asset serving behavior.