README.md in pagelet_rails-0.1.6 vs README.md in pagelet_rails-0.1.7
- old
+ new
@@ -1,27 +1,31 @@
# PageletRails
+[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/antulik/pagelet_rails/master/LICENSE)
[![Gem Version](https://badge.fury.io/rb/pagelet_rails.svg)](https://badge.fury.io/rb/pagelet_rails)
+[![Build Status](https://travis-ci.org/antulik/pagelet_rails.svg?branch=master)](https://travis-ci.org/antulik/pagelet_rails)
+[![Code Climate](https://codeclimate.com/github/antulik/pagelet_rails/badges/gpa.svg)](https://codeclimate.com/github/antulik/pagelet_rails)
+[![Test Coverage](https://codeclimate.com/github/antulik/pagelet_rails/badges/coverage.svg)](https://codeclimate.com/github/antulik/pagelet_rails/coverage)
Warning: This gem is still in testing, so API might change.
## Why?
-* Do you have pages with a lot of information?
+* Do you have pages with a lot of information?
* The pages where you need to get data from 5 or 10 different sources?
* What if one of them is slow?
* Does this mean your users have to wait?
Don't make your users wait for page to load.
-
+
[View Demo Project](http://polar-river-18908.herokuapp.com)
-
+
## Example
-
+
![](https://camo.githubusercontent.com/50f4078cc4015e3df89afc753a5ff79828ac0e8e/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f662e636c2e6c792f6974656d732f303031323133314d324b3147335831483276314f2f313433303033383036373738372e6a7067)
-For example let's take facebook user home page. It has A LOT of data, but it loads very quickly. How? The answer is [perceived performance](https://en.wikipedia.org/wiki/Perceived_performance). It's not about in how many milliseconds you can serve request, but how fast it **feels** to the user.
+For example let's take facebook user home page. It has A LOT of data, but it loads very quickly. How? The answer is [perceived performance](https://en.wikipedia.org/wiki/Perceived_performance). It's not about in how many milliseconds you can serve request, but how fast it **feels** to the user.
The page body is served instantly and all the data is loaded after. Even for facebook it takes multiple seconds to fully load the page. But it feels instant, that it's all about.
## Who is doing that?
@@ -31,17 +35,17 @@
* [Engineering the New LinkedIn Profile](https://engineering.linkedin.com/profile/engineering-new-linkedin-profile)
* [Play Framework & SF Scala: Jim Brikman, Composable Applications in Play, 7/23/14](https://www.youtube.com/watch?v=4b1XLka0UIw)
## What is Pagelet?
-You can break a web page into number of sections, where each one is responsible for its own functionality. Pagelet is the name for each section. It is a part of the page which has it's own route, controller and view.
+You can break a web page into number of sections, where each one is responsible for its own functionality. Pagelet is the name for each section. It is a part of the page which has it's own route, controller and view.
-The closest alternative in ruby is [cells gem](https://github.com/apotonick/cells). After using it for long time I've faced many limitations of its approach. Cells has a custom Rails-like syntax but not quite. That is frustrating as you have to learn and remember those differences. The second issue, and the biggest, cells are internal only and not designed to be routable. This stops many great possibilities for improving perceived performance, as request has to wait for all cells to render.
-
+The closest alternative in ruby is [cells gem](https://github.com/apotonick/cells). After using it for long time I've faced many limitations of its approach. Cells has a custom Rails-like syntax but not quite. That is frustrating as you have to learn and remember those differences. The second issue, and the biggest, cells are internal only and not designed to be routable. This stops many great possibilities for improving perceived performance, as request has to wait for all cells to render.
+
Pagelet_rails is built on top of Rails and uses it as much as possible. The main philosophy: **Do not reinvent the wheel, build on shoulders of giants.**
-
+
# Usage
## Installation
Add this line to your application's Gemfile:
@@ -83,20 +87,20 @@
## Example Usage
```ruby
# app/pagelets/current_time/current_time_controller.rb
class CurrentTime::CurrentTimeController < ApplicationController
- include PageletRails::Concerns::Controller
-
+ include PageletRails::Concerns::Controller
+
# add pagelets_current_time_path route
# which gives "/pagelets/current_time" url route
- pagelet_resource only: [:show]
+ pagelet_resource only: [:show]
def show
end
end
-```
+```
```erb
<!-- Please note view path -->
<!-- app/pagelets/current_time/views/show.erb -->
<div class="panel-heading">Current time</div>
@@ -128,11 +132,11 @@
- [Super smart caching](#super-smart-caching)
- [Ajax Batching](#ajax-batching)
## Pagelet view helper
-`pagelet` helper allows you to render pagelets in views. Name of pagelet is its path.
+`pagelet` helper allows you to render pagelets in views. Name of pagelet is its path.
For example pagelet with route `pagelets_current_time_path` will have `pagelets_current_time` name.
### remote
@@ -196,26 +200,26 @@
`pagelet_options` is similar to `params` object, but for private data and config. Options can be global for all actions or specific actions only.
```ruby
class CurrentTime::CurrentTimeController < ::ApplicationController
include PageletRails::Concerns::Controller
-
+
# Set default option for all actions
- pagelet_options remote: true
-
+ pagelet_options remote: true
+
# set option for :show and :edit actions only
- pagelet_options :show, :edit, remote: :turbolinks
-
+ pagelet_options :show, :edit, remote: :turbolinks
+
def show
end
-
+
def new
end
-
+
def edit
end
-
+
end
```
```erb
<%= pagelet :new_pagelets_current_time %><!-- defaults to remote: true -->
@@ -224,69 +228,69 @@
<%= pagelet :pagelets_current_time, remote: false %> <!-- force remote: false -->
```
## Inline routes
-Because pagelets are small you will have many of them. In order to keep them under control pagelet_rails provides helpers.
+Because pagelets are small you will have many of them. In order to keep them under control pagelet_rails provides helpers.
You can inline routes inside you controller.
```ruby
class CurrentTime::CurrentTimeController < ::ApplicationController
include PageletRails::Concerns::Controller
-
+
pagelet_resource only: [:show]
# same as in config/routes.rb:
#
# resource :current_time, only: [:show]
#
-
+
pagelet_resources
# same as in config/routes.rb:
#
# resources :current_time
#
-
+
pagelet_routes do
# this is the same context as in config/routes.rb:
get 'show_me_time' => 'current_time/current_time#show'
end
-
+
end
```
## Pagelet cache
-Cache of pagelet rails is built on top of [actionpack-action_caching gem](https://github.com/rails/actionpack-action_caching).
+Cache of pagelet rails is built on top of [actionpack-action_caching gem](https://github.com/rails/actionpack-action_caching).
Simple example
```ruby
# app/pagelets/current_time/current_time_controller.rb
class CurrentTime::CurrentTimeController < ::ApplicationController
include PageletRails::Concerns::Controller
-
+
pagelet_options expires_in: 10.minutes
def show
end
end
```
### cache_path
-Is a hash of additional parameters for cache key.
+Is a hash of additional parameters for cache key.
* `Hash` - static hash
* `Proc` - dynamic params, it must return hash. Eg. `Proc.new { params.permit(:sort_by) }`
* `Lambda` - same as `Proc` but accepts `controller` as first argument
* `String` - any custom identifier
### expires_in
-Set the cache expiry. For example `expires_in: 1.hour`.
+Set the cache expiry. For example `expires_in: 1.hour`.
Warning: if `expires_in` is missing, it will be cached indefinitely.
### cache
@@ -328,42 +332,44 @@
<p>
<%= link_to 'Refresh', pagelets_current_time_path, remote: true %>
</p>
</div>
```
-Please note `remote: true` option for `link_to`.
+Please note `remote: true` option for `link_to`.
This is default Rails functionality with small addition. If that link is inside pagelet, than controller response will be replaced in that pagelet.
```ruby
# app/pagelets/current_time/current_time_controller.rb
class CurrentTime::CurrentTimeController < ::ApplicationController
include PageletRails::Concerns::Controller
-
+
pagelet_resource only: [:show]
def show
end
end
-```
+```
This will partially update the page and replace only that pagelet.
### Streaming
This is the most efficient way to deliver data with minimum delays. The placeholder will be rendered first and the full version will be delivered at the end of page and replaced with Javascript code. Everything is delivered in the same request.
This mode requires rendering of templates with [streaming mode](http://api.rubyonrails.org/classes/ActionController/Streaming.html) enabled.
+Warning: Session and Cookies are currently not supported in streaming mode.
+
```ruby
#...
def show
render :show, stream: true
end
- #...
+ #...
```
In you layout add `pagelet_stream` right before `</body>` tag.
```erb
@@ -374,27 +380,27 @@
<% pagelet_stream %>
</body>
```
-Usage:
+Usage:
```erb
<%= pagelet :pagelets_current_time, remote: :stream %>
```
**Warning!!!** You also should have webserver compatible for streaming like puma, passenger or unicorn (requires special config).
-**Warning!!!** you need to have multiple threads/processes configured in the web server. This is required so the page could fetch assets while content is streaming.
-
-Finally if everything is done right you should see significant rendering speed improvements especially on old browsers, slow network or with cold cache.
+**Warning!!!** you need to have multiple threads/processes configured in the web server. This is required so the page could fetch assets while content is streaming.
+Finally if everything is done right you should see significant rendering speed improvements especially on old browsers, slow network or with cold cache.
+
### Super smart caching
Probably one of the coolest functionality of pagelet_rails is "super smart caching". It allows you to render pagelets through ajax and cache them, but if page is reloaded the pagelet is rendered instantly from cache.
-
+
So on the first page load user sees "Loading..." blocks, but after the content is instant.
The best thing, it's enabled by default if pagelet has caching enabled and is rendering through ajax request.
### Ajax Batching
@@ -408,9 +414,11 @@
There will be one request per group. Missing value is considered a separate group as well.
## Todo
+* assets support with webpacker in rails 5.1
+* session (and CSRF) support in streaming mode
* delay load of not visible pagelets (aka. below the fold)
* do not load pagelets which are not visible to the user until user scrolls down. For example like Youtube comments.
* high test coverage
* update actionpack-action_caching gem to support rails 5