question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get CSS and JS for requested chunk at server itself

See original GitHub issue

When a route is requested from server, I need to find all the required style and bundle required for that route.

import { ChunkExtractor, ChunkExtractorManager } from '@loadable/server';
const statsFile = path.resolve('./loadable-stats-web.json');

const extractor = new ChunkExtractor({ statsFile }); // Do I need to pass more params here to get my case working?
const scriptTags = extractor.getScriptTags();
const styleTags = extractor.getStyleTags();

extractor.getRequiredChunksScriptContent() // . This is giving empty array.

This gives me script and style tags for base and vendor bundle. BUT the requested chunk’s JS and CSS are not included here. It’s this base.js bundle when loaded on browser makes request to corresponding chunk.

How we can achieve that in such a way that loadableReady includes that chunk’s JS and CSS as well for that loadableReady( ofcourse on client)?

import { loadableReady } from '@loadable/component';

I can do the same in react-loadable

import { getBundles } from "react-loadable/webpack";
const stats = require("./react-loadable-web.json");

getBundles(stats, modules);

This gives me the required chunk’s JS and CSS but the limitation is it doesn’t include base and vendor bundle’s JS/CSS

and hence, opposite to loadable-components, Loadable.preloadReady won’t include main and vendor bundle’s CSS and JS to load.

import Loadable from "react-loadable";
Loadable.preloadReady()

So, for react-loadable, I’ve to implement loading vendor and base bundle before chunk’s JS/CSS. But not sure how to know the chunk in case of loadable-components while at server

Note: I’m having only one entry point for JS base. style for base’s CSS are extracted using mini-css-extract plugin. And vendor is split using webpack’s Splitchunks.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
eseQcommented, Oct 16, 2019

I found it! Just getMainAssets call before addChunk. Because I used it like:

Wrong way
const getExt = (url, { routerContext: RC, ...data }, routerContext, req) => {
  const extractor = new ChunkExtractor({ statsFile, entrypoints: ['app'] });
  const nodeExtractor = new ChunkExtractor({ statsFile: nodeStatsFile, entrypoints: ['node'] });
  const App = nodeExtractor.requireEntrypoint('node');
  const jsx = extractor.collectChunks(React.createElement(App, params));

  const scriptTags = extractor.getScriptTags();
  const linkTags = extractor.getLinkTags();
  const styleTags = extractor.getStyleTags();
  return { jsx, scriptTags, linkTags, styleTags };
};

const renderWithStream = (params) => {
  const { jsx, ...ext } = getExt(params);
  const htmlStream = ReactDOMServer.renderToNodeStream(jsx);
  ...
  return { htmlStream, ...ext };
};

const render = (params) => {
  const { jsx, ...ext } = getExt(params);
  const html = ReactDOMServer.renderToString(jsx);
  ...
  return { html, ...ext };
}

IMPORTANT use renderToString or renderToNodeStream before get scripts and styles with extractor.

const renderStatic = (params) => {
  const { jsx, extractor } = getExt(params);
  const html = ReactDOMServer.renderToString(jsx); // <--- IMPORTANT before use extractor.get
  const scriptTags = extractor.getScriptTags();
  const linkTags = extractor.getLinkTags();
  const styleTags = extractor.getStyleTags();
  return { html, scriptTags, linkTags, styleTags };
}

Can I ask why it is so important? How extractor works with ReactDOMServer?

@aseem2625 maybe it is will be useful for you.

2reactions
gregbergecommented, May 28, 2019

@aseem2625 react-loadable have issue tab closed and only merge PR that modifies docs. The project is literally dead. It is not compatible with new features coming into React and not compatible with last versions of webpack. But yeah you could use it if you want.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Components: Server-Side vs. Client-Side | CSS-Tricks
Technically all you need when rendering SSR components is a single request. Because client-side components require JavaScript, the browser must ...
Read more >
Nginx failing to load CSS and JS files (MIME type error)?
I was getting a similar error with Angular 5 - typescript and Nginx server. error in console
Read more >
Code Splitting - webpack
runtimeChunk : 'single' is needed too, otherwise we could get into trouble described here. webpack.config.js const path = require('path'); module.
Read more >
Link types: preload - HTML: HyperText Markup Language | MDN
Here we preload our CSS and JavaScript files so they will be available as soon as they are required for the rendering of...
Read more >
Server-Side Caching vs. Client-Side Caching - Edgemesh
Some of the files cached on the client-side include CSS files, JavaScripts, Images, HTML codes, and images. When it comes to browser request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found