markfinger / python-react

Server-side rendering of React components
MIT License
1.62k stars 116 forks source link

Reload components on file changes #12

Closed matthewryanscott closed 9 years ago

matthewryanscott commented 9 years ago

Is there a recommended way of reloading the node server when source code for a module changes?

It seems to have something to do with using require: https://github.com/markfinger/django-react/blob/47f4774e291151854c1752ce6223a400a0e18356/django_react/renderer.js#L21

Once it requires it a single time, there is probably a cache being kept somewhere, I'm guessing. (Still pretty new to nodejs)

markfinger commented 9 years ago

I'll leave this open, re: https://github.com/markfinger/django-react/pull/13#issuecomment-74470222

markfinger commented 9 years ago

Hmm, rather than rolling our own custom cache invalidator, a possibility would be using webpack to bundle the component.

If you use Webpack in a persistent process, they have a bunch of infrastructure for detecting file changes and rebuilding only the parts of the bundle which have changed.

markfinger commented 9 years ago

@gldnspud during development I've been running ./manage.py start_node_server in another terminal.

There's an overhead to initially building + watching a component, but running the server in separate process means that it'll persist when the python server restarts and the rendering will stay pretty performant.

Just a heads up that the master branches of django-node/django-webpack/django-react have all diverged pretty heavily from latest stable releases. I should be able to wrap up the changes in the next week or so, and focus on the examples/docs.

Cheers, Mark

bobwatcherx commented 1 year ago

how to reload the render_server.js file so that it reloads automatically and there is no need to re-run the render_server.js node. in your code example which is basic_rendering

markfinger commented 1 year ago

The simplest approach would be using a parent process runs the renderer in a child process. The parent uses a file watcher to detect changes and then uses the change events to restart the child.

I'm not sure if there are any examples of this sort of system. Node has some built-in file watching capabilities. There are also some commonly used libs: chokidar, watchman, etc. I have a very vague memory of a specific CLI which was designed for this purpose, but this is many years ago.

Note that I'd recommend against using a render server when your code is changing regularly, see: https://github.com/markfinger/python-react#usage-in-development. Serverside rendering is an optimisation intended for specific situations in production environments, so using it in a development environment adds a lot of overheads for little gain.