Coder Social home page Coder Social logo

Invariant failed in HeadTags about react-head HOT 9 CLOSED

tizmagik avatar tizmagik commented on September 24, 2024
Invariant failed in HeadTags

from react-head.

Comments (9)

casertap avatar casertap commented on September 24, 2024

This is the React tree on my dev env:

screenshot from 2018-09-10 15-43-40

from react-head.

casertap avatar casertap commented on September 24, 2024

For some reasons the React Context did not make it to the HeadTag component, making this line fail:

invariant(headTags, '<HeadProvider /> should be in the tree');

Now I am using the redux (provider) with the connect method (consumer) on server side without any issue.

from react-head.

TrySound avatar TrySound commented on September 24, 2024

Could you try to wrap all other providers with HeadProvider? There can be a conflict between old and new context apis.

from react-head.

casertap avatar casertap commented on September 24, 2024

I am also trying react-helmet-async and I also got some weird errors.

I am doing something a little unconventional on the server side and this might be the issue.
I have an universal folder a client folder and a server folder.
On the server side I using babel alone to transpile the few js files in the server folder but then I use webpack to build the universal bundle (on the server side).
The entry point it a top level component called Routes.

my server.js file just require the build component like that:

  const Layout = require("../../build/Routes.js").default

And then I use this top level component like this:

 const rootComponent = PROD ? (
      <IntlProvider locale={locale} messages={messages[locale]}>
        <Provider store={store}>
          <HeadProvider headTags={headTags}>
            <StaticRouter location={location} context={context}>
              <Layout />
            </StaticRouter>
          </HeadProvider>
        </Provider>
      </IntlProvider>
    ) : null;

My transpiled server will still use the node_modules folder during runtime.
While my Routes bundle will include it own node_modules js file within the bundle.

Can you tell me if you think this is an issue?
Because from what I understood the React.Context should be the same (it is kind of statefull).

from react-head.

casertap avatar casertap commented on September 24, 2024

Now this should work though, but it doesn't:
on server.js

    const Layout = require("../../build/Routes.js").default
    const headTags = [];
    const rootComponent = PROD ? (
      <IntlProvider locale={locale} messages={messages[locale]}>
        <Provider store={store}>
          <StaticRouter location={location} context={context}>
            <HeadProvider value={{headTags: headTags}}>
              {[
                <Title>Search</Title>,
                <Meta name="description" content="Some Search" />,
                <Meta property="og:title" content="Search" />,
                <Meta property="og:image" content="path/to/search.jpg" />,
                <Layout />,
              ]}
            </HeadProvider>
          </StaticRouter>
        </Provider>
      </IntlProvider>
    ) : null;
    const markup = ReactDOMServer.renderToString(rootComponent);

fail with: invariant failed again


(node:4316) UnhandledPromiseRejectionWarning: Error: Invariant failed
    at index (/home/pc/Documents/funspace/babysearch/isomorphic-react-redux-saga-ssr/node_modules/tiny-invariant/dist/tiny-invariant.cjs.js:13:11)

from react-head.

casertap avatar casertap commented on September 24, 2024

@TrySound
I tried to wrap my HeadTags with a HeadProvider within the universal components but I still ge the same error

from react-head.

TrySound avatar TrySound commented on September 24, 2024

This error looks like you have two instances of head context. Make sure you use one instance in server side. Try to put console.log in react-head/dist/index.cjs.js and react-head/dist/index.esm.js and then run your app.

from react-head.

oyeanuj avatar oyeanuj commented on September 24, 2024

@casertap What solution did you end up with here?

from react-head.

TrySound avatar TrySound commented on September 24, 2024

@oyeanuj Add HeadProvider in the root of the tree like said in docs.

from react-head.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.