seo_cache 1.1.0 → 1.2.1

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: 7286c3adfbf791c5b7091e4f689e4fbfe69bef35973e01c3f50f4d2fa3c8b9bb
4
- data.tar.gz: 4b27b16b60243f1f428ad36aa6e00d9699d1e71afe42100c1e643ed56b06ba4a
3
+ metadata.gz: 612bb7e5cfe82ac49b35bc86915082f054c9e4dbe91bc2b664038de965d1f07e
4
+ data.tar.gz: ca1dcfcff1cfa0fb9bcf8dc4ec6d3edc0660c2965fc9c9276c01581638271a27
5
5
  SHA512:
6
- metadata.gz: 66ab72c0aea1d7029ebd8f39e90b8aa5b36e8d15951b0359824989175e56098d380882fe0e05c8a90e9faa1fbf4b43d2a9acd53ec9aea67aebf3ed87399858d2
7
- data.tar.gz: 60121490b30d93c0fcd204f3dcf5c06bf3c9a14aa5e76eb19a9cf9986ff119bfb72b6e6ead3a119f33479334eeb5e8831b99bf68f56c7ad54ad44bfd20741eb8
6
+ metadata.gz: 69904412e9083c552ae777c266b960e0d5295fc3536d4f85da90ad47bcb77349e2a6c53be9e4dc9eb0ee8602dc121841974205e1480a15347ed5798a8c781bd9
7
+ data.tar.gz: 205c8bc45ac28e7969342b5a495530557a1020762969c7fed49cea7bc3bbdf8126fe02866d8eef4f43267753886ee51ac1080aded6e50df4ed8ae946234f2ec3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## 1.2.1
2
+
3
+ - Update deprecated Selenium options
4
+
5
+ ## 1.2.0
6
+
7
+ - Do not try to cache invalid requests
8
+
9
+ ## 1.1.2
10
+
11
+ - Correct populate seo cache method
12
+
13
+ ## 1.1.1
14
+
15
+ - Add new option to use locale as first directory for cache path
16
+
1
17
  ## 1.1.0
2
18
 
3
19
  - Update dependencies
@@ -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)
@@ -33,21 +33,14 @@ module SeoCache
33
33
 
34
34
  Selenium::WebDriver::Chrome.path = SeoCache.chrome_path if SeoCache.chrome_path
35
35
 
36
- browser_options = ::Selenium::WebDriver::Chrome::Options.new
37
- browser_options.args << 'disable-infobars'
38
- browser_options.args << '--headless'
39
- browser_options.args << '--no-sandbox'
40
- browser_options.args << '--incognito'
41
- browser_options.args << '--disable-dev-shm-usage'
42
- browser_options.args << '--disable-gpu'
43
- browser_options.args << '--disable-web-security'
44
- browser_options.args << '--disable-extensions'
45
- browser_options.args << '--disable-logging'
46
- browser_options.args << '--disable-notifications'
47
- browser_options.args << '--disable-sync'
48
- browser_options.args << '--window-size=1920x1080'
49
- browser_options.args << "--remote-debugging-port=#{SeoCache.chrome_debugging_port}" if SeoCache.chrome_debugging_port
50
- @driver = ::Selenium::WebDriver.for(:chrome, options: browser_options)
36
+ browser_options = %w[headless incognito disable-gpu disable-infobars disable-dev-shm-usage disable-gpu disable-web-security disable-extensions no-sandbox disable-logging disable-notifications disable-sync window-size=1920x1080]
37
+ browser_options << "remote-debugging-port=#{SeoCache.chrome_debugging_port}" if SeoCache.chrome_debugging_port
38
+ @driver = ::Selenium::WebDriver.for(
39
+ :chrome,
40
+ capabilities: [Selenium::WebDriver::Chrome::Options.new(
41
+ args: browser_options
42
+ )]
43
+ )
51
44
  end
52
45
  end
53
46
  end
@@ -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.1.0'.freeze
2
+ VERSION = '1.2.1'.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
 
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.1.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - FloXcoder
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-21 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
@@ -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.29
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