2.0.5 • Published 7 months ago

react-helmet-async v2.0.5

Weekly downloads
2,268,476
License
Apache-2.0
Repository
github
Last release
7 months ago

react-helmet-async

CircleCI

Announcement post on Times Open blog

This package is a fork of React Helmet. <Helmet> usage is synonymous, but server and client now requires <HelmetProvider> to encapsulate state per request.

react-helmet relies on react-side-effect, which is not thread-safe. If you are doing anything asynchronous on the server, you need Helmet to encapsulate data on a per-request basis, this package does just that.

Usage

The main way that this package differs from react-helmet is that it requires using a Provider to encapsulate Helmet state for your React tree. If you use libraries like Redux or Apollo, you are already familiar with this paradigm:

import React from 'react';
import ReactDOM from 'react-dom';
import Helmet, { HelmetProvider } from 'react-helmet-async';

const app = (
  <HelmetProvider>
    <App>
      <Helmet>
        <title>Hello World</title>
        <link rel="canonical" href="https://www.tacobell.com/" />
      </Helmet>
      <h1>Hello World</h1>
    </App>
  </HelmetProvider>
);

ReactDOM.hydrate(
  app,
  document.getElementById(‘app’)
);

On the server, we will no longer use static methods to extract state. react-side-effect exposed a .rewind() method, which Helmet used when calling Helmet.renderStatic(). Instead, we are going to pass a context prop to HelmetProvider, which will hold our state specific to each request.

import React from 'react';
import { renderToString } from 'react-dom/server';
import Helmet, { HelmetProvider } from 'react-helmet-async';

const helmetContext = {};

const app = (
  <HelmetProvider context={helmetContext}>
    <App>
      <Helmet>
        <title>Hello World</title>
        <link rel="canonical" href="https://www.tacobell.com/" />
      </Helmet>
      <h1>Hello World</h1>
    </App>
  </HelmetProvider>
);

const html = renderToString(app);

const { helmet } = helmetContext;

// helmet.title.toString() etc…

Streams

This package only works with streaming if your <head> data is output outside of renderToNodeStream(). This is possible if your data hydration method already parses your React tree. Example:

import through from 'through';
import { renderToNodeStream } from 'react-dom/server';
import { getDataFromTree } from 'react-apollo';
import Helmet, { HelmetProvider } from 'react-helmet-async';
import template from 'server/template';

const helmetContext = {};

const app = (
  <HelmetProvider context={helmetContext}>
    <App>
      <Helmet>
        <title>Hello World</title>
        <link rel="canonical" href="https://www.tacobell.com/" />
      </Helmet>
      <h1>Hello World</h1>
    </App>
  </HelmetProvider>
);

await getDataFromTree(app);

const [header, footer] = template({
  helmet: helmetContext.helmet,
});

res.status(200);
res.write(header);
renderToNodeStream(app)
  .pipe(
    through(
      function write(data) {
        this.queue(data);
      },
      function end() {
        this.queue(footer);
        this.queue(null);
      }
    )
  )
  .pipe(res);

License

Licensed under the Apache 2.0 License, Copyright © 2018 Scott Taylor

@modern-js/doc-corebullseye-stories@uponco/admin-uimikorab-uupm-ui@studiobear/gatsby-theme-ionic@studiobear/gatsby-theme-blogafiniti_video-playerflask_reactfrontity-core-recoded@studiobear/gatsby-theme-blog-ionicalice-reactilottie@phpfox/frameworkliuzy-ant-design-proinlada-meetings-reactjyswsdssdfdfdsafdtedsssswdsws@gulibs/react-prosemirror-editorfrontity-k8s-not-meant-to-be-usefulcra-template-gtja-czcra-template-ltbycecra-template-cz-testmarketing-sitecra-template-josephscandinavia@joseirrazabal/links@joseirrazabal/kittest-widget-order@nosycode/cra-templatesamtools-web-templateant-views@itwin/error-handling-react-3.0dmd-componentsimple-antd-management-fast-framework@cointap/cointap-chartsendpoint-todo-listsinopia_editorracyreact0rm4l_graphicsbinaryblox-react-corecra-template-redux-tailwind-typescriptart47ladys-npm-baselabswap-frontendislands-pressreact-islandcomponents-bullseye@everything-registry/sub-chunk-2558ant-pro-test-larry@paras205/react-ts-starter@ascendware/mxmedusa-admin-ui-facherry-designsftdsofty-icons@perseides/admin-uichat-bot-v1evite-react-libswaptokensvl-admin-templatetest-formilytest-frm-video-player-cmpntest-frm-video-player-cmpntwidget-embeded-demoweb-cp-vbwf-elementsxkally-uixl-apixiongdemoxmluirunmixshaco-ssgsanity-plugin-milkdownscaffoldr-reactscab-chatreact-web-editorreacticarockbl_testrmw-shell-previewstorefront-alphastorefront-icu_admin-uisportsbet-apollosportsbet-promotions-appsportsbet-user-appsequelcomponentseo-boost-testtest-video-player-cmpnttest-video-player-rodrigofeztest-pushlish-npmtest-widget-order-componentsvelte-component-libsy-corethreed-aithunderballthunderball-clientvpressifyvisitevite-react-ssgvitextvendor-ui-alpha-v1
2.0.5

7 months ago

2.0.5-alpha

7 months ago

2.0.5-next

7 months ago

2.0.5-beta

7 months ago

2.0.4

11 months ago

2.0.4-beta

11 months ago

2.0.4-alpha

11 months ago

2.0.4-next

11 months ago

2.0.0-next

1 year ago

2.0.3

12 months ago

2.0.3-beta

12 months ago

2.0.2

12 months ago

2.0.0-beta

1 year ago

2.0.3-next

12 months ago

2.0.0-next.1

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

2.0.2-next

12 months ago

2.0.1-alpha

1 year ago

2.0.0-beta.1

1 year ago

2.0.0-alpha

1 year ago

2.0.3-alpha

12 months ago

1.3.0

3 years ago

1.2.0

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.1

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-beta.1

6 years ago

1.0.0-beta.0

6 years ago

0.3.0-beta.3

6 years ago

0.3.0-beta.2

6 years ago

0.3.0-beta.1

6 years ago

0.3.0-beta.0

6 years ago

0.2.1-0

6 years ago

0.2.0

6 years ago

0.2.0-beta.1

6 years ago

0.2.0-beta.0

6 years ago

0.1.0-alpha

6 years ago

0.1.0

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago