Parcel Bundler&React Server Side Rendering

问题描述 投票:0回答:1

[当我使用React Server Side Rendering时,我试图将Parcel Bundler用作客户端文件的捆绑器。

我已经使用了包裹中间件,并为其分配了客户端入口点的位置。

当我启动脚本时,它表明Parcel正在捆绑我的文件,但是我的ReactDOM.hydrate方法从未被调用,而且看来捆绑程序根本就没有使用该文件。

这是我的server.js文件:

import Bundler from 'parcel-bundler';
import express from 'express';
import { renderer } from './Helpers';

const app = express();
const bundler = new Bundler(__dirname + '/index.js');

app.use(express.static('public'));
app.use(bundler.middleware());

app.get('*', (req, res) => {
    const context = {};
    const content = renderer(req, context);

    if (context.url) return res.redirect(301, context.url);
    if (context.notFound) res.status(404);

    res.send(content);
});

const listeningPort = 5000;

app.listen(listeningPort, () => {
    console.log(`Server is now live on port ${listeningPort}.`);

这是我的index.js文件:

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { routes as Routes } from './Routes';

const routes = Routes;

if (process.env.NODE_ENV === 'development')
    ReactDOM.render(
        <BrowserRouter>
            {routes}
        </BrowserRouter>,
        document.querySelector('#root'));
else
    ReactDOM.hydrate(
        <BrowserRouter>
            {routes}
        </BrowserRouter>,
        document.querySelector('#root'));

这是我的渲染器文件,基本上渲染html文件:

import React from 'react';
import { renderToString } from 'react-dom/server';
import { StaticRouter } from 'react-router-dom';
import { routes as Routes } from '../Routes';

const routes = Routes;

export default (req, context) => {
  const content = renderToString(
    <StaticRouter location={req.path} context={context}>
      {routes}
    </StaticRouter>
  );

  const lang = "en";

  return `
    <!DOCTYPE html>
    <html lang="${lang}">
      <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 name="apple-mobile-web-app-capable" content="yes">

        <link href="./public/plugin/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
        <link href="./public/plugin/font-awesome/css/fontawesome-all.min.css" rel="stylesheet" />
        <link href="./public/plugin/et-line/style.css" rel="stylesheet" />
        <link href="./public/plugin/themify-icons/themify-icons.css" rel="stylesheet" />
        <link href="./public/plugin/owl-carousel/css/owl.carousel.min.css" rel="stylesheet" />
        <link href="./public/plugin/magnific/magnific-popup.css" rel="stylesheet" />

        <link href="./public/css/style.css" rel="stylesheet" />
        <link href="./public/css/color/default.css" rel="stylesheet" id="color_theme" />
      </head>
      <body>
        <div id="root">${content}</div>
      </body>
    </html>
  `;
};

应用程序运行并加载StaticRouter中的内容,但从不执行Hydrate。

reactjs server-side-rendering parcel
1个回答
0
投票

我终于使用以下示例项目解决了我的问题。

https://github.com/reactivestack/parcel-react-ssr

基本上,解决方案是不使用Bundler Middleware,而是将客户端和服务器端分别与Parcel捆绑在一起,然后运行项目。

© www.soinside.com 2019 - 2024. All rights reserved.