When writing code for realtime features we have to keep a couple of things in mind:
- Do not overload the server with requests.
- It should feel realtime.
Thus, we must strike a balance between sending requests and the feeling of realtime. Use the following rules when creating realtime solutions.
- The server will tell you how much to poll by sending
Poll-Intervalin the header. Use that as your polling interval. This way it is easy for system administrators to change the polling rate. A
Poll-Interval: -1means you should disable polling, and this must be implemented.
- A response with HTTP status
5XXshould disable polling as well.
- Use a common library for polling.
- Poll on active tabs only. Please use Visibility.
- Use regular polling intervals, do not use backoff polling, or jitter, as the interval will be controlled by the server.
- The backend code will most likely be using etags. You do not and should not check for status
304 Not Modified. The browser will transform it for you.
To improve the time to first render we are using lazy loading for images. This works by setting
the actual image source on the
the value of
data-src will be moved to
src automatically if the image is in the current viewport.
- Prepare images in HTML for lazy loading by renaming the
data-srcAND adding the class
- If you are using the Rails
image_taghelper, all images will be lazy-loaded by default unless
lazy: falseis provided.
If you are asynchronously adding content which contains lazy images then you need to call the function
gl.lazyLoader.searchLazyImages() which will search for lazy images and load them if needed.
But in general it should be handled automatically through a
MutationObserver in the lazy loading function.
transform properties. Other properties (such as
padding) all cause
Layout to be recalculated, which is much more expensive. For details on this, see "Styles that Affect Layout" in
High Performance Animations.
If you do need to change layout (e.g. a sidebar that pushes main content over), prefer FLIP to change expensive properties once, and handle the actual animation with transforms.
Reducing Asset Footprint
In cases where libraries are only used on a few specific pages, we use
main.js file from
becoming unnecessarily large.
- Create a directory for the specific page(s), e.g.
- In that directory, create a
- Add the new "bundle" file to the list of entry files in
- For example:
- For example:
- Move code reliant on these libraries into the
require('./path_to_some_component.js');statements to load any other files in this directory. Make sure to use relative urls.
- In the relevant views, add the scripts to the page with the following:
The above loads
graphs_bundle.js for this page only.
is separated from the bundle file so it can be cached separately from the bundle
and reused for other pages that also rely on the library. For an example, see
this Haml file.
TODO flesh out this section once webpack is ready for code-splitting
Minimizing page size
A smaller page size means the page loads faster (especially important on mobile and poor connections), the page is parsed more quickly by the browser, and less data is used for users with capped data plans.
- Don't add new fonts.
- Prefer font formats with better compression, e.g. WOFF2 is better than WOFF, which is better than TTF.
- Compress and minify assets wherever possible (For CSS/JS, Sprockets and webpack do this for us).
- If some functionality can reasonably be achieved without adding extra libraries, avoid them.
- High Performance Animations