alchemy-json_api 2.1.0 → 2.3.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: 69488e287d0cb294483885e5b03deaebcf94a0905cd181613c84c6be90f4582f
4
+ data.tar.gz: 5fcffd69254524ab47f3f30b592633ce7ede6fc4312f02571297073655d3e2d1
5
5
  SHA512:
6
- metadata.gz: 0cc256eb440f91369407d301410674cd7703a84bc28704e4ae220c32ee69d6cc6984f84bf53b6ec9d45c29b203381f30173292ef384b200aed95ab89aff96b12
7
- data.tar.gz: e67fb531194df5b8b457f5cbdeadedd78e04cf5f2adfdb86ded3ec4a0eb8081e858bdff63a59b9f1a71ea81d87cae99e81526affcbb234985108d9b9d207b62b
6
+ metadata.gz: ef53c0124e8afd5eb85820362c5512be74a63659b1992b3aa43d22f884d9a5851fb1ae05722472b13873d3369c9ec475fa86de4329ec5b1c2d757271cf94032d
7
+ data.tar.gz: 3b7932b0e11841eabd4eeead158cdb54ac1484bd655629956354945383719e5f294736a7d74889b61c32a22f3f3befe19e1b3845abb9750b5c192e29ce453f53
@@ -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)
@@ -15,7 +15,8 @@ module Alchemy
15
15
  :meta_keywords,
16
16
  :meta_description,
17
17
  :created_at,
18
- :updated_at
18
+ :updated_at,
19
+ :restricted
19
20
  )
20
21
 
21
22
  cache_options store: Rails.cache, namespace: "alchemy-jsonapi"
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Alchemy
4
4
  module JsonApi
5
- VERSION = "2.1.0"
5
+ VERSION = "2.3.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.3.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-06-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: alchemy_cms