react-jobs icon indicating copy to clipboard operation
react-jobs copied to clipboard

Run only once on server

Open oyeanuj opened this issue 6 years ago • 3 comments

Hey again @ctrlplusb, I was wondering if you think there should be a way for a job to run only once on the server (and not again on the client). Testing with some pages, I realized that for search engines, I was getting the LoadingComponent even when the job was completed on the server. It seems that code does get into withJob and the jobState.completed is not set to true on the client. So, it needs one additional pass to process some custom logic that I provided in the workDefinition to ensure that the jobState is considered complete.

Thoughts on if it would be possible to remember jobState from the server when evaluating the work on client?

oyeanuj avatar Feb 24 '18 23:02 oyeanuj

@ctrlplusb On further investigation, it seems that the job is re-running because the window.jobState is empty:

window.__JOBS_STATE__={"jobs":{}}

This seems to happen if the job is resolved synchronously, as the this.context.jobs.register line below never gets called: https://github.com/ctrlplusb/react-jobs/blob/7268262fa77d69edb0baf629e4a0961ef6ec3d31/src/withJob.js#L132

That is not a problem on the server-side but on the client side, it will try to re-evaluate the work definition, which means there will be a render of just the loading (at the time of mount). Unfortunately, that breaks the experience for SEO, since that's what the crawler sees even though it gets fixed in the next render phase.

I think the best way to fix this bug would be to consistently register job result regardless of whether it is synchronous or asynchronous (not sure about the side-effects of that)

Here is how that looks in my fork: https://github.com/oyeanuj/react-jobs/commit/5a9d5ce47eaa3d2f218f3edbdb7d49132a53aaaa

If this looks good, I'd be happy to make a PR!

oyeanuj avatar Apr 08 '18 17:04 oyeanuj

Hey @oyeanuj

Great catch! Yes, please can you create a PR.

👍

ctrlplusb avatar Aug 10 '18 12:08 ctrlplusb

FYI it may be best to pull the latest version of the library and make your change. I changed up the build process quite a bit.

ctrlplusb avatar Aug 10 '18 12:08 ctrlplusb