canonical-rails 0.1.2 → 0.2.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +1 -2
- data/app/helpers/canonical_rails/tag_helper.rb +39 -8
- data/lib/canonical-rails.rb +3 -0
- data/lib/canonical-rails/engine.rb +4 -2
- data/lib/canonical-rails/version.rb +1 -1
- data/lib/generators/canonical_rails/install/templates/canonical_rails.rb +8 -6
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0ab1e0b7b6f0b2ab95ac94692939a74ade7bd8a2cb744bec7c6762ae04a1dfe7
|
4
|
+
data.tar.gz: 8091321b37d368601c5242496bade7282fc2e7a31a4d7ee547cddae8eaa116a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6015361191325a29ea441f6150277126ba216c59ac40f09954b6b178b497b84216e6783505dafab8b479d4264949836a98d6175101fd275d18b3c941ff7e8022
|
7
|
+
data.tar.gz: a6bf895a0626fb7dfd47e4bc88c19e2bd90d8671b42c95d5f0878a7578104e9b40af35662806abc442f53d4a2a54e7afea8baa7eca89a5f9e753ee16d8221e33
|
data/README.md
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
CanonicalRails
|
2
2
|
==============
|
3
|
-
[![Dependency Status](https://gemnasium.com/jumph4x/canonical-rails.png)](https://gemnasium.com/jumph4x/canonical-rails)
|
4
3
|
[![Build Status](https://travis-ci.org/jumph4x/canonical-rails.svg?branch=master)](https://travis-ci.org/jumph4x/canonical-rails)
|
5
4
|
|
6
5
|
A number of articles exist explaining the issue concisely and at length:
|
@@ -15,7 +14,7 @@ Take a look at this blog post that can guide you through the idea and the setup:
|
|
15
14
|
|
16
15
|
## Challenge
|
17
16
|
|
18
|
-
I've seen a lot of folks do more harm by neglecting canonicalization altogether than by applying
|
17
|
+
I've seen a lot of folks do more harm by neglecting canonicalization altogether than by applying too narrowly and conservatively, so here is an attempt to let people start modestly without spending too much time on it and whitelist parameters as they need to.
|
19
18
|
|
20
19
|
## Install
|
21
20
|
|
@@ -1,14 +1,25 @@
|
|
1
1
|
module CanonicalRails
|
2
2
|
module TagHelper
|
3
3
|
def trailing_slash_needed?
|
4
|
-
CanonicalRails.sym_collection_actions.include?
|
4
|
+
request.params.key?('action') && CanonicalRails.sym_collection_actions.include?(request.params['action'].to_sym)
|
5
|
+
end
|
6
|
+
|
7
|
+
# Leave force_trailing_slash as nil to get the original behavior of trailing_slash_if_needed
|
8
|
+
def trailing_slash_config(force_trailing_slash = nil)
|
9
|
+
if force_trailing_slash
|
10
|
+
"/"
|
11
|
+
elsif force_trailing_slash.nil?
|
12
|
+
trailing_slash_if_needed
|
13
|
+
end
|
5
14
|
end
|
6
15
|
|
7
16
|
def trailing_slash_if_needed
|
8
|
-
"/" if trailing_slash_needed?
|
17
|
+
"/" if trailing_slash_needed?
|
9
18
|
end
|
10
19
|
|
11
20
|
def path_without_html_extension
|
21
|
+
return '' if request.path == '/'
|
22
|
+
|
12
23
|
request.path.sub(/\.html$/, '')
|
13
24
|
end
|
14
25
|
|
@@ -24,20 +35,32 @@ module CanonicalRails
|
|
24
35
|
(CanonicalRails.port || request.port).to_i
|
25
36
|
end
|
26
37
|
|
27
|
-
def canonical_href(host = canonical_host, port = canonical_port)
|
38
|
+
def canonical_href(host = canonical_host, port = canonical_port, force_trailing_slash = nil)
|
28
39
|
default_ports = { 'https://' => 443, 'http://' => 80 }
|
29
40
|
port = port.present? && port.to_i != default_ports[canonical_protocol] ? ":#{port}" : ''
|
30
|
-
raw "#{canonical_protocol}#{host}#{port}#{path_without_html_extension}#{
|
41
|
+
raw "#{canonical_protocol}#{host}#{port}#{path_without_html_extension}#{trailing_slash_config(force_trailing_slash)}#{whitelisted_query_string}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def canonical_path(force_trailing_slash = nil)
|
45
|
+
raw "#{path_without_html_extension}#{trailing_slash_config(force_trailing_slash)}#{whitelisted_query_string}"
|
31
46
|
end
|
32
47
|
|
33
|
-
def canonical_tag(host = canonical_host, port = canonical_port)
|
34
|
-
|
48
|
+
def canonical_tag(host = canonical_host, port = canonical_port, force_trailing_slash = nil)
|
49
|
+
canonical_url = canonical_href(host, port, force_trailing_slash)
|
50
|
+
capture do
|
51
|
+
if CanonicalRails.opengraph_url
|
52
|
+
concat tag(:meta, property: 'og:url', content: canonical_url)
|
53
|
+
end
|
54
|
+
concat tag(:link, href: canonical_url, rel: :canonical)
|
55
|
+
end
|
35
56
|
end
|
36
57
|
|
37
58
|
def whitelisted_params
|
38
|
-
params.select do |key, value|
|
59
|
+
selected_params = params.select do |key, value|
|
39
60
|
value.present? && CanonicalRails.sym_whitelisted_parameters.include?(key.to_sym)
|
40
61
|
end
|
62
|
+
|
63
|
+
selected_params.respond_to?(:to_unsafe_h) ? selected_params.to_unsafe_h : selected_params.to_h
|
41
64
|
end
|
42
65
|
|
43
66
|
def whitelisted_query_string
|
@@ -51,7 +74,15 @@ module CanonicalRails
|
|
51
74
|
# Rack 1.6.0 has it
|
52
75
|
# https://github.com/rack/rack/blob/65a7104b6b3e9ecd8f33c63a478ab9a33a103507/test/spec_utils.rb#L251
|
53
76
|
|
54
|
-
|
77
|
+
wl_params = whitelisted_params
|
78
|
+
|
79
|
+
"?" + Rack::Utils.build_nested_query(convert_numeric_params(wl_params)) if wl_params.present?
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def convert_numeric_params(params_hash)
|
85
|
+
Hash[params_hash.map { |k, v| v.is_a?(Numeric) ? [k, v.to_s] : [k, v] }]
|
55
86
|
end
|
56
87
|
end
|
57
88
|
end
|
data/lib/canonical-rails.rb
CHANGED
@@ -26,6 +26,9 @@ module CanonicalRails
|
|
26
26
|
mattr_accessor :whitelisted_parameters
|
27
27
|
@@whitelisted_parameters = []
|
28
28
|
|
29
|
+
mattr_accessor :opengraph_url
|
30
|
+
@@opengraph_url = false
|
31
|
+
|
29
32
|
def self.sym_collection_actions
|
30
33
|
@@sym_collection_actions ||= self.collection_actions.map(&:to_sym)
|
31
34
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
require_relative '../../app/helpers/canonical_rails/tag_helper'
|
2
|
+
|
1
3
|
module CanonicalRails
|
2
4
|
class Engine < ::Rails::Engine
|
3
|
-
|
5
|
+
|
4
6
|
initializer 'canonical_rails.add_helpers' do |app|
|
5
7
|
ActionView::Base.send :include, CanonicalRails::TagHelper
|
6
8
|
end
|
7
|
-
|
9
|
+
|
8
10
|
end
|
9
11
|
end
|
@@ -1,13 +1,13 @@
|
|
1
|
-
# Do yourself a favor and set these up right when you install the engine.
|
1
|
+
# Do yourself a favor and set these up right when you install the engine.
|
2
2
|
|
3
3
|
CanonicalRails.setup do |config|
|
4
4
|
|
5
5
|
# Force the protocol. If you do not specify, the protocol will be based on the incoming request's protocol.
|
6
6
|
|
7
7
|
config.protocol#= 'https://'
|
8
|
-
|
8
|
+
|
9
9
|
# This is the main host, not just the TLD, omit slashes and protocol. If you have more than one, pick the one you want to rank in search results.
|
10
|
-
|
10
|
+
|
11
11
|
config.host# = 'www.mywebstore.com'
|
12
12
|
config.port# = '3000'
|
13
13
|
|
@@ -16,12 +16,14 @@ CanonicalRails.setup do |config|
|
|
16
16
|
# otherwise we have to assume semantics of an instance of a resource type, a member view - implying a :show get route
|
17
17
|
#
|
18
18
|
# Acts as a whitelist for routes to have trailing slashes
|
19
|
-
|
19
|
+
|
20
20
|
config.collection_actions# = [:index]
|
21
21
|
|
22
22
|
# Parameter spamming can cause index dilution by creating seemingly different URLs with identical or near-identical content.
|
23
23
|
# Unless whitelisted, these parameters will be omitted
|
24
|
-
|
24
|
+
|
25
25
|
config.whitelisted_parameters# = []
|
26
|
-
|
26
|
+
|
27
|
+
# Output a matching OpenGraph URL meta tag (og:url) with the canonical URL, as recommended by Facebook et al
|
28
|
+
config.opengraph_url#= true
|
27
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canonical-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Ivanov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '4.1'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '6.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '4.1'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '6.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: appraisal
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -45,19 +45,19 @@ dependencies:
|
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
48
|
+
name: sprockets
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- - "
|
51
|
+
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '0'
|
53
|
+
version: '3.0'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- - "
|
58
|
+
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: '0'
|
60
|
+
version: '3.0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: rspec-rails
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
125
|
version: '0'
|
126
126
|
requirements: []
|
127
127
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.
|
128
|
+
rubygems_version: 2.7.6.2
|
129
129
|
signing_key:
|
130
130
|
specification_version: 4
|
131
131
|
summary: Simple and configurable Rails canonical ref tag helper
|