electrode-react-ssr-caching icon indicating copy to clipboard operation
electrode-react-ssr-caching copied to clipboard

can not patch react code

Open lonelyclick opened this issue 9 years ago • 9 comments

This is the case with me: I have a dist.js generated by the webpack, in order not to be affected by the impact of webpack, I create a new file main.js: (webpack entry is dist.src.js and output is dist.js

main.js

global.SSRCaching = require('electrode-react-ssr-caching')
require('./dist')

dist.src.js

import React from 'react' // for jsx
import {renderToString} from 'react-dom/server' // for react server render

// some logic

SSRCaching.clearProfileData()
SSRCaching.enableProfiling()
SSRCaching.enableCachingDebug()

renderToString(<MyApp />)

SSRCaching.enableProfiling(false);
console.log(JSON.stringify(SSRCaching.profileData, null, 2))

Start command is node main.js

It is frustrating that:

node_modules/electrode-react-ssr-caching/lib/ssr-caching.js

ReactCompositeComponent.construct = function (element) {
  console.log('execute constructor .......')
  if (config.enabled) {
    if (config.profiling) {
      this.__p = {time: undefined};
      this.__profileTime = this.__realProfileTime;
    }
    this._name = element.type && typeof element.type !== "string" && element.type.name;
    this.mountComponent = this._name ? this.mountComponentCache : this._mountComponent;
  }
  return this._construct(element);
};

execute constructor ....... never apper

lonelyclick avatar Nov 26 '16 08:11 lonelyclick

Excuse me, what is the progress now ? @ananavati

lonelyclick avatar Dec 01 '16 04:12 lonelyclick

@lonelyclick We will let you know once we have more info.

Do you see any other errors and are you running node in production env?

ananavati avatar Dec 01 '16 06:12 ananavati

I was running in the development mode, there are webpack integration. However, after I read the source code, I believe that the development model and the deployment model is no different. I understand that the development model should also be able to patch the react. But always fail. So the essence of the problem is how we can be elegant to react patch.

lonelyclick avatar Dec 01 '16 06:12 lonelyclick

In order for the enableCaching() method to work, you'll also need NODE_ENV set to production, or else it will throw an error.

as mentioned in README/docs pre-requisite/by-design to reassigning props is running production env https://github.com/electrode-io/electrode-react-ssr-caching#caching

refer to this closed ticket: https://github.com/electrode-io/electrode-react-ssr-caching/issues/14

a good stackoverflow reference on why patching/updating props doesn't work in react http://stackoverflow.com/questions/24939623/can-i-update-a-components-props-in-react-js

ananavati avatar Dec 01 '16 06:12 ananavati

Thank you very much, but I still not work.

NODE_ENV=production node main.js

lonelyclick avatar Dec 01 '16 08:12 lonelyclick

@lonelyclick I encountered the same issue when using webpack, and I just fixed that by setting 'react-dom/server' as external in webpack config.

TerenceGe avatar Dec 10 '16 17:12 TerenceGe

cool, it's worked ! @TerenceGe

I think it should be added to the document, the webpack environment to explain @ananavati

lonelyclick avatar Dec 12 '16 05:12 lonelyclick

@TerenceGe Can you make an example please?

maxpain avatar Dec 14 '16 03:12 maxpain

fwiw i just ran into the same problem and had to specify react-dom/server as an external dep in webpack (like @TerenceGe suggested), but additionally had to use webpack's BannerPlugin to ensure electrode is required before.

example webpack config:

module.exports = {
  entry: '...',
  output: '...',
  externals: ['react-dom/server'],
  plugins: [
    new webpack.BannerPlugin({
      banner: 'global.SSRCaching = require("electrode-react-ssr-caching")',
      raw: true,
      entryOnly: true
    })
  ]
};

Peleg avatar Jan 30 '17 22:01 Peleg