alchemy-json_api 2.1.0 → 2.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: 89edfe1ba595351698467e746b9451be8483071b6e4878fd9ef30c6cd062011a
4
- data.tar.gz: 5cdbc986b5eeae3dadfb7dd8f5beb6fc2632a5766131ac7b12e50b2595062e11
3
+ metadata.gz: 998909ab79b771f8adf243e5aa3878116ff30b0292990676f2e3e66c9bcf7df4
4
+ data.tar.gz: 5981655e53fbd3af1d1d70260122db353b3f0cbf81622bb7ac2f891f6fcc3fdc
5
5
  SHA512:
6
- metadata.gz: 0cc256eb440f91369407d301410674cd7703a84bc28704e4ae220c32ee69d6cc6984f84bf53b6ec9d45c29b203381f30173292ef384b200aed95ab89aff96b12
7
- data.tar.gz: e67fb531194df5b8b457f5cbdeadedd78e04cf5f2adfdb86ded3ec4a0eb8081e858bdff63a59b9f1a71ea81d87cae99e81526affcbb234985108d9b9d207b62b
6
+ metadata.gz: 372fe15209e5eb39a767d40914cbe2bf77b4a7014e7db764bccf762206e019772d7fa9833fec19df80245b078acf2ee483e064829e37b824da613ff0dba68e6a
7
+ data.tar.gz: 3ee1be6b2024a95645d15aa4d98e6882fd356a31ca96263128a5ef9ac75aedd88679852816368f7988932550d906ac7fcdc5c821b51878c12f10cf096a222270
@@ -25,7 +25,7 @@ module Alchemy
25
25
  end
26
26
  end
27
27
 
28
- def last_modified_for(page)
28
+ def page_cache_key(page)
29
29
  page.updated_at
30
30
  end
31
31
 
@@ -3,6 +3,8 @@
3
3
  module Alchemy
4
4
  module JsonApi
5
5
  class NodesController < JsonApi::BaseController
6
+ THREE_HOURS = 10800
7
+
6
8
  def index
7
9
  @nodes = node_scope.select(:id, :updated_at)
8
10
  if stale?(last_modified: @nodes.maximum(:updated_at), etag: @nodes)
@@ -17,7 +19,7 @@ module Alchemy
17
19
  private
18
20
 
19
21
  def cache_duration
20
- ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", 3).to_i.hours
22
+ ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", THREE_HOURS).to_i
21
23
  end
22
24
 
23
25
  def jsonapi_meta(nodes)
@@ -3,6 +3,9 @@
3
3
  module Alchemy
4
4
  module JsonApi
5
5
  class PagesController < JsonApi::BaseController
6
+ THREE_HOURS = 10800
7
+ JSONAPI_STALEMAKERS = %i[include fields sort filter page]
8
+
6
9
  before_action :load_page_for_cache_key, only: :show
7
10
 
8
11
  def index
@@ -10,9 +13,10 @@ module Alchemy
10
13
 
11
14
  jsonapi_filter(page_scope, allowed) do |filtered_pages|
12
15
  @pages = filtered_pages.result
16
+
13
17
  if !@pages.all?(&:cache_page?)
14
18
  render_pages_json(allowed) && return
15
- elsif stale?(last_modified: @pages.maximum(:published_at), etag: @pages.max_by(&:cache_key_with_version)&.cache_key_with_version)
19
+ elsif stale?(etag: etag(@pages))
16
20
  render_pages_json(allowed)
17
21
  end
18
22
  end
@@ -25,7 +29,7 @@ module Alchemy
25
29
  render(jsonapi: api_page(load_page)) && return
26
30
  end
27
31
 
28
- if stale?(last_modified: last_modified_for(@page), etag: @page.cache_key_with_version)
32
+ if stale?(etag: etag(@page))
29
33
  # Only load page with all includes when browser cache is stale
30
34
  render jsonapi: api_page(load_page)
31
35
  end
@@ -47,7 +51,7 @@ module Alchemy
47
51
  end
48
52
 
49
53
  def cache_duration
50
- ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", 3).to_i.hours
54
+ ENV.fetch("ALCHEMY_JSON_API_CACHE_DURATION", THREE_HOURS).to_i
51
55
  end
52
56
 
53
57
  def caching_options
@@ -60,10 +64,6 @@ module Alchemy
60
64
  .or(page_scope.where(urlname: params[:path])).first!
61
65
  end
62
66
 
63
- def last_modified_for(page)
64
- page.published_at
65
- end
66
-
67
67
  def jsonapi_meta(pages)
68
68
  pagination = jsonapi_pagination_meta(pages)
69
69
 
@@ -117,6 +117,17 @@ module Alchemy
117
117
  Alchemy::JsonApi::Page.new(page, page_version_type: page_version_type)
118
118
  end
119
119
 
120
+ def etag(pages)
121
+ pages = Array.wrap(pages)
122
+ return unless pages.any?
123
+ relevant_params = params.to_unsafe_hash.slice(*JSONAPI_STALEMAKERS).flatten.compact
124
+ pages.map { |page| page_cache_key(page) }.concat(relevant_params)
125
+ end
126
+
127
+ def page_cache_key(page)
128
+ page.cache_key_with_version
129
+ end
130
+
120
131
  def base_page_scope
121
132
  # cancancan is not able to merge our complex AR scopes for logged in users
122
133
  if can?(:edit_content, ::Alchemy::Page)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Alchemy
4
4
  module JsonApi
5
- VERSION = "2.1.0"
5
+ VERSION = "2.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alchemy-json_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Meyerhoff
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-05-07 00:00:00.000000000 Z
12
+ date: 2024-05-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: alchemy_cms