render_async lets you include pages asynchronously with AJAX
render_async lets you include pages asynchronously with AJAX
Welcome to render_async
Let's make your Rails pages fast again :racehorse:
render_async is here to make your pages show faster to users.Pages become faster seamlessly by rendering partials to your views.
Partials render asynchronously and let users see your page faster than using regular rendering.
It works with Rails and its tools out of the box.
:sparkles: A quick overview of how render_async does its magic:
render_async makes an AJAX request on the controller actionrender_async view helperJavaScript is injected straight into `` so you choose where to put it.
:mega: P.S. Join our Discord channel for help and discussion, and let's make render_async even better!
Add this line to your application's Gemfile:
gem 'render_async'And then execute:
$ bundle installInclude render_async view helper somewhere in your views (e.g. app/views/comments/show.html.erb):
Then create a route for it config/routes.rb:
get :comment_stats, controller: :commentsFill in the logic in your controller (e.g. app/controllers/comments_controller.rb):
def comment_stats
@stats = Comment.get_stats
render partial: "comment_stats"
endCreate a partial that will render (e.g. app/views/comments/_comment_stats.html.erb):
Add content_for in your base view file in the body part (e.g. app/views/layouts/application.html.erb):
Advanced usage includes information on different options, such as:
render_async renders an element that gets replaced with the content
of your request response. In order to have more control over the element
that renders first (before the request), you can set the ID of that element.
To set ID of the container element, you can do the following:
Rendered code in the view:
...render_async renders an element that gets replaced with the content of your
request response. If you want to style that element, you can set the class name
on it.
Rendered code in the view:
...render_async can accept html_options as a hash.
html_options is an optional hash that gets passed to a Rails'
javascript_tag, to drop HTML tags into the script element.
Example of utilizing html_options with a nonce:
Rendered code in the view:
...
:bulb: You can enable
nonceto be set everywhere by using configuration option render_async provides.
render_async can take in an HTML element name, allowing you to control
what type of container gets rendered. This can be useful when you're using
render_async inside a table
and you need it to render a tr element before your request gets loaded, so
your content doesn't get pushed out of the table.
Example of using HTML element name:
Rendered code in the view:
...render_async can be called with a block that will act as a placeholder before
your AJAX call finishes.
Example of passing in a block:
Users are loading...
Rendered code in the view:
After AJAX is finished, placeholder will be replaced with the request's response.
render_async can receive :event_name option which will emit JavaScript
event after it's done with fetching and rendering request content to HTML.
This can be useful to have if you want to add some JavaScript functionality
after your partial is loaded through render_async.
You can also access the associated container (DOM node) in the event object that gets emitted.
Example of passing it to render_async:
Rendered code in view:
Then, in your JavaScript code, you could do something like this:
document.addEventListener("users-loaded", function(event) {
console.log("Users have loaded!", event.container); // Access the container which loaded the users
});:bulb: Dispatching events is also supported for older browsers that don't support Event constructor.
render_async will fire the event render_async_load when an async partial
has loaded and rendered on the page.
In case there is an error, the event render_async_error will fire instead.
This event will fire for all render_async partials on the page. For every
event, the associated container (DOM node) will be passed along.
This can be useful to apply JavaScript to content loaded after the page is ready.
Example of using events:
…render_async lets you refresh (reload) the partial by letting you dispatch
the 'refresh' event on the render_async's container. An example:
Refresh comments
If you follow the example above, when you click "Refresh comments" button,
render_async will trigger again and reload the comments_path.
:bulb: Note that you need to pass
replace_container: falseso you can later dispatch an event on that container.
render_async can retry your requests if they fail for some reason.
If you want render_async to retry a request for number of times, you can do
this:
Now render_async will retry users_path for 5 times. If it succeeds in
between, it will stop with dispatching requests. If it fails after 5 times,
it will show an error message which you need to specify.
This can show useful when you know your requests often fail, and you don't want to refresh the whole page just to retry them.
If you want to retry requests but with some delay in between the calls, you can
pass a retry_delay option together with retry_count like so:
This will make render_async wait for 2 seconds before retrying after each
failure. In the end, if the request is still failing after 5th time, it will
dispatch a default error event.
:candy: If you are catching an event after an error, you can get
retryCountfrom the event.retryCountwill have the number of retries it took before the event was dispatched.
Here is an example on how to get retryCount:
If you need to pass retry count to the backend, you can pass retry_count_header in render_async's options:
And then in controller you can read the value from request headers.
request.headers['Retry-Count-Current']&.to_iYou can trigger render_async loading by clicking or doing another event to a
certain HTML element. You can do this by passing in a selector and an event
name which will trigger render_async. If you don't specify an event name, the
default event that will trigger render_async will be 'click' event. You can
do this by doing the following:
Load comments
This will trigger render_async to load the comments_path when you click the #comments-button element.
If you want to remove an event once it's triggered, you can pass once: true in the toggle options.
The once option is false (nil) by default.
You can also pass in a placeholder before the render_async is triggered. That
way, the element that started render_async logic will be removed after the
request has been completed. You can achieve this behaviour with something like this:
Load comments
Also, you can mix interval and toggle features. This way, you can turn polling
on, and off by clicking the "Load comments" button. In order to do this, you need to
pass toggle and interval arguments to render_async call like this:
Load comments
If you want render_async to render the request on load, you can pass start: true. Passing the start option inside the toggle hash will trigger
render_async on page load. You can then toggle off polling by interacting
with the element you specified. An example:
Toggle comments loading
In the example above, the comments will load as soon as the page is rendered. Then, you can stop polling for comments by clicking the "Toggle comments loading" button.
You can call render_async with interval argument. This will make render_async
call specified path at the specified interval.
By doing this:
You are telling render_async to fetch comments_path every 5 seconds.
This can be handy if you want to enable polling for a specific URL.
:warning: By passing interval to
render_async, the initial container element will remain in the HTML tree and it will not be replaced with request response. You can handle how that container element is rendered and its style by passing in an HTML element name and HTML element class.
You can controller render_async polling in 2 manners.
First one is pretty simple, and it involves using the toggle
feature. To do this, you can follow instructions in the
control polling with a toggle section.
The second option is more advanced and it involves emitting events to the render_async's
container element. From your code, you can emit the following events:
:bulb: Please note that events need to be dispatched to a render_async container.
An example of how you can do this looks like this:
Stop polling
Start polling
We are rendering two buttons - "Stop polling" and "Start polling". Then, we attach an event listener to catch any clicking on the buttons. When the buttons are clicked, we either stop the polling or start the polling, depending on which button a user clicks.
render_async lets you handle errors by allowing you to pass in error_message
and error_event_name.
error_message
passing an error_message will render a messag
No open issues yet, or sync has not completed.