Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helmet to web for meta tags | Prerender support #2909

Merged
merged 12 commits into from
Jul 7, 2021

Conversation

dac09
Copy link
Contributor

@dac09 dac09 commented Jun 29, 2021

What?

Adds support for using react-helmet-async to modify , including open graph metadata, during prerender (and at run time too)

  • Built-in support for setting headers using the <Head> or <Helmet> components (aliased)
  • Handy-dandy <MetaTags> component that lets you set commonly used meta tags for opengraph SEO/Unfurling
  • Prerender will respect these headers, and add them to the generated HTML. See caveat for dynamic pages though in notes.

Notes

  • This is currently backwards compatible, technically - if you don't add helmet, it shouldn't break anything
  • This PR will not add support for dynamically generated headers (aka SSR) e.g. setting headers from a cell. But.... you could use Netlify's prerender or prerender.io to achive this.

Linked to #2001


For release notes 👇

We need to make sure the RedwoodProvider wraps your entire App. In App.tsx:

- import { FatalErrorBoundary } from '@redwoodjs/web'
+ import { FatalErrorBoundary, RedwoodProvider } from '@redwoodjs/web'


  <FatalErrorBoundary page={FatalErrorPage}>
+    <RedwoodProvider>
      <RedwoodApolloProvider>
        <Routes />
      </RedwoodApolloProvider>
+    </RedwoodProvider>
  </FatalErrorBoundary>

This is an optional change, as your Redwood app will continue to work without this, but we recommend making the change now, so you're up-to-date with the templates we use in our tests.

@dac09 dac09 force-pushed the feature/prerender-helmet-support branch from 44363c0 to 692a1c8 Compare July 6, 2021 16:26
@dac09 dac09 marked this pull request as ready for review July 6, 2021 16:26
@dac09 dac09 requested a review from dthyresson July 6, 2021 16:29
@dac09 dac09 self-assigned this Jul 6, 2021
Comment on lines 25 to 26
export { Helmet as Head, Helmet } from 'react-helmet-async'
export { HelmetProvider } from 'react-helmet-async'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you exporting Head, Helmet and HelmetProvider here because other people might want to use those instead of Head? My suggestion would be to just export Head and abstract away Helmet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - my idea was to officially document <Head> but given the popularity of Helmet, I thought maybe users might prefer to use that name instead. Doesn’t hurt right?

Can remove HelmetProvider though, not really sure what I was thinking there.

packages/web/package.json Outdated Show resolved Hide resolved
@@ -7,6 +9,7 @@ declare global {
* This global is set to true by the prerendering CLI command.
*/
__REDWOOD__PRERENDERING: boolean
__REDWOOD__HELMET_CONTEXT: { helmet?: HelmetData }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for pre-rendering?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup - helmet async gathers up all the headers in this variable during prerender - will also be the same if we do this in builder functions in the future.

Copy link
Contributor

@dthyresson dthyresson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you be keeping RedwoodProvider?

packages/web/src/components/MetaTags.tsx Show resolved Hide resolved
@dac09
Copy link
Contributor Author

dac09 commented Jul 7, 2021

Will you be keeping RedwoodProvider?

Yes, that's the plan.

@dthyresson
Copy link
Contributor

Will you be keeping RedwoodProvider?

Yes, that's the plan.

Ah, sorry, I got RedwoodProvider confused with the comment:

Can remove HelmetProvider though

dac09 added 3 commits July 7, 2021 13:37
…edwood into feature/prerender-helmet-support

* 'feature/prerender-helmet-support' of github.com:dac09/redwood:
  upgrade Prisma v2.26.0 (redwoodjs#2915)
  Upgrade supabase-js with Discord auth support (redwoodjs#2965)
  resolve storybook config to Framework (redwoodjs#2892)
  Update packages/web/package.json
@dthyresson
Copy link
Contributor

LGTM!

@dac09 dac09 merged commit a4bcec0 into redwoodjs:main Jul 7, 2021
@dac09 dac09 deleted the feature/prerender-helmet-support branch July 7, 2021 14:00
@redwoodjs-bot redwoodjs-bot bot added this to the next-release milestone Jul 7, 2021
dac09 added a commit to dac09/redwood that referenced this pull request Jul 7, 2021
… fix/cli-command-names

* 'fix/cli-command-names' of github.com:dac09/redwood:
  Add helmet to web for meta tags | Prerender support (redwoodjs#2909)
dac09 added a commit that referenced this pull request Jul 7, 2021
…endabot-updates-070621

* 'main' of github.com:redwoodjs/redwood:
  Add helmet to web for meta tags | Prerender support (#2909)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants