react-document-meta

HTML meta tags for React-based apps. Works for both client- and server-side rendering, and has a strict but flexible API.

  • 所有者: kodyl/react-document-meta
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

React Document Meta Build Status Coverage Status npm version

HTML meta tags for React-based apps. Works for both client- and server-side rendering, and has a strict but flexible API.

Built with React Side Effect


Installation

npm install --save react-document-meta

Note: React Side Effect requires React 0.14+ - and so does React Document Meta.

Using with React v0.13

Due to several deprecations and breaking changes to React, you'll have to use react-document-meta@^1.0.0.

Upgrading from 0.1.x to 1.x

As React Side Effect has been upgraded there is a few breaking changes, which is found in the changelog.

Features

  • Supports extending and nesting on any number of levels
  • Ability to auto generate some meta tags for open graph, twitter, and more

Usage

See example folder for a working sample.

import React from 'react';
import DocumentMeta from 'react-document-meta';

class Example extends React.Component {
  render() {
    const meta = {
      title: 'Some Meta Title',
      description: 'I am a description, and I can create multiple tags',
      canonical: 'http://example.com/path/to/page',
      meta: {
        charset: 'utf-8',
        name: {
          keywords: 'react,meta,document,html,tags'
        }
      }
    };

    return (
      <DocumentMeta {...meta}>
        <h1>Hello World!</h1>
      </DocumentMeta>
    );
  }
}

React.render(<Example />, document.getElementById('root'));

Nesting

In most real world use cases, you would like to set some defaults and modify, replace or add just some of the meta tags. react-document-meta always use the deepest data set, but you can add an extend attribute (<DocumentMeta {...metaData} extend />), to instruct the component to merge with the meta data specified one level up. You can add the extend attribute to as many DocumentMeta components you would like, but the chain needs to be complete.

Automatic Meta Tags

react-document-meta has the ability to generate meta tags based on the already provided meta data. Currently only open graph title, description and url is supported, which uses the data from title, description and canonical, and only in the case where the values has not been explicit set for og:title, og:description or og:url respectively.

Server Usage

When using react-document-meta in a project with server-side rendering, you would like to have the final meta data chunk available in your HTML output. You can achieve this by calling DocumentMeta.rewind().

Instead of getting a plain object, you can have the module return the meta as either React components or a HTML string. This is achieved by calling DocumentMeta.renderAsReact() or DocumentMeta.renderAsHTML().

import React from 'react';
import DocumentMeta from 'react-document-meta';

export default handler = (...args) => {
  ...
  const app = React.renderToString(components);
  const meta = DocumentMeta.renderAsHTML();
  const markup = `
    <html>
      <head>
        ${meta}
      </head>
      <body>
        <div id="app">
          ${app}
        </div>
      </body>
    </html>
  `
  ...
}

TODO:

  • Create full documentation
  • Improve flexibility for custom attributes

主要指標

概覽
名稱與所有者kodyl/react-document-meta
主編程語言JavaScript
編程語言Makefile (語言數: 3)
平台
許可證MIT License
所有者活动
創建於2015-05-01 10:52:03
推送於2021-03-04 22:58:18
最后一次提交2018-03-21 15:04:49
發布數17
最新版本名稱v3.0.0-beta.5 (發布於 2018-03-21 15:04:49)
第一版名稱v0.1.1 (發布於 2015-05-06 10:30:14)
用户参与
星數320
關注者數6
派生數19
提交數115
已啟用問題?
問題數41
打開的問題數11
拉請求數3
打開的拉請求數2
關閉的拉請求數3
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?