0

I make app.client.js and app.server.js bundles, plus I perfom code splitting and create vendor.chunk.js 1.chunk.js (this one should be my component bundle thas is uploaded on request).

When I take a look at them, they are all different, but when I look at them via DevTools, every bundle is like on the screen below:

enter image description here

That's the part of my renderer.js that I use to perform res.send(~html~):

export default (req, res, next) => {
let context = {};
let modules = [];
const html = ReactDomServer.renderToString(
    <Capture report={moduleName => modules.push(moduleName)}>
        <Router context={context} location={req.url}>
            <App/>
        </Router>
    </Capture>
);

let bundles = getBundles(stats, modules);

return res.send(`
        <!doctype html>
        <html lang="en">
          <head>...</head>
          <body>
            <div id="app">${html}</div>
            <script src="../../../dist/vendor.chunk.js"></script>
            ${bundles.map(bundle => {
    return `<script src="${bundle.publicPath}"></script>`
}).join('\\n')}
            <script src="../../../dist/app.client.js"></script>
          </body>
        </html>
`);
}

Can you explain me why it is broken? Thanks!

UPD: after setting my server/index.js file as follows ...

const app = Express();
const Router = Express.Router();

Router.use(Express.static(
    'dist',
    { maxAge: '30d' }));
Router.use('*', serverRenderer);

app.use(Router);
preloadAll().then(() => {
    app.listen(PORT, (error) => {
        if (error) return console.log('ERROR', error.message);
        console.log('Server listening on ', PORT);
    });
});

... I've got another problem: 1.chunk.js and 1.chunk.js.map are somehow duplicated and devtools warns me with the error "Unexpected token :". This is how it looks like now:

enter image description here

Evgeny Kobzev
  • 89
  • 1
  • 12
  • 2
    That's because you're replying to EVERY request with this html page. Even the requests that are trying to get the js files. You need to add another express handler before this one. A static handler should be good for you: https://expressjs.com/en/starter/static-files.html. – Alon Bar Sep 13 '18 at 23:16
  • @AlonBar i've actually got it `Router.use('*', renderer); Router.use(Express.static( resolve(__dirname, 'dist'), { maxAge: '30d' }) );` – Evgeny Kobzev Sep 14 '18 at 05:25
  • 1
    Is it working for you? It seems that the order of handlers is incorrect. You've put the static handler after the render handler but your requests never get to it because they're all handled by the renderer. Just change the order and put the static handler first. – Alon Bar Sep 14 '18 at 07:38
  • Also, the leading dots seems unnecessary. Notice how the path outputted by `bundle.path` doesn't have them. Try changing this "../../../dist/vendor.chunk.js" to this "/vendor.chunk.js". – Alon Bar Sep 14 '18 at 07:46
  • @AlonBar I've tried every option, but everything is the same :( – Evgeny Kobzev Sep 14 '18 at 14:06
  • @AlonBar after making some tweaks it actually works, but now 1.chunk.js and 1.chunk.js.map are duplicated.. one of them still looks like html and the other looks like bundle (but it has "unexpected token : " error) – Evgeny Kobzev Sep 14 '18 at 14:42
  • Can you update your question so that it will reflect the current situation? – Alon Bar Sep 14 '18 at 14:48
  • @AlonBar added an update – Evgeny Kobzev Sep 15 '18 at 22:37
  • Well the only issue I still see is that you're probably missing a leading / in your script tag, i.e. ` – Alon Bar Sep 17 '18 at 20:37

0 Answers0