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"),
    ]
)