2019-09-01 22:36:19 +00:00
|
|
|
|
|
|
|
Starlette includes support for HTTP/2 and HTTP/3 server push, making it
|
|
|
|
possible to push resources to the client to speed up page load times.
|
|
|
|
|
|
|
|
### `Request.send_push_promise`
|
|
|
|
|
|
|
|
Used to initiate a server push for a resource. If server push is not available
|
|
|
|
this method does nothing.
|
|
|
|
|
|
|
|
Signature: `send_push_promise(path)`
|
|
|
|
|
|
|
|
* `path` - A string denoting the path of the resource.
|
|
|
|
|
|
|
|
```python
|
|
|
|
from starlette.applications import Starlette
|
|
|
|
from starlette.responses import HTMLResponse
|
2019-11-13 12:25:18 +00:00
|
|
|
from starlette.routing import Route, Mount
|
2019-09-01 22:36:19 +00:00
|
|
|
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(
|
|
|
|
'<html><head><link rel="stylesheet" href="/static/style.css"/></head></html>'
|
|
|
|
)
|
|
|
|
|
2019-11-13 12:25:18 +00:00
|
|
|
routes = [
|
|
|
|
Route("/", endpoint=homepage),
|
|
|
|
Mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
]
|
2019-09-01 22:36:19 +00:00
|
|
|
|
2019-11-13 12:25:18 +00:00
|
|
|
app = Starlette(routes=routes)
|
2019-09-01 22:36:19 +00:00
|
|
|
```
|