Skip to main content

Prerender Python Starlette

Starlette middleware for Prerender

build codecov Dependabot Status PyPI version


Documentation: https://BeeMyDesk.github.io/prerender-python-starlette/

Source Code: https://github.com/BeeMyDesk/prerender-python-starlette


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

  • prerender_service_url: URL of Prerender server. Defaults to PRERENDER_SERVICE_URL environment variable.
  • prerender_service_username: HTTP basic auth username of Prerender server. Defaults to PRERENDER_SERVICE_USERNAME environment variable.
  • prerender_service_password: HTTP basic auth password of Prerender server. Defaults to PRERENDER_SERVICE_PASSWORD environment variable.
  • prerender_service_token: Token set in X-Prerender-Token header. Defaults to PRERENDER_SERVICE_TOKEN environment variable.
  • crawler_user_agents: List of crawler user agents to intercept. Defaults to DEFAULT_CRAWLER_USER_AGENTS list.
  • extensions_to_ignore: List of file extensions to ignore. Defaults to DEFAULT_EXTENSIONS_TO_IGNORE list.
  • whitelist: List of path patterns to whitelist. Path not matching a pattern in the list won't be prerendered. Defaults to None.
  • blacklist: List of path patterns to blacklist. Path matching a pattern in the list won't be prerendered. Defaults to None.
  • before_render: Async function called before the prerendering. If it returns an HTMLResponse, it will be considered as cache and will bypass the call to the Prerender server. Defaults to None.
  • after_render: Async function called after the prerendering. Defaults to None.

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.

Metadata

Release files for prerender-python-starlette 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prerender-python-starlette 1.0.1
File Size Uploaded
prerender-python-starlette-1.0.1.tar.gz 22.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prerender-python-starlette 1.0.1
File Interpreter ABI Platform
prerender_python_starlette-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 29.3 kB

Release files / prerender-python-starlette-1.0.1.tar.gz

Download URL prerender-python-starlette-1.0.1.tar.gz
Size 22.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c83b1fd087a3db61ff29bc1b7a591e75e9d57ecd8210310691973c577afe6837
BLAKE2b-256 checksum
How to use checksums
9dc7e54641e105be374cb2d7f8455305728af3d15bc3529e636c223c5fde767f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.23.0

Release files / prerender_python_starlette-1.0.1-py3-none-any.whl

Download URL prerender_python_starlette-1.0.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a689b8001e26e295d13ff6eac325f29f7e8a85f758375198a10c4c36631e0c45
BLAKE2b-256 checksum
How to use checksums
bb18102be3c17426155badb09c57db323aed129ecf974b48419fecea4505556b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.23.0

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page