seo_cache 1.0.7 → 1.2.0

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: 91e81cc42e58735dd234bd336c165f5f98c910be1352d411328773ec665bafc3
4
- data.tar.gz: 4eb8d5a45fb09013a626ad2dd1fba85dae74d93a7db47756b6761d1c1a6abc09
3
+ metadata.gz: 257a485a274532a58693567428f24615cf4f1c4ad9f45546d81b6ed26b2352c2
4
+ data.tar.gz: 6fe1ac814ab110ab97aca70682cdf4cff66cebc3449b6abd14594417bd26876d
5
5
  SHA512:
6
- metadata.gz: a40d821a8bdeb984d5a0f52840680e76d7867bc5044805ccebe719ee485d79f9df16a98c1307704b9e869844f06e159bf96528293c4ca4383fa1fb60299970ae
7
- data.tar.gz: 6bd0c086023df029305d247d48f4a0a08a0af23d85c739cce351134f21ca7e4c225f5f3ea5c4b4cad42f805ae70929f0d2984106499fa3ceb9e34d7ba4963ac0
6
+ metadata.gz: a5d651a0568691b994fdd94d76c52a8b6c83907e91dd8e95edcdf380cca1e994a7929e0ac92b94a0658be46f4564e33367bc6413c657fc9bcd372062362e922a
7
+ data.tar.gz: cbdbc8ba5ced409d94ddd0fed596c768042a32b1fd9ae861d2e0d2f5c406a51b447856a1005a9eded8b6ede343eeeac8acf9b6914fd4278688819d170b093204
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## 1.1.2
2
+
3
+ - Do not try to cache invalid requests
4
+
5
+ ## 1.1.2
6
+
7
+ - Correct populate seo cache method
8
+
9
+ ## 1.1.1
10
+
11
+ - Add new option to use locale as first directory for cache path
12
+
13
+ ## 1.1.0
14
+
15
+ - Update dependencies
16
+
1
17
  ## 1.0.7
2
18
 
3
19
  - Use new syntax to check if Redis key exists
@@ -16,52 +16,39 @@ module SeoCache
16
16
  end
17
17
 
18
18
  def call(env)
19
- if prerender_page?(env)
19
+ @status, @headers, @response = @app.call(env)
20
+
21
+ if prerender_page?(env, @status)
20
22
  cached_response = before_render(env)
21
23
 
22
24
  return cached_response.finish if cached_response.present?
23
25
 
24
- if SeoCache.log_missed_cache
25
- env_request = Rack::Request.new(env)
26
- SeoCache.log("missed cache : #{env_request.path} (User agent: #{env_request.user_agent})")
27
- end
28
-
29
26
  if SeoCache.prerender_service_url.present?
30
27
  prerender_response = prerender_service(env)
31
28
  if prerender_response
32
29
  response = build_response_from_prerender(prerender_response.body)
33
30
  after_render(env, prerender_response)
31
+
34
32
  return response.finish
35
33
  end
36
34
  else
37
35
  Thread.new do
38
36
  prerender_data = page_render(env)
39
- # Extract status from render page or return success (200)
40
- status = prerender_data&.scan(/<!--status:(\d+)-->/)&.last&.first || 200
41
-
42
- if SeoCache.log_missed_cache && status.to_s.start_with?('2')
43
- env_request = Rack::Request.new(env)
44
- SeoCache.log("missed cache : #{env_request.path} (User agent: #{env_request.user_agent})")
45
- end
46
37
 
47
- after_render(env, prerender_data, status)
38
+ after_render(env, prerender_data, @status)
48
39
  end
49
40
  end
50
41
  elsif prerender_params?(env)
51
- env['seo_mode'] = true
52
- # Add status to render page because Selenium doesn't return http headers or status...
42
+ env['seo_mode'] = true
53
43
  status, headers, response = @app.call(env)
54
- status_code = "<!--status:#{status}-->"
55
- # Cannot add at the top of file, Chrome removes leading comments...
56
44
  begin
