Giter Site home page Giter Site logo

Comments (7)

dglozic avatar dglozic commented on June 15, 2024 1

Using CommonJS on the client side can also interfere with Webpack tree shaking.

In my corner of IBM, what we do is:

  1. Build client and server separately.
  2. We build client chunks using Babel for JSX, but leave it pure ES6, using ES6 imports. This code is exposed in package.json via 'module'.
  3. We build server side using Babel for JSX + CommonJS transpiling. This code is exposed in package.json via 'main'.
  4. We use rollup to build the same React code twice (for client and server as above).

SSR pass will require react components as CommonJS modules. Meanwhile, Webpack will build dependency tries for chunks using pure ES6 imports and tree shaking works properly, resulting in smaller chunks and proper hydration.

from loadable-components.

dglozic avatar dglozic commented on June 15, 2024 1

You can do (1) and (2) with Webpack alone, for UI microservices. We do (3) and (4) when creating reusable React components - I am sorry for putting them together as 1-4. Again, you don't have to use rollup for 3/4, but it is useful for us to run the same build with different config and copy to esm and cjs dist folders, to be consumed on the client and server, respectively.

from loadable-components.

gandazgul avatar gandazgul commented on June 15, 2024

My issue is the hydration, without any warnings or errors creates another duplicate layout instead of hydrating the server html. Both generated HTMLs are identical and react doesn't complaint about differences.

The layout itself is being loaded with loadable. One thing that's different from all examples is that I created a loader for loadable components:

loader.js

function loadComponent(name) {
  switch(name) {
    case 'ComponentA': {
      const Component = loadable(() => /*webpackChunkName: componentA */ import(@namespace/componentA));
    
      return {
        Component, // render this
        load: Component.load, // used to hookup other things to the framework
        name: 'ComponentA'
      }
    }
  }
}

App.jsx

import loadComponent from loader.js;

function App({layout}) {
return (<div>{loadComponent(layout).Component}</div>);
}

entry.js

loadableReady(() => { hydrateRoot(d.gebid('root'), <App layout={config.layout} />) });

from loadable-components.

theKashey avatar theKashey commented on June 15, 2024

Yeah, hydration with Suspense can lead to duplicated content instead of hydration failure.
Everything depends on details and without these details, it's hard to help here.

from loadable-components.

gandazgul avatar gandazgul commented on June 15, 2024

@theKashey thank you for your response. I was able to figure out my issue. It was babel preset-env i made modules set to commonjs this was changing the imports and preventing webpack from splitting. I had stopped the client from processing the loader with babel and this was causing the difference between server and client render.

If you are having issues with loadable components not getting split change preset-env modules from commonjs to the default 'auto'.

from loadable-components.

gandazgul avatar gandazgul commented on June 15, 2024

We do 1 and 2. 2 via npm packages (private repo) that are the raw es6 code without processing. Server and client import them and we have 2 webpack configs one for server and one for client.

I don't understand your 3rd and 4th steps? Do you process the packages again with a server side config and commonjs? Do you publish these separately to a repo?

How do you use roll-up? To build the final application? Why roll-up?

from loadable-components.

stale avatar stale commented on June 15, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from loadable-components.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.