Package: prerender-python-starlette
Prerender Python StarletteStarlette middleware for PrerenderDocumentation:https://BeeMyDesk.github.io/prerender-python-starlette/Source Code:https://github.com/BeeMyDesk/prerender-python-starletteIntroductionGoogle, 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.ioto 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 ofprerender_railsThis library is a Python implementation of a Prerender middleware forStarlette. It should work flawlessly withFastAPIand, probably, with any ASGI framework.Installationpipinstallprerender-python-starletteUsagefromstarlette.applicationsimportStarlettefromstarlette.middlewareimportMiddlewarefromprerender_python_starletteimportPrerenderMiddlewareroutes=...middleware=[Middleware(PrerenderMiddleware),]app=Starlette(routes=routes,middleware=middleware)Parametersprerender_service_url: URL of Prerender server. Defaults toPRERENDER_SERVICE_URLenvironment variable.prerender_service_username: HTTP basic auth username of Prerender server. Defaults toPRERENDER_SERVICE_USERNAMEenvironment variable.prerender_service_password: HTTP basic auth password of Prerender server. Defaults toPRERENDER_SERVICE_PASSWORDenvironment variable.prerender_service_token: Token set inX-Prerender-Tokenheader. Defaults toPRERENDER_SERVICE_TOKENenvironment variable.crawler_user_agents: List of crawler user agents to intercept. Defaults toDEFAULT_CRAWLER_USER_AGENTSlist.extensions_to_ignore: List of file extensions to ignore. Defaults toDEFAULT_EXTENSIONS_TO_IGNORElist.whitelist: List of path patterns to whitelist. Path not matching a pattern in the list won't be prerendered. Defaults toNone.blacklist: List of path patterns to blacklist. Path matching a pattern in the list won't be prerendered. Defaults toNone.before_render: Async function called before the prerendering. If it returns anHTMLResponse, it will be considered as cache and will bypass the call to the Prerender server. Defaults toNone.after_render: Async function called after the prerendering. Defaults toNone.Cache examplefromstarlette.applicationsimportStarlettefromstarlette.middlewareimportMiddlewarefromprerender_python_starletteimportPrerenderMiddlewareasyncdefbefore_render(request:Request)->Optional[HTMLResponse]:cached_response=awaitcache.get(f"prerender:{request.url.path}")ifcached_response:returnHTMLResponse(cached_response)returnNoneasyncdefafter_render(request:Request,response:HTMLResponse,cached:bool)->None:ifnotcached:awaitcache.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)DevelopmentSetup environementYou should havePipenvinstalled. Then, you can install the dependencies with:pipenvinstall--devAfter that, activate the virtual environment:pipenvshellRun unit testsYou can run all the tests with:maketestAlternatively, you can runpytestyourself:pytestFormat the codeExecute the following command to applyisortandblackformatting:makeformatLicenseThis project is licensed under the terms of the MIT license.