Next.js: react-intl in Head

Created on 6 Apr 2017  路  6Comments  路  Source: vercel/next.js

I am using react-intl as in project example. All works fine except for one small issue. If I try to set title in Head using FormattedMessage component it throws an error on SSR saying:

Could not find required intl object. needs to exist in the component ancestry.

<Head>
    <title>
      <FormattedMessage id="login.title" defaultMessage="login" />
    </title>
</Head>

However if I set the title using the formatMessage method things work fine:

const title = intl.formatMessage({
      id: 'login.title',
      defaultMessage: 'login',
    });
...
<Head>
    <title>
      {title}
    </title>
</Head>

Using <FormattedMessage> component elsewhere in render still works as it should though

example

Most helpful comment

I already have that set up and working following this example:
https://github.com/zeit/next.js/tree/master/examples/with-react-intl

Problem is <FormattedMessage> component is somehow causing error but only when used within <Head> whereas calling intl.formatMessage() directly is not

All 6 comments

I think you need an intl ponyfill for node, such as https://www.npmjs.com/package/intl

I already have that set up and working following this example:
https://github.com/zeit/next.js/tree/master/examples/with-react-intl

Problem is <FormattedMessage> component is somehow causing error but only when used within <Head> whereas calling intl.formatMessage() directly is not

Same issue here. It's like the intlprovider is not "providing" the Head so you can't use the components:

[React Intl] Could not find required intl object. needs to exist in the component ancestry.

is https://github.com/zeit/next.js/pull/2283 related @coluccini?

@ericf could you have a look since you made the example?

It shouldn't. This is happening with and without the polyfill.
I think this is more related to how next/head works than something related directly to Intl. Cause if you do this

<Head>
    <title>
      <FormattedMessage id="login.title" defaultMessage="login" />
    </title>
</Head>
<FormattedMessage id="login.title" defaultMessage="login" />

Only the first <FormattedMessage /> will throw the error

Closing for inactivity

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rauchg picture rauchg  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

pie6k picture pie6k  路  3Comments

timneutkens picture timneutkens  路  3Comments

wagerfield picture wagerfield  路  3Comments