Home

Awesome

Prerender Python Starlette

<p align="center"> <em>Starlette middleware for Prerender</em> </p>

build codecov Dependabot Status PyPI version


Documentation: <a href="https://BeeMyDesk.github.io/prerender-python-starlette/" target="_blank">https://BeeMyDesk.github.io/prerender-python-starlette/</a>

Source Code: <a href="https://github.com/BeeMyDesk/prerender-python-starlette" target="_blank">https://github.com/BeeMyDesk/prerender-python-starlette</a>


Introduction

Google, Facebook, Twitter, and Bing are constantly trying to view your website... but Google is the only crawler that executes a meaningful amount of JavaScript and Google even admits that they can execute JavaScript weeks after actually crawling. Prerender allows you to serve the full HTML of your website back to Google and other crawlers so that they don't have to execute any JavaScript. Google recommends using Prerender.io to prevent indexation issues on sites with large amounts of JavaScript.

Prerender is perfect for Angular SEO, React SEO, Vue SEO, and any other JavaScript framework.

This middleware intercepts requests to your Node.js website from crawlers, and then makes a call to the (external) Prerender Service to get the static HTML instead of the JavaScript for that page. That HTML is then returned to the crawler.

README of prerender_rails

This library is a Python implementation of a Prerender middleware for Starlette. It should work flawlessly with FastAPI and, probably, with any ASGI framework.

Installation

pip install prerender-python-starlette

Usage

from starlette.applications import Starlette
from starlette.middleware import Middleware
from prerender_python_starlette import PrerenderMiddleware

routes = ...

middleware = [
  Middleware(PrerenderMiddleware),
]

app = Starlette(routes=routes, middleware=middleware)

Parameters

Cache example

from starlette.applications import Starlette
from starlette.middleware import Middleware
from prerender_python_starlette import PrerenderMiddleware


async def before_render(request: Request) -> Optional[HTMLResponse]:
    cached_response = await cache.get(f"prerender:{request.url.path}")
    if cached_response:
        return HTMLResponse(cached_response)
    return None


async def after_render(
    request: Request, response: HTMLResponse, cached: bool
) -> None:
    if not cached:
        await cache.set(
            f"prerender:{request.url.path}", response.body.decode(response.charset)
        )


routes = ...

middleware = [
  Middleware(PrerenderMiddleware, before_render=before_render, after_render=after_render),
]

app = Starlette(routes=routes, middleware=middleware)

Development

Setup environement

You should have Pipenv installed. Then, you can install the dependencies with:

pipenv install --dev

After that, activate the virtual environment:

pipenv shell

Run unit tests

You can run all the tests with:

make test

Alternatively, you can run pytest yourself:

pytest

Format the code

Execute the following command to apply isort and black formatting:

make format

License

This project is licensed under the terms of the MIT license.