Skip to content

Commit c54dd85

Browse files
authored
Merge pull request #342 from joyofrails/fix/page-caching
Fix for page caching
2 parents d9a44a2 + 008402c commit c54dd85

File tree

12 files changed

+90
-38
lines changed

12 files changed

+90
-38
lines changed

app/content/pages/about/design.html.mdrb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ I am [not a designer](https://notadesigner.io/). Though the design of this site
77

88
## Layout
99

10-
The design leans heavily into [CSS grid](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_grid_layout/Basic_concepts_of_grid_layout) I’m a relatively new student of CSS grid so I’m sure the approach could be improved. I built a 12 column layout from using https://utopia.fyi to generate based grid, layout, and type settings as CSS variables. Utopia allows designers and developers to generate a mathematically consistent
10+
The design leans heavily into [CSS grid](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_grid_layout/Basic_concepts_of_grid_layout) I’m a relatively new student of CSS grid so I’m sure the approach could be improved. I built a 12 column layout using https://utopia.fyi to generate mathematically consistent grid, layout, and type settings as CSS variables.
1111

1212
## Colors
1313

@@ -25,6 +25,6 @@ I want to Joy of Rails to "own more" of its dependencies—part of why I impleme
2525

2626
When I was in grade school, I doodled a lot. Drawing brought me a lot of joy. After years of working at a computer, I got away from it. But more recently, especially now that I have kids of my own, I’ve had the itch to return to my roots.
2727

28-
For Joy of Rails, I wanted to add some fun visuals to the in-depth articles. So, in this age of AI-generated imagery, I decided I would do the opposite of what everyone else seems to be doing—draw everything by hand. You‘ll pictures of rainbows, ice cream cones, and smiley faces on Joy of Rails as a reminder of the joy coding brings to my life.
28+
For Joy of Rails, I wanted to add some fun visuals to the in-depth articles. So, in this age of AI-generated imagery, I decided I would do the opposite of what everyone else seems to be doing—draw everything by hand. Pictures of rainbows, ice cream cones, and smiley faces on Joy of Rails serve as a reminder of the joy building and creating brings to my life.
2929

3030
Most of the illustrations are done in [Procreate](https://procreate.com/) on my iPad with an Apple Pencil. Procreate is a great little app I highly recommend for anyone looking to rekindle their childhood doodling habit.

app/controllers/site_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def process_rendition(rendition)
3939
# @param rendition [Sitepress::Rendition] Rendered representatio of current_resource
4040
#
4141
def post_render(rendition)
42-
if skip_http_cache? || stale?(rendition.source, last_modified: current_resource.asset.updated_at.utc, public: true)
42+
last_modified_at = @current_page&.upserted_at || @current_page&.created_at || current_resource.asset.updated_at || Time.now
43+
44+
if skip_http_cache? || stale?(rendition.source, last_modified: last_modified_at.utc, public: true)
4345
render body: rendition.output, content_type: rendition.mime_type
4446
end
4547
end

app/models/page.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# indexed_at :datetime
1010
# published_at :datetime
1111
# request_path :string not null
12+
# revised_at :datetime
13+
# upserted_at :datetime
1214
# created_at :datetime not null
1315
# updated_at :datetime not null
1416
#

app/models/page/sitepressed.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ def resource_missing? = sitepress_resource.is_a?(NullSitepressResource)
8080
def handler = sitepress_resource.handler
8181

8282
class_methods do
83-
# We currently have a dual system of content management between Sitepress and
84-
# Page models for handling static pages While not ideal, it currently allows
85-
# us to live in both worlds depending on the context. Ultimately, migrating
86-
# away from Sitepress for indexed content may be what‘s needed, but keeping
87-
# the split personality for now.
83+
# Joy of Rails has logic to represent static pages split between Sitepress
84+
# and Page models. While not ideal, it currently allows us to live in both
85+
# worlds depending on the context. Ultimately, migrating away from
86+
# Sitepress for indexed content may be what‘s needed.
8887
#
8988
def upsert_collection_from_sitepress!(limit: nil)
9089
enum = SitepressPage.all.resources.lazy.map { |s| Resource.from(s) }
90+
upserted_at = Time.zone.now
9191

9292
if limit
9393
enum = enum.filter do |resource|
@@ -96,7 +96,7 @@ def upsert_collection_from_sitepress!(limit: nil)
9696
end
9797

9898
enum = enum.map do |resource|
99-
upsert_page_from_resource!(resource)
99+
upsert_page_from_resource!(resource, upserted_at: upserted_at)
100100
end
101101

102102
if limit
@@ -106,14 +106,15 @@ def upsert_collection_from_sitepress!(limit: nil)
106106
enum.to_a
107107
end
108108

109-
def upsert_page_by_request_path!(request_path) = upsert_page_from_sitepress!(Sitepress.site.get(request_path))
109+
def upsert_page_by_request_path!(request_path, **opts) = upsert_page_from_sitepress!(Sitepress.site.get(request_path), **opts)
110110

111-
def upsert_page_from_sitepress!(sitepress_resource) = upsert_page_from_resource! Resource.from(sitepress_resource)
111+
def upsert_page_from_sitepress!(sitepress_resource, **opts) = upsert_page_from_resource!(Resource.from(sitepress_resource), **opts)
112112

113-
def upsert_page_from_resource!(resource)
113+
def upsert_page_from_resource!(resource, upserted_at: Time.zone.now)
114114
page = find_or_initialize_by(request_path: resource.request_path)
115115
page.published_at = resource.published_at if resource.published_at
116-
page.updated_at = resource.revised_at if resource.revised_at # Should use a `Page#revised_at` column instead
116+
page.revised_at = resource.revised_at if resource.revised_at
117+
page.upserted_at = upserted_at
117118
page.save!
118119
page
119120
end

db/cable_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2024_09_14_010017) do
13+
ActiveRecord::Schema[8.1].define(version: 2024_09_14_010017) do
1414
create_table "solid_cable_messages", force: :cascade do |t|
1515
t.binary "channel", limit: 1024, null: false
1616
t.binary "payload", limit: 536870912, null: false

db/cache_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2024_02_10_145644) do
13+
ActiveRecord::Schema[8.1].define(version: 2024_02_10_145644) do
1414
create_table "_litestream_lock", id: false, force: :cascade do |t|
1515
t.integer "id"
1616
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddRevisionTimestampsToPages < ActiveRecord::Migration[8.1]
2+
def change
3+
add_column :pages, :revised_at, :datetime, null: true # For manually tracking content revisions
4+
add_column :pages, :upserted_at, :datetime, null: true # For automatically tracking upserts
5+
end
6+
end

db/queue_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2024_10_25_223629) do
13+
ActiveRecord::Schema[8.1].define(version: 2024_10_25_223629) do
1414
create_table "_litestream_lock", id: false, force: :cascade do |t|
1515
t.integer "id"
1616
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/factories/pages.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# indexed_at :datetime
77
# published_at :datetime
88
# request_path :string not null
9+
# revised_at :datetime
10+
# upserted_at :datetime
911
# created_at :datetime not null
1012
# updated_at :datetime not null
1113
#

0 commit comments

Comments
 (0)