57
- body_code = response.body.sub(/<head( ?)(.*?)>/i, "<head\\1\\2>#{status_code}")
58
- return [status, headers, [body_code]]
45
+ return [status, headers, [response.body]]
59
46
  rescue
60
47
  return [status, headers, [nil]]
61
48
  end
62
49
  end
63
50
 
64
- return @app.call(env)
51
+ return [@status, @headers, @response]
65
52
  end
66
53
 
67
54
  def prerender_params?(env)
@@ -75,13 +62,15 @@ module SeoCache
75
62
  return true if query_params.has_key?(SeoCache.prerender_url_param) || query_params.has_key?(SeoCache.force_cache_url_param)
76
63
  end
77
64
 
78
- def prerender_page?(env)
65
+ def prerender_page?(env, status)
79
66
  user_agent = env['HTTP_USER_AGENT']
80
67
  buffer_agent = env['HTTP_X_BUFFERBOT']
81
68
  is_requesting_prerender_page = false
82
69
 
83
70
  return false unless user_agent
84
71
 
72
+ return false if status.to_i > 299
73
+
85
74
  return false if env['REQUEST_METHOD'] != 'GET'
86
75
 
87
76
  request = Rack::Request.new(env)
@@ -186,7 +175,9 @@ module SeoCache
186
175
  # return nil unless @options[:before_render]
187
176
  # cached_render = @options[:before_render].call(env)
188
177
 
189
- cached_render = @page_caching.get(Rack::Request.new(env).path)
178
+ env_request = Rack::Request.new(env)
179
+
180
+ cached_render = @page_caching.get(env_request.path, get_locale(env_request.params, env_request.host))
190
181
 
191
182
  return nil unless cached_render
192
183
 
@@ -213,12 +204,21 @@ module SeoCache
213
204
  def after_render(env, response, status = 200)
214
205
  return unless response && SeoCache.cache_only_status.include?(status.to_i)
215
206
 
207
+ env_request = Rack::Request.new(env)
208
+
216
209
  if SeoCache.log_missed_cache
217
- env_request = Rack::Request.new(env)
218
210
  SeoCache.log("missed cache : #{env_request.path} (User agent: #{env_request.user_agent})")
219
211
  end
220
212
 
221
- @page_caching.cache(response, Rack::Request.new(env).path)
213
+ @page_caching.cache(response, env_request.path, get_locale(env_request.params, env_request.host))
214
+ end
215
+
216
+ def get_locale(params, host)
217
+ if SeoCache.locale_as_first_directory
218
+ SeoCache.locale_method ? SeoCache.locale_method.call(params, host) : I18n.locale
219
+ else
220
+ nil
221
+ end
222
222
  end
223
223
  end
224
224
  end
@@ -13,31 +13,31 @@ module SeoCache
13
13
  @redis = Redis::Namespace.new(SeoCache.redis_namespace, redis: Redis.new(host: uri.host, port: uri.port, password: uri.password, connect_timeout: 1, timeout: 1), warnings: false)
14
14
  end
15
15
 
16
- def get(path, extension = nil)
17
- @redis.get(cache_path(path, extension)) if SeoCache.memory_cache? && @redis
16
+ def get(path, locale_domain = nil, extension = nil)
17
+ @redis.get(cache_path(path, locale_domain, extension)) if SeoCache.memory_cache? && @redis
18
18
  end
19
19
 
20
20
  def cache(content, path, locale_domain = nil, extension = nil, gzip = Zlib::BEST_COMPRESSION)
21
21
  instrument :write_page, path do
22
22
  if SeoCache.memory_cache? && @redis
23
- write_to_memory(content, cache_path(path, extension, locale_domain))
23
+ write_to_memory(content, cache_path(path, locale_domain, extension))
24
24
  else
25
- write_to_disk(content, cache_path(path, extension, locale_domain), gzip)
25
+ write_to_disk(content, cache_path(path, locale_domain, extension), gzip)
26
26
  end
27
27
  end
28
28
  end
29
29
 
30
- def expire(path)
30
+ def expire(path, locale_domain = nil, extension = nil)
31
31
  instrument :expire_page, path do
32
- delete(cache_path(path))
32
+ delete(cache_path(path, locale_domain, extension))
33
33
  end
34
34
  end
35
35
 
36
- def cache_exists?(path)
36
+ def cache_exists?(path, locale_domain = nil, extension = nil)
37
37
  if SeoCache.memory_cache? && @redis
38
- @redis.exists?(cache_path(path))
38
+ @redis.exists?(cache_path(path, locale_domain, extension))
39
39
  else
40
- File.exist?(cache_path(path))
40
+ File.exist?(cache_path(path, locale_domain, extension))
41
41
  end
42
42
  end
43
43
 
@@ -51,7 +51,7 @@ module SeoCache
51
51
  SeoCache.cache_extension
52
52
  end
53
53
 
54
- def cache_file(path, extension, locale_domain)
54
+ def cache_file(path, locale_domain, extension)
55
55
  name = if path.empty? || path =~ %r{\A/+\z}
56
56
  '/index'
57
57
  else
@@ -65,8 +65,8 @@ module SeoCache
65
65
  name
66
66
  end
67
67
 
68
- def cache_path(path, extension = nil, locale_domain = nil)
69
- File.join(cache_directory, cache_file(path, extension, locale_domain))
68
+ def cache_path(path, locale_domain = nil, extension = nil)
69
+ File.join(cache_directory, cache_file(path, locale_domain, extension))
70
70
  end
71
71
 
72
72
  def write_to_memory(content, path)
@@ -35,7 +35,7 @@ module SeoCache
35
35
  private
36
36
 
37
37
  def generate_cache(path, locale_domain = nil)
38
- return if @page_caching.cache_exists?(path) && !@force_cache
38
+ return if @page_caching.cache_exists?(path, locale_domain) && !@force_cache
39
39
 
40
40
  url = @host + path
41
41
  url += path.include?('?') ? '&' : '?'
@@ -1,3 +1,3 @@
1
1
  module SeoCache
2
- VERSION = '1.0.7'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
data/lib/seo_cache.rb CHANGED
@@ -22,6 +22,12 @@ module SeoCache
22
22
  mattr_accessor :cache_path
23
23
  self.cache_path = ''
24
24
 
25
+ mattr_accessor :locale_as_first_directory
26
+ self.locale_as_first_directory = false
27
+
28
+ mattr_accessor :locale_method
29
+ self.locale_method = nil
30
+
25
31
  mattr_accessor :cache_extension
26
32
  self.cache_extension = '.html'
27
33
 
data/seo_cache.gemspec CHANGED
@@ -25,8 +25,8 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency 'railties', '>= 5'
26
26
  spec.add_dependency 'redis', '~> 4'
27
27
  spec.add_dependency 'redis-namespace', '~> 1'
28
- spec.add_dependency 'selenium-webdriver', '~> 3'
29
- spec.add_dependency 'webdrivers', '~> 4'
28
+ spec.add_dependency 'selenium-webdriver', '>= 3'
29
+ spec.add_dependency 'webdrivers', '>= 3'
30
30
 
31
31
  spec.add_development_dependency 'bundler', '~> 2'
32
32
  spec.add_development_dependency 'rake', '~> 13'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seo_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - FloXcoder
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-19 00:00:00.000000000 Z
11
+ date: 2022-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -84,30 +84,30 @@ dependencies:
84
84
  name: selenium-webdriver
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '3'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '3'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: webdrivers
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - "~>"
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: '4'
103
+ version: '3'
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - "~>"
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
- version: '4'
110
+ version: '3'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: bundler
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -229,7 +229,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
229
229
  - !ruby/object:Gem::Version
230
230
  version: '0'
231
231
  requirements: []
232
- rubygems_version: 3.2.27
232
+ rubygems_version: 3.3.5
233
233
  signing_key:
234
234
  specification_version: 4
235
235
  summary: Cache dedicated for SEO with Javascript rendering