All Projects → chasefinch → amp-renderer

chasefinch / amp-renderer

Licence: Apache-2.0 License
Server-side rendering for AMP in Python

Programming Languages

python
139335 projects - #7 most used programming language
HTML
75241 projects
Makefile
30231 projects

Projects that are alternatives of or similar to amp-renderer

amp-converter
A PHP library to convert HTML articles, blog posts or similar content to AMP (Accelerated Mobile Pages).
Stars: ✭ 59 (+321.43%)
Mutual labels:  amp, amp-html
wordpress-amp-theme
A free WordPress theme for blogging built entirely on Google AMP
Stars: ✭ 22 (+57.14%)
Mutual labels:  amp, amp-html
react-amp-components
A (hopefully) simple way to render AMP components via React on the server.
Stars: ✭ 30 (+114.29%)
Mutual labels:  amp, amp-html
ampjucks
Boilerplate and base project to create static websites with AMP, Nunjucks and Gulp
Stars: ✭ 18 (+28.57%)
Mutual labels:  amp, amp-html
amp-browser
AMP Browser
Stars: ✭ 39 (+178.57%)
Mutual labels:  amp, amp-html
amp-react-renderer-plugin
⚡Plugin makes it painless to create React component based AMP page⚡
Stars: ✭ 29 (+107.14%)
Mutual labels:  amp, amp-html
amp-surface
⚡ AMP Surface CSS Framework
Stars: ✭ 26 (+85.71%)
Mutual labels:  amp, amp-html
amp-mui
⚡ AMP MUI CSS Framework
Stars: ✭ 18 (+28.57%)
Mutual labels:  amp, amp-html
gomodest-starter
A complex SAAS starter kit using Go, the html/template package, and sprinkles of javascript.
Stars: ✭ 68 (+385.71%)
Mutual labels:  server-side-rendering
SSR-React-Using-Serverless
SSR-React Using Serverless(aws)
Stars: ✭ 34 (+142.86%)
Mutual labels:  server-side-rendering
ssr-starter-pack
Moved to https://github.com/Brigad/ssr-starter-pack
Stars: ✭ 12 (-14.29%)
Mutual labels:  server-side-rendering
ficus
Scala-friendly companion to Typesafe config
Stars: ✭ 321 (+2192.86%)
Mutual labels:  amp
ng1-server
Node.js server side library for pre-rendering complex AngularJS app. Supports caching and URL rules.
Stars: ✭ 43 (+207.14%)
Mutual labels:  server-side-rendering
fastify-angular-universal
Angular Universal integration to Fastify for rendering Angular apps on the server
Stars: ✭ 20 (+42.86%)
Mutual labels:  server-side-rendering
next-utils
🥩 🍳 A set of Next.js HoC utilities to make your life easier
Stars: ✭ 30 (+114.29%)
Mutual labels:  server-side-rendering
medellinjs
MedellinJS
Stars: ✭ 61 (+335.71%)
Mutual labels:  server-side-rendering
awesome-milligram
A curated list of Milligram resources.
Stars: ✭ 76 (+442.86%)
Mutual labels:  amp
web-starter-kit
An opinionated starter kit with styled-system, graphql-hooks, mobx and nextjs (PWA)
Stars: ✭ 17 (+21.43%)
Mutual labels:  server-side-rendering
nx-ng-nest-universal
Nx Workspace with a seperated Nest App for Angular Universal SSR.
Stars: ✭ 32 (+128.57%)
Mutual labels:  server-side-rendering
server-authentication-next.js
No description or website provided.
Stars: ✭ 103 (+635.71%)
Mutual labels:  server-side-rendering

AMP Renderer

Python 3.6+ Build Status Coverage

Unofficial Python port of server-side rendering from AMP Optimizer. Supports Python 3.6 and above.

AMP Renderer performs the following optimizations:

  1. Inject the specific layout markup into each AMP element
  2. Insert the AMP runtime styles into the document
  3. Remove the AMP boilerplate styles, if possible
  4. Mark the document as “transformed” with the appropriate tags on the html element
  5. Insert img tags for images with the data-hero attribute

It also makes these formatting updates:

  1. Remove empty class and style tags for AMP HTML elements
  2. Convert tag names and attribute names to lowercase
  3. Convert numerical attribute values to strings
  4. Use double quotes ("") for attributes, and escape double quotes inside attribute values
  5. Remove whitespace between html attributes
  6. If desired, removes comments (disabled by default)
  7. If desired, trims whitespace around HTML attribute values (disabled by default, and not always a good idea)

AMPRenderer can be used on a block of arbitrary HTML, but when used on a full document, it will insert the AMP runtime styles and, if possible, remove the AMP boilerplate styles.

Boilerplate styles can be removed except in these cases:

  • An AMP element uses an unsupported value for the layout attribute
  • amp-audio is used
  • There is at least one amp-experiment active
  • Transformation fails for one or more elements due to an invalid attribute value for media, sizes, or heights
  • Any render-delaying extension is used. Currently this means:
    • amp-dynamic-css-classes
    • amp-story

If boilerplate styles can’t be removed, the attribute no_boilerplate will be set to True after calling render; otherwise it will be False. Until render runs, the no_boilerplate attribute isn’t set at all.

Usage

If using Django, you can use the Django AMP Renderer middleware.

Otherwise, install via PyPI:

pip install amp-renderer

Minimal usage:

from amp_renderer import AMPRenderer

...

RUNTIME_VERSION = "012345678" /* Current AMP runtime version number */
RUNTIME_STYLES = "..." /* Current contents of https://cdn.ampproject.org/v0.css */

renderer = AMPRenderer(
	runtime_version=RUNTIME_VERSION,
	runtime_styles=RUNTIME_STYLES)

original_html = """
    <!doctype html>
    <html ⚡>
      ...
    </html>
"""

result = renderer.render(original_html)

print(result)

Remove comments and/or trim attributes:

renderer.should_strip_comments = True
renderer.should_trim_attributes = True
result = renderer.render(original_html)

print(result)

The AMPRenderer class inherits from HTMLParser, and can be similarly extended.

Testing, etc.

Install development requirements:

make install

Sort imports (Requires Python >= 3.8):

make format

Lint (Requires Python >= 3.8):

make lint

Test:

make test

Discussion

There are still some aspects of the official AMP Optimizer implementation that haven’t been addressed yet. PRs welcome.

General

  • Tested against AMP Optimizer’s ServerSideRendering spec
  • Automatic runtime version management

Dynamic attributes

  • Support sizes, media, and heights via CSS injection
  • Group CSS injections for media attributes by shared media queries to reduce necessary bytes
  • Support percent values in heights
  • Warn or fail if CSS injection puts the amp-custom element over the byte limit

Hero Images

  • Inject img tag for amp-imgs with the data-hero attribute
  • Enforce 2-image limit on data-hero
  • Autodetect hero images
  • Support hero image functionality for amp-iframe, amp-video, and amp-video-iframe

Performance

The Python AMP Renderer does not insert preload links into the head of the DOM object for hero images; This can be done by hand for more control over the critical path.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].