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

Meta tags ssr #1

Closed
fireantlabs opened this issue Mar 30, 2018 · 2 comments
Closed

Meta tags ssr #1

fireantlabs opened this issue Mar 30, 2018 · 2 comments

Comments

@fireantlabs
Copy link

Thanks for help me make react ssr with php.
But i have problem when make it better for me. I add meta tags use react-helmet or react-meta-tags . It change title with my route but when i view source page, Meta tags is not change.
Can you have solution for this problem.

@chenos
Copy link
Owner

chenos commented Mar 30, 2018

Look at the example

https://github.com/s-yadav/react-meta-tags/blob/master/examples/app.js

Mainly look at the code

match({
  routes, location: req.url
}, (error, redirectLocation, renderProps) => {
  if(error) {
    res.status(500).send(error.message);
  } else if (redirectLocation) {
    res.redirect(302, redirectLocation.pathname + redirectLocation.search)
  } else if (renderProps) {
      let reactString;

      const metaTagsInstance = MetaTagsServer();

      try{
        reactString = ReactDomServer.renderToString(
          <MetaTagsContext extract = {metaTagsInstance.extract}>
            <RouterContext {...renderProps}/>
          </MetaTagsContext>
        );
      }
      catch(e){
        console.log(e);
        res.status(500).send(e.stack);
        return;
      }

      const meta = metaTagsInstance.renderToString();

      const template = `
        <!doctype html>
        <html lang="en">
        <head>
          <meta charSet="utf-8"/>
          <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
          <meta http-equiv="x-ua-compatible" content="ie=edge">

          ${meta}
          <!-- Bootstrap CSS -->
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" integrity="2hfp1SzUoho7/TsGGGDaFdsuuDL0LX2hnUp6VkX3CUQ2K4K+xjboZdsXyp4oUHZj" crossorigin="anonymous">

        </head>
        <body>
          <div id="app">${reactString}</div>
          <script src="http://localhost:9000/bundle.js"></script>
        </body>
      `;

    res.status(200).send(template)
  } else {
    console.log('redirected');
    res.status(301).redirect('/')
  }

PHP

$context->set('req', new Request(), true);
$context->set('res', new Response(), true);

@fireantlabs
Copy link
Author

fireantlabs commented Mar 30, 2018

Thanks you for helping me.
I was config success with "react-helmet ^^!.
in webpack.config.js i add

externals:{'react-helmet': 'react-helmet'}

and in App.php

function __construct(){
//......
$context->require('react-helmet', 'Helmet');
}

function responsive(){
//....................
$data['content']=$this->context->eval("ReactDOMServer.renderToString(React.createElement(App, httpServer.data))");
$this->context->eval("const helmet = Helmet.renderStatic()");
$data['title'] = $this->context->eval("helmet.title.toString()");
return $data;

and in Index.php file, i add

{$html['title']} ---> in head
{$html['content']} --> in content ID

One again, i thank u so much.

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

2 participants