javascript - Nodejs & react : render another HTML file upon request

标签 javascript node.js reactjs server-side-rendering

我正在尝试在我们的项目中实现放大器页面。

到目前为止,我已经得出以下解决方案:如果网址中有查询,例如:myurl.com?amp=1,则页面将在必要的标记下完全重绘。问题是,目前我们的服务器配置为在 2 个 html 文件之间进行选择,如果有错误,则标记有错误,如果没有,则使用通常的索引。它的工作原理如下:

yield this.render('index', {
        state: encodeURIComponent(JSON.stringify(state)),
        body: renderToString(
            <Provider store={store}>
                <RouterContext {...props}/>
            </Provider>
        )
    });

和错误

app.use(isProduction ? function * error(next) { // error handler
    try {
        yield next;
        if (this.response.status === 404 && !this.response.body) {
            this.throw(404);
        }
    } catch (err) {
        const STATUS_CODES = require('http').STATUS_CODES;
        const seo = require('app/modules/seo').initialState;

        this.status = err.status = err.status || 500;
        if (err instanceof URIError) {
            this.redirect('/search');
            return;
        }
        err.message = STATUS_CODES[this.status];
        this.app.emit('error', err, this);

        yield this.render('error', {
            assets,
            err,
            seo: {...seo, long_title: `${err.status} – ${seo.long_title}`}
        });
    }
} : error());
componentDidMount() {
        if (this.state.isAmp) {
            document.write(expotedAmpMarkup(this.props.body))
        };
}

请告诉我,当 URL 中有请求时,服务器端如何禁用标准标记?

是否可以在服务器端绘制新页面?

对于困惑的文字,我提前表示歉意。我知道的太少,无法提出一个有效的问题,而且除了这个论坛之外,我没有人可以寻求帮助。

如有必要,它已准备好发送整个服务器和 webpack 配置。 准备好回答任何问题。谢谢。

最佳答案

您可以根据您的请求在 SSR 上呈现您需要的任何页面。

您可以查看我处理路由的 SSR 应用程序: https://github.com/tornado1979/ssr-rentalcars

一些要点如下:

客户端“index.js”:

.....
import { BrowserRouter } from 'react-router-dom'
import { renderRoutes } from 'react-router-config'

import store from './store'
import Routes from './components/Router/routes'


ReactDOM.hydrate(
<Provider store={store}>
  <BrowserRouter>
    <div>{renderRoutes(Routes)}</div>
  </BrowserRouter>
</Provider>,
document.getElementById('root'),
)

客户端“routes.js”,在这里放置所有页面

export default [
{
  ...App,
  routes: [
   {
     ...HomePage,
     description: "Compare car hire deals and find the cheapest prices in.",
    keywords: 'car hire, cheap car hire, car rental uk,  rent a car, car rentals, uk car car, cheap car rentals spain, cheap car rental usa, carrentals, rent car, car hire comparison, carrental, carhire, compare car hire, car rental comparison, rentalcars, rental cars',
    path: '/home',
    title: '.......',
  },
  {
    ...About,
    description: 'About Compare car hire deals...',
    keywords: 'car hire, ...',
    path: '/about',
    title: 'About - Rentalcars',
  },
  {
    ...NotFoundPage,
    description: '',
    keywords: '',
    title: 'page not found - Rentalcars',
  },
],
 },
  ]

服务器端,“index.js” 您接收请求并将正确的组件发送给客户端 //你需要这个路由器:
import { matchRoutes } from 'react-router-config'

app.get('*', (req, res) => {
  const store = createStore()
   // Initialize and load data into the store
   const promises = matchRoutes(Routes, req.path).map(({ route }) => {
    return route.loadData ? route.loadData(store) : null
  })

Promise.all(promises).then(() => {
  const context = {}
  const content = renderer(req, store, context)

  if (context.notFound) {
    res.status(404)
  }

  return res.send(content)
 }).catch(error => res.status(500).send(`Internal Server Error:, 
${error}`))
})

渲染 pagestore并将它们传递给客户端“render.js”

export default (req, store, context = {}) => {
const content = renderToString(
<Provider store={store}>
  <StaticRouter context={context} location={req.path}>
    <div>{renderRoutes(Routes)}</div>
  </StaticRouter>
</Provider>,

) const Helm = Helmet.renderStatic()

返回( <!DOCTYPE html> <html ${helmet.htmlAttributes.toString()}> <head> <meta charset="UTF-8"> ${helmet.title.toString()} ${helmet.meta.toString()} </head> <body ${helmet.bodyAttributes.toString()}> <div id="root">${content}</div> <script> window.INITIAL_STATE = ${serialize(store.getState())} </script> <script src="bundle.js"></script> </body> </html> ) }

希望对您有所帮助。

关于javascript - Nodejs & react : render another HTML file upon request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56207232/

相关文章:

javascript - 将 json 数据获取到数组 angular2 中

javascript - 如何为我所有的 Express 中间件设置默认的拒绝 promise 行为?

javascript - ReactJS 如何处理 Rails 事件记录关联

javascript - 在 React-ace 中设置值时出现类型错误

javascript - 使用 += 运算符 undefined object 中的第一个元素

javascript - 我在制造内存泄漏吗?

javascript - 如何为特定节点编写条件 xpath 选择?

javascript - 使用 cordova 表达 nodejs socket.io

node.js - 由heroku调度程序创建的一次性dyno永远不会死(永远不会自杀,自杀......)

javascript - React Native,如何将嵌套的if else转换为switch语句?