【问题标题】:ReactJS not displaying the HTML in when clicked on View Page Sorce单击查看页面源时,React JS 不显示 HTML
【发布时间】:2018-11-26 05:10:20
【问题描述】:

我是 ReactJS 的新手,我想建立一个 ReactJS 网站并进一步实施 SEO。当我点击“查看页面源”选项练习时,它不会显示页面的整个html,而是只显示index.html的内容。

我尝试使用来自here, 的服务器端渲染参考来实现它

但是没有用。

当我使用-npm run build && npm run start 构建和运行它时,它会给出错误:-

  project_frontend@0.1.0 babel /usr/react_projects/project_frontend
> babel src -d views

 { SyntaxError: /usr/react_projects/project_frontend/src/bundle.js: Unexpected token (8:2)

   6 | const store = configureStore();
   7 | render(
>  8 |   <Provider store={store}>
     |   ^
   9 |     <App />
  10 |   </Provider>,
  11 |   document.querySelector("#app")
  at Parser.raise (/usr/react_projects/project_frontend/node_modules/@babel/parser/lib/index.js:4028:15)
  at Parser.unexpected (/usr/react_projects/project_frontend/node_modules/@babel/parser/lib/index.js:5343:16)
  at Parser.parseExprAtom (/usr/react_projects/project_frontend/node_modules/@babel/parser/lib/index.js:6432:20)
  at Parser.parseExprSubscripts (/usr/react_projects/project_frontend/node_modules/@babel/parser/lib/index.js:6019:21)
  at Parser.parseMaybeUnary (/usr/react_projects/project_frontend/node_modules/@babel/parser/lib/index.js:5998:21)
  at Parser.parseExprOps (/usr/react_projects/project_frontend/node_modules/@babel/parser/lib/index.js:5907:21)
  at Parser.parseMaybeConditional (/usr/react_projects/project_frontend/node_modules/@babel/parser/lib/index.js:5879:21)
  at Parser.parseMaybeAssign (/usr/react_projects/project_frontend/node_modules/@babel/parser/lib/index.js:5826:21)
  at Parser.parseExprListItem (/usr/react_projects/project_frontend/node_modules/@babel/parser/lib/index.js:7111:18)
  at Parser.parseCallExpressionArguments (/usr/react_projects/project_frontend/node_modules/@babel/parser/lib/index.js:6227:22)
pos: 234,
loc: Position { line: 8, column: 2 },
code: 'BABEL_PARSE_ERROR' }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project_frontend@0.1.0 babel: `babel src -d views`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project_frontend@0.1.0 babel script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/user/.npm/_logs/2018-11-26T04_40_59_532Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project_frontend@0.1.0 build: `npm run pack && npm run babel`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project_frontend@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/user/.npm/_logs/2018-11-26T04_40_59_580Z-debug.log

谁能建议一个更好的方法/教程来实现这个。

【问题讨论】:

  • 您必须更具体。有很多关于如何在 React 中实现 SSR 的教程,所以最好展示一下你尝试了什么以及具体它是如何不起作用的。
  • @Jayce444 请检查更新后的问题。
  • 您好,您解决了这个问题吗?谢谢

标签: javascript html reactjs react-redux


【解决方案1】:

您可以使用react-meta-tags。它允许您以声明性方式和正常 jsx 格式的元标记,这将被移动到头部(检查文档上的服务器使用情况)。

import React from 'react';
import MetaTags from 'react-meta-tags';

class MyComponent extends React.Component {
  render() {
    return (
        <div class="wrapper">
          <MetaTags>
            <title>Page 1</title>
            <meta id="meta-description" name="description" content="Some description." />
            <meta id="og-title" property="og:title" content="MyApp" />
            <meta id="og-image" property="og:image" content="path/to/image.jpg" />
          </MetaTags>
          <div class="content"> Some Content </div>
        </div>
      )
  }
}

或者您可以使用 JS 更改 componentDidMount() 方法中的元标记

document.title ="Welcome | here is your page title to display"; 
document.getElementsByTagName("META")[2].content="Your description about the page or site here to set dynamically";

【讨论】:

    【解决方案2】:

    如果您尝试查看页面源代码, 你会看到这个

    <body>
        <div id="root"></div>
        <script ........./>
    </body>
    

    当 ReactJS 开发服务器运行时,bundle.js 会自动编译,页面会重新渲染。

    看不到整页源码的原因很自然。 所以如果你想在浏览器上看到源代码工作,请在浏览器上使用 React/Redux 开发扩展

    【讨论】:

    • 但我想对其实施 SEO,为此我需要完整的页面源代码。
    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多