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 meta Tag dynamically #25

Open
arukumar opened this issue Aug 22, 2016 · 2 comments
Open

Add meta Tag dynamically #25

arukumar opened this issue Aug 22, 2016 · 2 comments

Comments

@arukumar
Copy link

arukumar commented Aug 22, 2016

Hi,
can you please explain, how to add meta tag dynamically in server side rending. I have written something like

import React, { Component, PropTypes } from 'react'
import { defineMessages, FormattedMessage } from 'react-intl'
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import DocumentMeta from 'react-document-meta';

export default class metaTag 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'
                },
                itemProp: {
                    name: 'The Name or Title Here',
                    description: 'This is the page description',
                    image: 'http://example.com/image.jpg'
                },
                property: {
                    'og:title': 'I am overriding!',
                    'og:type': 'article',
                    'og:image': 'http://example.com/image.jpg',
                    'og:site_name': 'Example Site Name',
                    'og:price:amount': '19.50',
                    'og:price:currency': 'USD',
                    'twitter:site': '@site',
                    'twitter:title': 'I am a Twitter title'
                }
            },
            auto: {
                ograph: true
            }
        };


        return (
            <div>
                <DocumentMeta {...meta} >
                <h2> Check Server side </h2>
            </div>
        );

    }
}

but how to add them to sever.js

@danieljuhl
Copy link
Contributor

Have you read the README and looked at the examples?

You have to call DocumentMeta.renderAsHTML() and inject the result in your server side rendered HTML.

@A-Kasaaian
Copy link

A-Kasaaian commented Feb 15, 2017

@arukumar unfortunately it's really difficult. I have been struggling for about one week to make it render on server side using data coming from another server. You should do it using react-redux and react-thunk. place your API call in an action, then call the action in onEnter event of your route, then in call back onEnter event to continue rendering in route dispatch(theAPICallAction()).then(callBackOfOnEnterEvent()).
The API call action should update your redux store, which gives you ability of using received data in your component.
I know It is a little bit complicated, but the result would be amazing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants