render_async 2.1.7 → 2.1.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 80f65d684e925ba32249eef7d5dd817b268b72dfebd0ecd9525fbe544d16af57
4
- data.tar.gz: 46b675280ceb449a68953e9a2833332ff74222d773a3aa66ec95036d25587273
3
+ metadata.gz: 5eb3554f389bd0bcab8944c56c59238359d1c181c7dbc212d12e0335a9960fa6
4
+ data.tar.gz: a00cfb7925232e67825579356c33707d97258d369d41d92f676077c6e0b1d17e
5
5
  SHA512:
6
- metadata.gz: 3bdd39f73528d3b9a8035ab5c8629b9c5261186f1d3eae2d5b82b856942c0ae602d5c670d96e009a240744fe5e7d93a97136298f05ef55c010f7d6f7972b4f40
7
- data.tar.gz: a73351396f72b02895c03a4ff20068f096ae71279adc7345518d31e26111f63ac46b5b76424a24e566a7a0268f1748d1c1c364489e17d030dea12a691cc227eb
6
+ metadata.gz: acd95e67267622528c723d41ba5dfe1114319173f3455004523fd0dab784d8626a21314a389373f473d5147465e29773defb75c40d7ff6cb15053b7eba44e1a2
7
+ data.tar.gz: ccb2a071554bdca88a02b8b3b178711b01d116067eda7d48f01385056459cd3f59e8561d489ce76380be26ad8979398f40300759915c07962899aef608751e24
data/.all-contributorsrc CHANGED
@@ -138,7 +138,8 @@
138
138
  "avatar_url": "https://avatars3.githubusercontent.com/u/1935686?v=4",
139
139
  "profile": "https://github.com/reneklacan",
140
140
  "contributions": [
141
- "code"
141
+ "code",
142
+ "doc"
142
143
  ]
143
144
  },
144
145
  {
@@ -205,6 +206,35 @@
205
206
  "contributions": [
206
207
  "code"
207
208
  ]
209
+ },
210
+ {
211
+ "login": "villu164",
212
+ "name": "villu164",
213
+ "avatar_url": "https://avatars0.githubusercontent.com/u/998682?v=4",
214
+ "profile": "https://github.com/villu164",
215
+ "contributions": [
216
+ "doc"
217
+ ]
218
+ },
219
+ {
220
+ "login": "Mbuckley0",
221
+ "name": "Mitchell Buckley",
222
+ "avatar_url": "https://avatars.githubusercontent.com/u/11203679?v=4",
223
+ "profile": "https://github.com/Mbuckley0",
224
+ "contributions": [
225
+ "code",
226
+ "doc"
227
+ ]
228
+ },
229
+ {
230
+ "login": "yhirano55",
231
+ "name": "yhirano55",
232
+ "avatar_url": "https://avatars.githubusercontent.com/u/15371677?v=4",
233
+ "profile": "https://github.com/yhirano55",
234
+ "contributions": [
235
+ "code",
236
+ "doc"
237
+ ]
208
238
  }
209
239
  ],
210
240
  "repoType": "github"
@@ -0,0 +1,40 @@
1
+ # :pray: Contributing
2
+
3
+ Thank you for considering or deciding to contribute, this is much appreciated!
4
+ Any kind of bug reports and pull requests are encouraged and welcome on GitHub at
5
+ https://github.com/renderedtext/render_async.
6
+
7
+ ## :inbox_tray: Installing dependencies
8
+
9
+ You can install all needed dependencies by running `bin/setup`.
10
+
11
+ ## :runner: Running RSpec tests
12
+
13
+ You can run either `rake spec` or `bundle exec rspec` to run all the RSpec tests
14
+ in the project.
15
+
16
+ ## :running_woman: Running integration tests
17
+
18
+ There is a simple command `bin/integration-tests` which sets up 2 submodules,
19
+ and runs Cucumber features in them.
20
+
21
+ There are 2 submodules for render_async. The submodules are Rails 5 and Rails 6
22
+ projects which are located in:
23
+
24
+ - `spec/fixtures/rails-5-base-app`, and
25
+ - `spec/fixtures/rails-6-base-app`.
26
+
27
+ You can find [Rails 5 base app here](https://github.com/nikolalsvk/rails-5-base-app/tree/render-async),
28
+ and the [Rails 6 base app here](https://github.com/nikolalsvk/rails-6-base-app/tree/render-async).
29
+
30
+ Each of them have different use cases of render_async defined in `app/views/render_asyncs/_use_cases.html.erb` in their repos.
31
+ All the feature tests are inside `features/render_async.feature` and `features/render_async_jquery.feature` files.
32
+
33
+ If you are adding one or more feature tests or use cases, make sure to make a
34
+ PR on those repos as well and include them in the PR on the render_async repo.
35
+
36
+ ## :sos: Need help?
37
+
38
+ Got any issues or difficulties?
39
+ Join [render_async's Discord channel](https://discord.gg/SPfbeRm)
40
+ and ask questions there. We will try to respond to you as quickly as possible.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .DS_Store
data/.gitmodules CHANGED
@@ -2,3 +2,7 @@
2
2
  path = spec/fixtures/rails-5-base-app
3
3
  url = git@github.com:nikolalsvk/rails-5-base-app.git
4
4
  branch = render-async
5
+ [submodule "spec/fixtures/rails-6-base-app"]
6
+ path = spec/fixtures/rails-6-base-app
7
+ url = git@github.com:nikolalsvk/rails-6-base-app.git
8
+ branch = render-async
data/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
1
+ ### 2.1.10 (2021/03/21)
2
+
3
+ * [#146](https://github.com/renderedtext/render_async/pull/146): Add comment when we check if container is present - [@nikolalsvk](https://github.com/nikolalsvk).
4
+ * [#145](https://github.com/renderedtext/render_async/pull/145): Wrap html_options for turbolinks - [@yhirano55](https://github.com/yhirano55).
5
+ * [#144](https://github.com/renderedtext/render_async/pull/144): Avoid TypeError if container has already disappear - [@yhirano55](https://github.com/yhirano55).
6
+
7
+ ### 2.1.9 (2021/02/23)
8
+
9
+ * [#142](https://github.com/renderedtext/render_async/pull/142): Update Turbolinks docs - [@nikolalsvk](https://github.com/nikolalsvk).
10
+ * [#141](https://github.com/renderedtext/render_async/pull/141): Add Support for Turbo - [@MBuckley0](https://github.com/Mbuckley0).
11
+ * [#139](https://github.com/renderedtext/render_async/pull/139): Fix readme configuration-options anchor - [@richardvenneman](https://github.com/richardvenneman).
12
+ * [#138](https://github.com/renderedtext/render_async/pull/138): Add Rails 6 base app as a fixture - [@nikolalsvk](https://github.com/nikolalsvk).
13
+ * [#137](https://github.com/renderedtext/render_async/pull/137): Rename config - [@nikolalsvk](https://github.com/nikolalsvk).
14
+
15
+ ### 2.1.8 (2020/10/24)
16
+
17
+ * [#134](https://github.com/renderedtext/render_async/pull/134): Add config option for setting nonce - [@nikolalsvk](https://github.com/nikolalsvk).
18
+ * [#132](https://github.com/renderedtext/render_async/pull/132): Refresh render_async with an event - [@nikolalsvk](https://github.com/nikolalsvk).
19
+ * [#131](https://github.com/renderedtext/render_async/pull/131): Start to poll on page load with toggle - [@nikolalsvk](https://github.com/nikolalsvk).
20
+ * [#130](https://github.com/renderedtext/render_async/pull/130): Set up control events after document loaded - [@nikolalsvk](https://github.com/nikolalsvk).
21
+ * [#127](https://github.com/renderedtext/render_async/pull/127): Update README.md, to reflect correct turbolinks configuration value - [@villu164](https://github.com/villu164).
22
+
1
23
  ### 2.1.7 (2020/8/1)
2
24
 
3
25
  * [#125](https://github.com/renderedtext/render_async/pull/125): Implement retry after some time feature - [@nikolalsvk](https://github.com/nikolalsvk).
data/README.md CHANGED
@@ -82,7 +82,7 @@ And then execute:
82
82
  <%= render_async comment_stats_path %>
83
83
  ```
84
84
 
85
- 2. Then create a route that will `config/routes.rb`:
85
+ 2. Then create a route for it `config/routes.rb`:
86
86
  ```ruby
87
87
  get :comment_stats, controller: :comments
88
88
  ```
@@ -119,6 +119,7 @@ Advanced usage includes information on different options, such as:
119
119
  - [Passing in a placeholder](#passing-in-a-placeholder)
120
120
  - [Passing in an event name](#passing-in-an-event-name)
121
121
  - [Using default events](#using-default-events)
122
+ - [Refreshing the partial](#refreshing-the-partial)
122
123
  - [Retry on failure](#retry-on-failure)
123
124
  - [Retry after some time](#retry-after-some-time)
124
125
  - [Toggle event](#toggle-event)
@@ -129,10 +130,11 @@ Advanced usage includes information on different options, such as:
129
130
  - [Caching](#caching)
130
131
  - [Doing non-GET requests](#doing-non-get-requests)
131
132
  - [Using with Turbolinks](#using-with-turbolinks)
133
+ - [Using with Turbo](#using-with-turbo)
132
134
  - [Using with respond_to and JS format](#using-with-respond_to-and-js-format)
133
- - [Nested Async Renders](#nested-async-renders)
135
+ - [Nested async renders](#nested-async-renders)
134
136
  - [Customizing the content_for name](#customizing-the-content_for-name)
135
- - [Configuration](#configuration)
137
+ - [Configuration options](#configuration-options)
136
138
 
137
139
  ### Passing in a container ID
138
140
 
@@ -178,6 +180,7 @@ Rendered code in the view:
178
180
  `javascript_tag`, to drop HTML tags into the `script` element.
179
181
 
180
182
  Example of utilizing `html_options` with a [nonce](https://edgeguides.rubyonrails.org/security.html#content-security-policy):
183
+
181
184
  ```erb
182
185
  <%= render_async users_path, html_options: { nonce: true } %>
183
186
  ```
@@ -196,6 +199,8 @@ Rendered code in the view:
196
199
  </div>
197
200
  ```
198
201
 
202
+ > :bulb: You can enable `nonce` to be set everywhere by using [configuration option](#configuration-options) render_async provides.
203
+
199
204
  ### Passing in an HTML element name
200
205
 
201
206
  `render_async` can take in an HTML element name, allowing you to control
@@ -287,7 +292,7 @@ document.addEventListener("users-loaded", function(event) {
287
292
  ### Using default events
288
293
 
289
294
  `render_async` will fire the event `render_async_load` when an async partial
290
- has loaded and rendered on page.
295
+ has loaded and rendered on the page.
291
296
 
292
297
  In case there is an error, the event `render_async_error` will fire instead.
293
298
 
@@ -317,6 +322,36 @@ $(document).on('render_async_error', function(event) {
317
322
  });
318
323
  ```
319
324
 
325
+ ### Refreshing the partial
326
+
327
+ `render_async` lets you refresh (reload) the partial by letting you dispatch
328
+ the 'refresh' event on the `render_async`'s container. An example:
329
+
330
+ ```erb
331
+ <%= render_async comments_path,
332
+ container_id: 'refresh-me',
333
+ replace_container: false %>
334
+
335
+ <button id="refresh-button">Refresh comments</button>
336
+
337
+ <script>
338
+ var button = document.getElementById('refresh-button')
339
+ var container = document.getElementById('refresh-me');
340
+
341
+ button.addEventListener('click', function() {
342
+ var event = new Event('refresh');
343
+
344
+ // Dispatch 'refresh' on the render_async container
345
+ container.dispatchEvent(event)
346
+ })
347
+ </script>
348
+ ```
349
+
350
+ If you follow the example above, when you click "Refresh comments" button,
351
+ `render_async` will trigger again and reload the `comments_path`.
352
+
353
+ > :bulb: Note that you need to pass `replace_container: false` so you can later dispatch an event on that container.
354
+
320
355
  ### Retry on failure
321
356
 
322
357
  `render_async` can retry your requests if they fail for some reason.
@@ -350,7 +385,7 @@ failure. In the end, if the request is still failing after 5th time, it will
350
385
  dispatch a [default error event](#using-default-events).
351
386
 
352
387
  > :candy: If you are catching an event after an error, you can get `retryCount` from
353
- the event. `retryCount` will have number of retries it took before the event was dispatched.
388
+ the event. `retryCount` will have the number of retries it took before the event was dispatched.
354
389
 
355
390
  Here is an example on how to get `retryCount`:
356
391
 
@@ -367,6 +402,20 @@ Here is an example on how to get `retryCount`:
367
402
  </script>
368
403
  ```
369
404
 
405
+ If you need to pass retry count to the backend, you can pass `retry_count_header` in `render_async`'s options:
406
+
407
+ ```erb
408
+ <%= render_async users_path,
409
+ retry_count: 5,
410
+ retry_count_header: 'Retry-Count-Current' %>
411
+ ```
412
+
413
+ And then in controller you can read the value from request headers.
414
+
415
+ ```
416
+ request.headers['Retry-Count-Current']&.to_i
417
+ ```
418
+
370
419
  ### Toggle event
371
420
 
372
421
  You can trigger `render_async` loading by clicking or doing another event to a
@@ -381,8 +430,8 @@ do this by doing the following:
381
430
  ```
382
431
 
383
432
  This will trigger `render_async` to load the `comments_path` when you click the `#comments-button` element.
384
- If you want to remove event once it's triggered, you can pass `once: true` in the toggle options.
385
- The `once` option is false by default.
433
+ If you want to remove an event once it's triggered, you can pass `once: true` in the toggle options.
434
+ The `once` option is false (`nil`) by default.
386
435
 
387
436
  You can also pass in a placeholder before the `render_async` is triggered. That
388
437
  way, the element that started `render_async` logic will be removed after the
@@ -405,10 +454,28 @@ pass `toggle` and `interval` arguments to `render_async` call like this:
405
454
  <%= render_async comments_path, toggle: { selector: '#comments-button', event: :click }, interval: 2000 %>
406
455
  ```
407
456
 
457
+ If you want `render_async` to render the request on load, you can pass `start:
458
+ true`. Passing the `start` option inside the `toggle` hash will trigger
459
+ `render_async` on page load. You can then toggle off polling by interacting
460
+ with the element you specified. An example:
461
+
462
+ ```erb
463
+ <a href='#' id='comments-button'>Toggle comments loading</a>
464
+ <%= render_async comments_path,
465
+ toggle: { selector: '#comments-button',
466
+ event: :click,
467
+ start: true },
468
+ interval: 2000 %>
469
+ ```
470
+
471
+ In the example above, the comments will load as soon as the page is rendered.
472
+ Then, you can stop polling for comments by clicking the "Toggle comments
473
+ loading" button.
474
+
408
475
  ### Polling
409
476
 
410
477
  You can call `render_async` with interval argument. This will make render_async
411
- call specified path at specified interval.
478
+ call specified path at the specified interval.
412
479
 
413
480
  By doing this:
414
481
  ```erb
@@ -418,8 +485,8 @@ You are telling `render_async` to fetch comments_path every 5 seconds.
418
485
 
419
486
  This can be handy if you want to enable polling for a specific URL.
420
487
 
421
- > :warning: By passing interval to `render_async`, initial container element
422
- > will remain in HTML tree, it will not be replaced with request response.
488
+ > :warning: By passing interval to `render_async`, the initial container element
489
+ > will remain in the HTML tree and it will not be replaced with request response.
423
490
  > You can handle how that container element is rendered and its style by
424
491
  > [passing in an HTML element name](#passing-in-an-html-element-name) and
425
492
  > [HTML element class](#passing-in-a-container-class-name).
@@ -431,8 +498,8 @@ First one is pretty simple, and it involves using the [toggle](#toggle-event)
431
498
  feature. To do this, you can follow instructions in the
432
499
  [control polling with a toggle section](#control-polling-with-a-toggle).
433
500
 
434
- Second option is more advanced and it involves emitting events to the `render_async`'s
435
- container element. From your code, you can emit following events:
501
+ The second option is more advanced and it involves emitting events to the `render_async`'s
502
+ container element. From your code, you can emit the following events:
436
503
  - 'async-stop' - this will stop polling
437
504
  - 'async-start' - this will start polling.
438
505
 
@@ -470,13 +537,13 @@ An example of how you can do this looks like this:
470
537
  ```
471
538
 
472
539
  We are rendering two buttons - "Stop polling" and "Start polling". Then, we
473
- attach event listener to catch any clicking on the buttons. When the buttons
474
- are clicked, we either stop the polling, or start the polling, depending which
540
+ attach an event listener to catch any clicking on the buttons. When the buttons
541
+ are clicked, we either stop the polling or start the polling, depending on which
475
542
  button a user clicks.
476
543
 
477
544
  ### Handling errors
478
545
 
479
- `render_async` let's you handle errors by allowing you to pass in `error_message`
546
+ `render_async` lets you handle errors by allowing you to pass in `error_message`
480
547
  and `error_event_name`.
481
548
 
482
549
  - `error_message`
@@ -563,19 +630,49 @@ end
563
630
  ```
564
631
 
565
632
  This way, you're not breaking Turbolinks flow of loading or reloading a page.
566
- It makes it more efficient that the next option that is suggested below.
633
+ It is more efficient than the next option below.
567
634
 
568
635
  Another option:
569
636
  If you want, you can tell Turbolinks to reload your `render_async` call as follows:
570
637
 
571
638
  ```erb
572
- <%= render_async events_path, 'data-turbolinks-track': 'reload' %>
639
+ <%= render_async events_path, html_options: { 'data-turbolinks-track': 'reload' } %>
573
640
  ```
574
641
 
575
642
  This will reload the whole page with Turbolinks.
576
643
 
577
- > :bulb: Make sure to put `<%= content_for :render_async %>` in your base view file in
578
- the `<head>` and not the `<body>`.
644
+ > :bulb: If Turbolinks is misbehaving in some way, make sure to put `<%= content_for :render_async %>` in your base view file in
645
+ the `<body>` and not the `<head>`.
646
+
647
+ ### Using with Turbo
648
+
649
+ On Turbo applications, you may experience caching issues when navigating
650
+ away from, and then back to, a page with a `render_async` call on it. This will
651
+ likely show up as an empty div.
652
+
653
+ If you're using Turbo, you can resolve this by setting Turbo
654
+ configuration of `render_async` to true:
655
+
656
+ ```rb
657
+ RenderAsync.configure do |config|
658
+ config.turbo = true # Enable this option if you are using Turbo
659
+ end
660
+ ```
661
+
662
+ This way, you're not breaking Turbos flow of loading or reloading a page.
663
+ It is more efficient than the next option below.
664
+
665
+ Another option:
666
+ If you want, you can tell Turbo to reload your `render_async` call as follows:
667
+
668
+ ```erb
669
+ <%= render_async events_path, html_options: { 'data-turbo-track': 'reload' } %>
670
+ ```
671
+
672
+ This will reload the whole page with Turbo.
673
+
674
+ > :bulb: If Turbo is misbehaving in some way, make sure to put `<%= content_for :render_async %>` in your base view file in
675
+ the `<body>` and not the `<head>`.
579
676
 
580
677
  ### Using with respond_to and JS format
581
678
 
@@ -594,7 +691,7 @@ def comment_stats
594
691
  end
595
692
  ```
596
693
 
597
- When you do this, Rails will sometime set the response's `Content-Type` header
694
+ When you do this, Rails will sometimes set the response's `Content-Type` header
598
695
  to `text/javascript`. This causes the partial not to be rendered in the HTML.
599
696
  This usually happens when there's browser caching.
600
697
 
@@ -605,7 +702,7 @@ render call:
605
702
  render partial: "comment_stats", content_type: 'text/html'
606
703
  ```
607
704
 
608
- ### Nested Async Renders
705
+ ### Nested async renders
609
706
 
610
707
  It is possible to nest async templates within other async templates. When doing
611
708
  so, another `content_for` is required to ensure the JavaScript needed to load
@@ -645,7 +742,7 @@ For example:
645
742
  <%= content_for :render_async_comment_stats %>
646
743
  ```
647
744
 
648
- ### Configuration
745
+ ### Configuration options
649
746
 
650
747
  `render_async` renders Vanilla JS (regular JavaScript, non-jQuery code)
651
748
  **by default** in order to fetch the request from the server.
@@ -655,10 +752,14 @@ so.
655
752
 
656
753
  You can configure it by doing the following anywhere before you call
657
754
  `render_async`:
755
+
658
756
  ```rb
659
757
  RenderAsync.configure do |config|
660
- config.jquery = true # This will render jQuery code, and skip Vanilla JS code
661
- config.turbolinks = false # Enable this option if you are using Turbolinks 5+
758
+ config.jquery = true # This will render jQuery code, and skip Vanilla JS code. The default value is false.
759
+ config.turbolinks = true # Enable this option if you are using Turbolinks 5+. The default value is false.
760
+ config.turbo = true # Enable this option if you are using Turbo. The default value is false.
761
+ config.replace_container = false # Set to false if you want to keep the placeholder div element from render_async. The default value is true.
762
+ config.nonces = true # Set to true if you want render_async's javascript_tag always to receive nonce: true. The default value is false.
662
763
  end
663
764
  ```
664
765
 
@@ -668,12 +769,20 @@ Also, you can do it like this:
668
769
  RenderAsync.configuration.jquery = true
669
770
  ```
670
771
 
772
+ Aside from configuring whether the gem relies on jQuery or VanillaJS, you can
773
+ configure other options:
774
+
775
+ - `turbolinks` option - If you are using Turbolinks 5+, you should enable this option since it supports Turbolinks way of loading data. The default value for this option is false.
776
+ - `turbo` option - If you are using Turbo, you should enable this option since it supports Turbo way of loading data. The default value for this option is false.
777
+ - `replace_container` option - If you want render_async to replace its container with the request response, turn this on. You can turn this on globally for all render_async calls, but if you use this option in a specific render_async call, it will override the global configuration. The default value is true.
778
+ - `nonces` - If you need to pass in `nonce: true` to the `javascript_tag` in your application, it might make sense for you to turn this on globally for all render_async calls. To read more about nonces, check out [Rails' official guide on security](https://edgeguides.rubyonrails.org/security.html). The default value is false.
779
+
671
780
  ## :hammer_and_pick: Development
672
781
 
673
782
  After checking out the repo, run `bin/setup` to install dependencies. Then, run
674
783
  `rake spec` to run the tests. You can also run `bin/console` for an interactive
675
784
  prompt that will allow you to experiment. To run integration tests, use
676
- `bin/integration-tests`.
785
+ `bin/integration-tests`. For more information, check out [CONTRIBUTING](.github/CONTRIBUTING.md) file, please.
677
786
 
678
787
  Got any questions or comments about development (or anything else)?
679
788
  Join [render_async's Discord channel](https://discord.gg/SPfbeRm)
@@ -681,9 +790,7 @@ and let's make `render_async` even better!
681
790
 
682
791
  ## :pray: Contributing
683
792
 
684
- Thank you for considering or deciding to contribute, this is much appreciated!
685
- Any kind of bug reports and pull requests are encouraged and welcome on GitHub at
686
- https://github.com/renderedtext/render_async.
793
+ Check out [CONTRIBUTING](.github/CONTRIBUTING.md) file, please.
687
794
 
688
795
  Got any issues or difficulties?
689
796
  Join [render_async's Discord channel](https://discord.gg/SPfbeRm)
@@ -701,8 +808,9 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
701
808
  <!-- prettier-ignore -->
702
809
  | [<img src="https://avatars2.githubusercontent.com/u/3028124?v=4" width="100px;"/><br /><sub><b>Nikola Đuza</b></sub>](https://nikolalsvk.github.io)<br />[💬](#question-nikolalsvk "Answering Questions") [💻](https://github.com/renderedtext/render_async/commits?author=nikolalsvk "Code") [📖](https://github.com/renderedtext/render_async/commits?author=nikolalsvk "Documentation") [👀](#review-nikolalsvk "Reviewed Pull Requests") | [<img src="https://avatars0.githubusercontent.com/u/3866868?v=4" width="100px;"/><br /><sub><b>Colin</b></sub>](http://www.colinxfleming.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=colinxfleming "Code") [📖](https://github.com/renderedtext/render_async/commits?author=colinxfleming "Documentation") [💡](#example-colinxfleming "Examples") | [<img src="https://avatars2.githubusercontent.com/u/334273?v=4" width="100px;"/><br /><sub><b>Kasper Grubbe</b></sub>](http://kaspergrubbe.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=kaspergrubbe "Code") | [<img src="https://avatars2.githubusercontent.com/u/163584?v=4" width="100px;"/><br /><sub><b>Sai Ram Kunala</b></sub>](https://sairam.xyz/)<br />[📖](https://github.com/renderedtext/render_async/commits?author=sairam "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3065882?v=4" width="100px;"/><br /><sub><b>Josh Arnold</b></sub>](https://github.com/nightsurge)<br />[💻](https://github.com/renderedtext/render_async/commits?author=nightsurge "Code") [📖](https://github.com/renderedtext/render_async/commits?author=nightsurge "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/107798?v=4" width="100px;"/><br /><sub><b>Elad Shahar</b></sub>](https://eladshahar.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=SaladFork "Code") [💡](#example-SaladFork "Examples") | [<img src="https://avatars3.githubusercontent.com/u/232392?v=4" width="100px;"/><br /><sub><b>Sasha</b></sub>](http://www.revzin.co.il)<br />[💻](https://github.com/renderedtext/render_async/commits?author=sasharevzin "Code") [📖](https://github.com/renderedtext/render_async/commits?author=sasharevzin "Documentation") |
703
810
  | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
704
- | [<img src="https://avatars3.githubusercontent.com/u/50223?v=4" width="100px;"/><br /><sub><b>Ernest Surudo</b></sub>](http://elsurudo.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=elsurudo "Code") | [<img src="https://avatars1.githubusercontent.com/u/334809?v=4" width="100px;"/><br /><sub><b>Kurtis Rainbolt-Greene</b></sub>](https://kurtis.rainbolt-greene.online)<br />[💻](https://github.com/renderedtext/render_async/commits?author=krainboltgreene "Code") | [<img src="https://avatars2.githubusercontent.com/u/59744?v=4" width="100px;"/><br /><sub><b>Richard Schneeman</b></sub>](https://www.schneems.com)<br />[📖](https://github.com/renderedtext/render_async/commits?author=schneems "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/75705?v=4" width="100px;"/><br /><sub><b>Richard Venneman</b></sub>](https://www.cityspotters.com)<br />[📖](https://github.com/renderedtext/render_async/commits?author=richardvenneman "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/381395?v=4" width="100px;"/><br /><sub><b>Filipe W. Lima</b></sub>](https://github.com/filipewl)<br />[📖](https://github.com/renderedtext/render_async/commits?author=filipewl "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/3135638?v=4" width="100px;"/><br /><sub><b>Jesús Eduardo Clemens Chong</b></sub>](https://github.com/eclemens)<br />[💻](https://github.com/renderedtext/render_async/commits?author=eclemens "Code") | [<img src="https://avatars3.githubusercontent.com/u/1935686?v=4" width="100px;"/><br /><sub><b>René Klačan</b></sub>](https://github.com/reneklacan)<br />[💻](https://github.com/renderedtext/render_async/commits?author=reneklacan "Code") |
811
+ | [<img src="https://avatars3.githubusercontent.com/u/50223?v=4" width="100px;"/><br /><sub><b>Ernest Surudo</b></sub>](http://elsurudo.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=elsurudo "Code") | [<img src="https://avatars1.githubusercontent.com/u/334809?v=4" width="100px;"/><br /><sub><b>Kurtis Rainbolt-Greene</b></sub>](https://kurtis.rainbolt-greene.online)<br />[💻](https://github.com/renderedtext/render_async/commits?author=krainboltgreene "Code") | [<img src="https://avatars2.githubusercontent.com/u/59744?v=4" width="100px;"/><br /><sub><b>Richard Schneeman</b></sub>](https://www.schneems.com)<br />[📖](https://github.com/renderedtext/render_async/commits?author=schneems "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/75705?v=4" width="100px;"/><br /><sub><b>Richard Venneman</b></sub>](https://www.cityspotters.com)<br />[📖](https://github.com/renderedtext/render_async/commits?author=richardvenneman "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/381395?v=4" width="100px;"/><br /><sub><b>Filipe W. Lima</b></sub>](https://github.com/filipewl)<br />[📖](https://github.com/renderedtext/render_async/commits?author=filipewl "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/3135638?v=4" width="100px;"/><br /><sub><b>Jesús Eduardo Clemens Chong</b></sub>](https://github.com/eclemens)<br />[💻](https://github.com/renderedtext/render_async/commits?author=eclemens "Code") | [<img src="https://avatars3.githubusercontent.com/u/1935686?v=4" width="100px;"/><br /><sub><b>René Klačan</b></sub>](https://github.com/reneklacan)<br />[💻](https://github.com/renderedtext/render_async/commits?author=reneklacan "Code") [📖](https://github.com/renderedtext/render_async/commits?author=reneklacan "Documentation") |
705
812
  | [<img src="https://avatars1.githubusercontent.com/u/1313442?v=4" width="100px;"/><br /><sub><b>Gil Gomes</b></sub>](http://gilgomes.com.br)<br />[📖](https://github.com/renderedtext/render_async/commits?author=gil27 "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/6081795?v=4" width="100px;"/><br /><sub><b>Khoa Nguyen</b></sub>](https://github.com/ThanhKhoaIT)<br />[💻](https://github.com/renderedtext/render_async/commits?author=ThanhKhoaIT "Code") [📖](https://github.com/renderedtext/render_async/commits?author=ThanhKhoaIT "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/8645918?v=4" width="100px;"/><br /><sub><b>Preet Sethi</b></sub>](https://www.linkedin.com/in/preetsethila/)<br />[💻](https://github.com/renderedtext/render_async/commits?author=preetsethi "Code") | [<img src="https://avatars3.githubusercontent.com/u/11586335?v=4" width="100px;"/><br /><sub><b>fangxing</b></sub>](https://github.com/fffx)<br />[💻](https://github.com/renderedtext/render_async/commits?author=fffx "Code") | [<img src="https://avatars3.githubusercontent.com/u/1191418?v=4" width="100px;"/><br /><sub><b>Emmanuel Pire</b></sub>](http://blog.lipsumarium.com)<br />[💻](https://github.com/renderedtext/render_async/commits?author=lipsumar "Code") [📖](https://github.com/renderedtext/render_async/commits?author=lipsumar "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/615509?v=4" width="100px;"/><br /><sub><b>Maxim Geerinck</b></sub>](https://github.com/maximgeerinck)<br />[💻](https://github.com/renderedtext/render_async/commits?author=maximgeerinck "Code") | [<img src="https://avatars1.githubusercontent.com/u/251706?v=4" width="100px;"/><br /><sub><b>Don</b></sub>](https://github.com/vanboom)<br />[💻](https://github.com/renderedtext/render_async/commits?author=vanboom "Code") |
813
+ | [<img src="https://avatars0.githubusercontent.com/u/998682?v=4" width="100px;"/><br /><sub><b>villu164</b></sub>](https://github.com/villu164)<br />[📖](https://github.com/renderedtext/render_async/commits?author=villu164 "Documentation") | [<img src="https://avatars.githubusercontent.com/u/11203679?v=4" width="100px;"/><br /><sub><b>Mitchell Buckley</b></sub>](https://github.com/Mbuckley0)<br />[💻](https://github.com/renderedtext/render_async/commits?author=Mbuckley0 "Code") [📖](https://github.com/renderedtext/render_async/commits?author=Mbuckley0 "Documentation") | [<img src="https://avatars.githubusercontent.com/u/15371677?v=4" width="100px;"/><br /><sub><b>yhirano55</b></sub>](https://github.com/yhirano55)<br />[💻](https://github.com/renderedtext/render_async/commits?author=yhirano55 "Code") [📖](https://github.com/renderedtext/render_async/commits?author=yhirano55 "Documentation") |
706
814
  <!-- ALL-CONTRIBUTORS-LIST:END -->
707
815
 
708
816
  This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
@@ -8,6 +8,7 @@
8
8
  <% content_for content_for_name do %>
9
9
  <%= javascript_tag html_options do %>
10
10
  <% locals = { container_id: container_id,
11
+ replace_container: replace_container,
11
12
  path: path,
12
13
  method: method,
13
14
  data: data,
@@ -17,14 +18,16 @@
17
18
  error_message: error_message,
18
19
  error_event_name: error_event_name,
19
20
  retry_count: retry_count,
21
+ retry_count_header: retry_count_header,
20
22
  retry_delay: retry_delay,
21
23
  interval: interval,
22
- turbolinks: RenderAsync.configuration.turbolinks } %>
24
+ turbolinks: RenderAsync.configuration.turbolinks,
25
+ turbo: RenderAsync.configuration.turbo} %>
23
26
 
24
27
  <% if RenderAsync.configuration.jquery %>
25
28
  <%= render partial: 'render_async/request_jquery',
26
29
  formats: [:js],
27
- locals: locals %>
30
+ locals: locals %>
28
31
  <% else %>
29
32
  <%= render partial: 'render_async/request_vanilla',
30
33
  formats: [:js],
@@ -5,6 +5,11 @@ if (window.jQuery) {
5
5
  return;
6
6
  }
7
7
  <% end %>
8
+ <% if turbo %>
9
+ if (document.documentElement.hasAttribute("data-turbo-preview")) {
10
+ return;
11
+ }
12
+ <% end %>
8
13
  function createEvent(name, container) {
9
14
  var event = undefined;
10
15
  if (typeof(Event) === 'function') {
@@ -24,6 +29,8 @@ if (window.jQuery) {
24
29
  } else {
25
30
  <% if turbolinks %>
26
31
  $(document).one('turbolinks:load', callback);
32
+ <% elsif turbo %>
33
+ $(document).one('turbo:load', callback);
27
34
  <% else %>
28
35
  $(document).ready(callback);
29
36
  <% end %>
@@ -32,9 +39,15 @@ if (window.jQuery) {
32
39
 
33
40
  function _makeRequest(currentRetryCount) {
34
41
  var headers = <%= headers.to_json.html_safe %>;
35
- var csrfTokenElement = document.querySelector('meta[name="csrf-token"]')
42
+ var csrfTokenElement = document.querySelector('meta[name="csrf-token"]');
36
43
  if (csrfTokenElement)
37
- headers['X-CSRF-Token'] = csrfTokenElement.content
44
+ headers['X-CSRF-Token'] = csrfTokenElement.content;
45
+
46
+ <% if retry_count_header %>
47
+ if (typeof(currentRetryCount) === 'number') {
48
+ headers['<%= retry_count_header.html_safe %>'] = currentRetryCount;
49
+ }
50
+ <% end %>
38
51
 
39
52
  $.ajax({
40
53
  url: '<%= path.html_safe %>',
@@ -43,11 +56,15 @@ if (window.jQuery) {
43
56
  headers: headers
44
57
  }).done(function(response) {
45
58
  var container = $("#<%= container_id %>");
46
- <% if interval %>
59
+
60
+ // If user navigated away before the request completed
61
+ if (!container.length) return;
62
+
63
+ <% if !interval && replace_container %>
64
+ container.replaceWith(response);
65
+ <% else %>
47
66
  container.empty();
48
67
  container.append(response);
49
- <% else %>
50
- container.replaceWith(response);
51
68
  <% end %>
52
69
 
53
70
  var loadEvent = createEvent('render_async_load', container);
@@ -66,6 +83,8 @@ if (window.jQuery) {
66
83
  if (skipErrorMessage) return;
67
84
 
68
85
  var container = $("#<%= container_id %>");
86
+ if (!container.length) return;
87
+
69
88
  container.replaceWith("<%= error_message.try(:html_safe) %>");
70
89
 
71
90
  var errorEvent = createEvent(
@@ -87,6 +106,8 @@ if (window.jQuery) {
87
106
  _makeRequest(currentRetryCount)
88
107
  }, <%= retry_delay %>)
89
108
  }
109
+ <% else %>
110
+ _retryMakeRequest = _makeRequest
90
111
  <% end %>
91
112
 
92
113
  function retry(currentRetryCount) {
@@ -122,17 +143,34 @@ if (window.jQuery) {
122
143
  }
123
144
  }
124
145
 
125
- var container = $("#<%= container_id %>");
146
+ function _setUpControlEvents() {
147
+ var container = $("#<%= container_id %>");
148
+
149
+ // Register a stop polling event on the container
150
+ $(container).on('async-stop', _clearRenderAsyncInterval)
126
151
 
127
- // Register a stop polling event on the container
128
- $(container).on('async-stop', _clearRenderAsyncInterval)
152
+ // Register a start polling event on the container
153
+ $(container).on('async-start', _renderAsyncFunction)
154
+ }
129
155
 
130
- // Register a start polling event on the container
131
- $(container).on('async-start', _renderAsyncFunction)
156
+ _runAfterDocumentLoaded(_setUpControlEvents)
132
157
 
133
158
  <% if turbolinks %>
134
159
  $(document).one('turbolinks:visit', _clearRenderAsyncInterval);
135
160
  <% end %>
161
+ <% if turbo %>
162
+ $(document).one('turbo:visit', _clearRenderAsyncInterval);
163
+ <% end %>
164
+ <% end %>
165
+
166
+ <% if !replace_container %>
167
+ function _setUpRefreshEvent() {
168
+ var container = $("#<%= container_id %>");
169
+
170
+ $(container).on('refresh', _renderAsyncFunction)
171
+ }
172
+
173
+ _runAfterDocumentLoaded(_setUpRefreshEvent)
136
174
  <% end %>
137
175
 
138
176
  <% if toggle %>
@@ -145,6 +183,10 @@ if (window.jQuery) {
145
183
  _renderAsyncFunction();
146
184
  }
147
185
  });
186
+
187
+ <% if toggle[:start] %>
188
+ _renderAsyncFunction()
189
+ <% end %>
148
190
  }
149
191
 
150
192
  _runAfterDocumentLoaded(_setUpToggle);
@@ -4,6 +4,11 @@
4
4
  return;
5
5
  }
6
6
  <% end %>
7
+ <% if turbo %>
8
+ if (document.documentElement.hasAttribute("data-turbo-preview")) {
9
+ return;
10
+ }
11
+ <% end %>
7
12
  function createEvent(name, container) {
8
13
  var event = undefined;
9
14
  if (typeof(Event) === 'function') {
@@ -22,6 +27,11 @@
22
27
  e.target.removeEventListener(e.type, arguments.callee);
23
28
  callback();
24
29
  });
30
+ <% elsif turbo %>
31
+ document.addEventListener("turbo:load", function(e) {
32
+ e.target.removeEventListener(e.type, arguments.callee);
33
+ callback();
34
+ });
25
35
  <% else %>
26
36
  document.addEventListener("DOMContentLoaded", callback);
27
37
  <% end %>
@@ -36,9 +46,15 @@
36
46
  request.open('<%= method %>', '<%= path.html_safe %>', asyncRequest);
37
47
 
38
48
  var headers = <%= headers.to_json.html_safe %>;
39
- var csrfTokenElement = document.querySelector('meta[name="csrf-token"]')
49
+ var csrfTokenElement = document.querySelector('meta[name="csrf-token"]');
40
50
  if (csrfTokenElement)
41
- headers['X-CSRF-Token'] = csrfTokenElement.content
51
+ headers['X-CSRF-Token'] = csrfTokenElement.content;
52
+
53
+ <% if retry_count_header %>
54
+ if (typeof(currentRetryCount) === 'number') {
55
+ headers['<%= retry_count_header.html_safe %>'] = currentRetryCount;
56
+ }
57
+ <% end %>
42
58
 
43
59
  Object.keys(headers).map(function(key) {
44
60
  request.setRequestHeader(key, headers[key]);
@@ -50,10 +66,14 @@
50
66
  if (request.readyState === 4) {
51
67
  if (request.status >= SUCCESS && request.status < ERROR) {
52
68
  var container = document.getElementById('<%= container_id %>');
53
- <% if interval %>
54
- container.innerHTML = request.response;
55
- <% else %>
69
+
70
+ // If user navigated away before the request completed
71
+ if (!container) return;
72
+
73
+ <% if !interval && replace_container %>
56
74
  container.outerHTML = request.response;
75
+ <% else %>
76
+ container.innerHTML = request.response;
57
77
  <% end %>
58
78
 
59
79
  var loadEvent = createEvent('render_async_load', container);
@@ -72,6 +92,8 @@
72
92
  if (skipErrorMessage) return;
73
93
 
74
94
  var container = document.getElementById('<%= container_id %>');
95
+ if (!container) return;
96
+
75
97
  container.outerHTML = '<%= error_message.try(:html_safe) %>';
76
98
 
77
99
  var errorEvent = createEvent(
@@ -92,11 +114,13 @@
92
114
  <% if retry_count > 0 %>
93
115
 
94
116
  <% if retry_delay %>
95
- _retryMakeRequest = function(currentRetryCount) {
117
+ var _retryMakeRequest = function(currentRetryCount) {
96
118
  setTimeout(function() {
97
119
  _makeRequest(currentRetryCount)
98
120
  }, <%= retry_delay %>)
99
121
  }
122
+ <% else %>
123
+ var _retryMakeRequest = _makeRequest
100
124
  <% end %>
101
125
 
102
126
  function retry(currentRetryCount) {
@@ -132,17 +156,34 @@
132
156
  }
133
157
  }
134
158
 
135
- var container = document.getElementById('<%= container_id %>');
159
+ function _setUpControlEvents() {
160
+ var container = document.getElementById('<%= container_id %>');
136
161
 
137
- // Register a polling stop event on the container
138
- container.addEventListener("async-stop", _clearRenderAsyncInterval)
162
+ // Register a polling stop event on the container
163
+ container.addEventListener("async-stop", _clearRenderAsyncInterval)
139
164
 
140
- // Register a start polling event on the container
141
- container.addEventListener("async-start", _renderAsyncFunction)
165
+ // Register a start polling event on the container
166
+ container.addEventListener("async-start", _renderAsyncFunction)
167
+ }
168
+
169
+ _runAfterDocumentLoaded(_setUpControlEvents)
142
170
 
143
171
  <% if turbolinks %>
144
172
  document.addEventListener("turbolinks:visit", _clearRenderAsyncInterval)
145
173
  <% end %>
174
+ <% if turbo %>
175
+ document.addEventListener("turbo:visit", _clearRenderAsyncInterval)
176
+ <% end %>
177
+ <% end %>
178
+
179
+ <% if !replace_container %>
180
+ function _setUpRefreshEvent() {
181
+ var container = document.getElementById('<%= container_id %>');
182
+
183
+ container.addEventListener('refresh', _renderAsyncFunction)
184
+ }
185
+
186
+ _runAfterDocumentLoaded(_setUpRefreshEvent)
146
187
  <% end %>
147
188
 
148
189
  <% if toggle %>
@@ -160,6 +201,10 @@
160
201
  <% end %>
161
202
  };
162
203
 
204
+ <% if toggle[:start] %>
205
+ _renderAsyncFunction()
206
+ <% end %>
207
+
163
208
  for (var i = 0; i < selectors.length; ++i) {
164
209
  selectors[i].addEventListener('<%= toggle[:event] || 'click' %>', handler)
165
210
  }
@@ -11,3 +11,10 @@ cd spec/fixtures/rails-5-base-app
11
11
  ls
12
12
  bundle install
13
13
  bundle exec cucumber
14
+
15
+ cd ../../../spec/fixtures/rails-6-base-app
16
+ ls
17
+ bundle install
18
+ yarn install
19
+ RAILS_ENV=test bundle exec rails webpacker:compile
20
+ bundle exec cucumber
@@ -1,10 +1,13 @@
1
1
  module RenderAsync
2
2
  class Configuration
3
- attr_accessor :jquery, :turbolinks
3
+ attr_accessor :jquery, :turbolinks, :turbo, :replace_container, :nonces
4
4
 
5
5
  def initialize
6
6
  @jquery = false
7
7
  @turbolinks = false
8
+ @turbo = false
9
+ @replace_container = true
10
+ @nonces = false
8
11
  end
9
12
  end
10
13
  end
@@ -1,3 +1,3 @@
1
1
  module RenderAsync
2
- VERSION = "2.1.7".freeze
2
+ VERSION = "2.1.11".freeze
3
3
  end
@@ -2,7 +2,6 @@ require 'securerandom'
2
2
 
3
3
  module RenderAsync
4
4
  module ViewHelper
5
-
6
5
  def render_async_cache_key(path)
7
6
  "render_async_#{path}"
8
7
  end
@@ -20,11 +19,10 @@ module RenderAsync
20
19
  def render_async(path, options = {}, &placeholder)
21
20
  event_name = options.delete(:event_name)
22
21
  placeholder = capture(&placeholder) if block_given?
23
- html_options = options.delete(:html_options) || {}
24
22
 
25
23
  render 'render_async/render_async', **container_element_options(options),
26
24
  path: path,
27
- html_options: html_options,
25
+ html_options: html_options(options),
28
26
  event_name: event_name,
29
27
  placeholder: placeholder,
30
28
  **request_options(options),
@@ -39,7 +37,16 @@ module RenderAsync
39
37
  def container_element_options(options)
40
38
  { html_element_name: options[:html_element_name] || 'div',
41
39
  container_id: options[:container_id] || generate_container_id,
42
- container_class: options[:container_class] }
40
+ container_class: options[:container_class],
41
+ replace_container: replace_container(options) }
42
+ end
43
+
44
+ def html_options(options)
45
+ set_options = options.delete(:html_options) || {}
46
+
47
+ set_options[:nonce] = configuration.nonces if set_options[:nonce].nil?
48
+
49
+ set_options
43
50
  end
44
51
 
45
52
  def request_options(options)
@@ -56,6 +63,7 @@ module RenderAsync
56
63
  def retry_options(options)
57
64
  {
58
65
  retry_count: options.delete(:retry_count) || 0,
66
+ retry_count_header: options.delete(:retry_count_header),
59
67
  retry_delay: options.delete(:retry_delay)
60
68
  }
61
69
  end
@@ -71,10 +79,18 @@ module RenderAsync
71
79
  }
72
80
  end
73
81
 
74
- private
75
-
76
82
  def generate_container_id
77
83
  "render_async_#{SecureRandom.hex(5)}#{Time.now.to_i}"
78
84
  end
85
+
86
+ def replace_container(options)
87
+ return options[:replace_container] unless options[:replace_container].nil?
88
+
89
+ configuration.replace_container
90
+ end
91
+
92
+ def configuration
93
+ RenderAsync.configuration
94
+ end
79
95
  end
80
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render_async
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.7
4
+ version: 2.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Grubbe
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-08-01 00:00:00.000000000 Z
12
+ date: 2021-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -61,6 +61,7 @@ extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
63
  - ".all-contributorsrc"
64
+ - ".github/CONTRIBUTING.md"
64
65
  - ".github/FUNDING.yml"
65
66
  - ".gitignore"
66
67
  - ".gitmodules"