angular / preboot

Coordinate transfer of state from server to client view for isomorphic/universal JavaScript web applications
MIT License
382 stars 51 forks source link

Preboot suffers from race conditions #82

Closed mgol closed 6 years ago

mgol commented 6 years ago

Note: for support questions, please use the Universal Slack Channcel or https://gitter.im/angular/universal

A bug.

Currently Preboot suffers from various race conditions:

  1. It waits for the document.body to be available and then applies its logic. However, the application root may not be available at this point - see issue #72.
  2. Even if application root exists when Preboot starts listening for events the server node may not be available in full yet. Since Preboots searches for nodes matching desired selectors inside of the existsing serverNode, it may skip some of them if the server node hasn't loaded fully. This is especially common if the internet connection is slow.
  3. Preboot waits for body in a tight loop, checking every 10 milliseconds. This starves the browser but may also be clamped by it to a larger delay (around 1 second); that's what happens in modern browsers if the tab where the page loads is inactive, e.g. if you open a link in a new tab in Chrome or Firefox. This means Preboot may start listening for events after Angular reports the app is stable and the PrebootComplete event fires. This then leaves the server node active, never transferring to the client one which makes for a broken site.

I don't have a quick test case at hand; however, the description from the previous point shows how to reproduce it.

Preboot shouldn't suffer from race conditions.

The current behavior is buggy as described above.

  1. Since we want to attach event listeners as soon as possible when the server node starts being available (so that it can be shallow-cloned) we cannot wait for it to be available in full. Therefore, we need to use delegated event handlers on document instead of on specific nodes.
  2. As we support multiple app roots, to not duplicate large inline preboot code, we'll split getInlinePrebootCode into two parts: function definitions to be put in <head> and the invocation separate for each app root put just after the app root opening tag. We'll maintain getInlinePrebootCode for backwards compatibility but we'll no longer use it internally.
  3. To maintain children offset numbers (otherwise selectors won't match between the client & the server) we'll remove the in-app-root script immediately when it starts executing; this won't stop execution. document.currentScript is a good way to find the currently running script but it doesn't work in IE. We'll need to apply a fallback behavior for IE, perhaps by tagging each script with a unique attribute value and injecting this value as a variable into the script.
  4. We'll remove waitUntilReady as there's no reliable way to wait; we'll invoke the init code immediately instead.
  5. We'll attach the overlay used for freeing the UI to document.documentElement instead of document.body so that we don't have to wait for body.
mgol commented 6 years ago

PR: #83.

arutnik commented 6 years ago

Can anyone look at this PR? With version 6.0.0-beta.3 the race conditions are quite apparent. With the preboot module my app will randomly transition from a full server page to a blank client page with no errors in the console. Without the preboot module there's a flash but everything loads fine. I would love to be able to use this!

CaerusKaru commented 6 years ago

@arutnik It's a WIP, and @mgol is OOTO for most of the month. I apologize for the delay, but that's the unfortunate nature of open source projects.