Socket
Book a DemoInstallSign in
Socket

react-amp-components

Package Overview
Dependencies
Maintainers
5
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-amp-components

React AMP Components

0.8.2
latest
Source
npmnpm
Version published
Maintainers
5
Created
Source

react-amp-components

npm version Greenkeeper badge

A (hopefully) simple way to render AMP components via React on the server.

It uses react-helmet for managing all the required meta and script tags.

That means that you can just import and use the AMP component you need, without having to care about adding the additional required script tags.

Warning

This project is still in its early infancy. Only a few components exist, but we will add more. Feel free to contribute :rocket:

Installation

Install it from npm:

yarn add react-amp-components

or:

npm install --save react-amp-components

Usage

Import and use the AMP component you would like to use:

// MyComponent.js
import {
  Layout, // Renders the boilerplate & delegates the passed children to Helmet
  Sidebar
} from "react-amp-components";

class MyComponent extends React.Component {
  render() {
    <div>
      <Layout>
        <title>{title}</title>
        <link rel="canonical" href="http://canonical.url" />
        <script type="application/ld+json">{`
          {
            "@context": "http://schema.org",
            "@type": "Product"
          }
        `}</script>
      </Layout>
      <Sidebar id="my-sidebar" layout="hidden">
        <p>Here is some sidebar content</p>
      </Sidebar>
    </div>;
  }
}

Then on the server you generate the final HTML output, and the content for the head:

// Server.js
const React = require("react");
const ReactDOMServer = require("react-dom/server");
const { Helmet } = require("react-amp-components");

const MyComponent = require("./MyComponent.js");

const content = ReactDOMServer.renderToStaticMarkup(
  React.createElement(MyComponent)
);
const meta = Helmet.renderStaticClean();

Which you can then interpolate:

const finalHTML = `
  <!doctype html>
  <html ${meta.htmlAttributes}>
    <head>
      ${meta.title}
      ${meta.meta}
      ${meta.link}
      ${meta.script}
      ${meta.noscript}
      ${meta.style}
    </head>
    <body ${meta.bodyAttributes}>
      ${content}
    </body>
  </html>
`;

AMP Components implemented

amp-sidebar

amp-img

amp-analytics

amp-accordion

amp-bind

amp-install-serviceworker

amp-ad

Example usage:

<InstallServiceworker
  src="https://www.your-domain.com/serviceworker.js"
  dataIframeSrc="https://www.your-domain.com/install-serviceworker.html"
/>

Props:

  • src (required)
  • dataIframeSrc (optional)
  • dataNoServiceWorkerFallbackUrlMatch (optional)
  • dataNoServiceWorkerFallbackShellUrl (optional)

Todo

  • Define all the AMP components with their properties and scripts in a JSON file and use that to generate them.
  • Rename Layout to Helmet
  • Provide helper finalHTML renderer

FAQs

Package last updated on 12 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.