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.

Server rendering with dynamic imports and CSS

See original GitHub issue

So I’ve got loadable-components working wonderfully with almost everything - Webpack 4 is happy, HMR (for dev) is happy, tests where dynamic imports need to resolve first are happy, etc.

But the part I’m a bit stuck on is server-side rendering with dynamic imports and per-module CSS files. I’m using MiniCssExtractPlugin, as ExtractTextPlugin is now deprecated. The CSS is splitting just fine. As an example, if I log the Webpack assets that are generated, I can see things like:

Descriptions:
   { css: 'Descriptions.efb10258987bd945.css',
     js: 'Descriptions.46113354beab1663.js' },

…but this is a problem, as whilst loadable-components does a brilliant job of splitting the JS and then recombining it server-side, the split CSS (and I presume other assets such as images?) isn’t coming along for the ride. As a result, only the “non-dynamic” CSS is delivered with a pure SSR render (client-side JS disabled to test, as the client will insert the CSS once it starts up).

I’ve tried as a hacky solution getting MiniCssExtractPlugin to combine all the CSS into one file, avoiding the issue entirely, but when I do that it breaks JS entry points and all SPA functionality is lost.

Is there a recommended or known way to deal with this issue, where non-JS assets aren’t brought along for SSR? I can write some super-hacky code to parse the script tag string and then match chunk names against Webpack’s assets, and that’ll probably work temporarily. But if there’s a better way to handle this, it’d be great to know.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
theKasheycommented, Oct 26, 2019

The isolation provided by CSS-in-JS pattern just makes it super easy to handle style ordering, as well as Emotion just inlines all styles in the HTML. But broadly speaking correct CSS ordering might be VERY complicated.

1reaction
jcklpecommented, Sep 13, 2019

I suggest you to use CSS in JS library like Styled Components or Emotion in order to solve this problem. Loadable Components is focused on JS only and will not provide a solution for CSS.

@neoziro That’s a little disappointing to hear. I understand why some devs like CSS in JS but there are still many devs who prefer using a SCSS/CSS build system. While many features of SCSS can be replicated in CSS in JS with some work, it’s also a pain and often produces mark up which is ugly and not aligned with the original design spec of CSS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Dynamic Import - Next.js
To dynamically load a component on the client side, you can use the ssr option to disable server-rendering. This is useful if an...
Read more >
Dynamic imports and preloading in Next.js, doing it the right way
Learn how to speed up Next.js web applications by preloading dynamically imported React and non React modules.
Read more >
Dynamic imports and code splitting with Next.js
This method creates multiple bundles that are dynamically loaded at the runtime of the webpage. Dynamic imports make use of import statements ...
Read more >
Code splitting with dynamic imports in Next.js - web.dev
This post explains different types of code splitting and how to use dynamic imports to speed up your Next.js apps.
Read more >
Dynamically Import CSS - Medium
The component is set to render dynamically our themes, but we do not yet lazy load these. Moreover, our CSS content has been...
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