Skip to content

Commit

Permalink
Disable Turbo caching on main page
Browse files Browse the repository at this point in the history
Turbo caching page meant that users often saw outdated read vs unread
thread styling on the main page (since we're not controlling this in
Javascript). For now, I'm disabling the caching, which will slow down
load times for users. In the longterm, I'd like to move toward updating
state before page cache using Turbo hooks.
  • Loading branch information
hartsick committed Mar 30, 2022
1 parent b416d00 commit 119d621
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module ApplicationHelper
def title(text)
content_for :title, text
end

def disable_cache
content_for :cache_control, '<meta name="turbo-cache-control" content="no-cache">'.html_safe
end
end
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<%= "#{sanitize(yield(:title))} · " if content_for?(:title) %>
<%= Rails.application.credentials.website_name %>
</title>
<%= yield(:cache_control) if content_for?(:cache_control) %>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
Expand Down
1 change: 1 addition & 0 deletions app/views/topics/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% title("Topics") %>
<% disable_cache %>

<div class="flex justify-between">
<h1 class="text-lg mb-1 px-1 inline-block bg-secondary-300">topics</h1>
Expand Down
56 changes: 23 additions & 33 deletions spec/system/user_views_threads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let!(:viewer) { FactoryBot.create(:user, username: 'viewer', email: '[email protected]', password: 'password1234') }

before do
Capybara.default_max_wait_time = 5
driven_by :selenium, using: :headless_chrome
end

Expand All @@ -22,11 +23,11 @@
expect(page).to have_content 'post your dogs'
click_on 'back to topics'

expect(page).to have_content 'topics'
expect(page).to have_selector('h1', text: 'topics')
expect(page).to have_content 'post your dogs'

sign_out :user

sign_out poster

# Viewer
sign_in viewer
visit '/'
Expand All @@ -39,55 +40,52 @@

click_on "back to topics"

expect(page).to have_selector('h1', text: 'topics')
expect(page).to have_content(2)

sign_out :user
end

it 'shows notifications for unread posts' do
FactoryBot.create(:topic, title: 'this is a topic', author: poster, created_at: 1.day.ago)
FactoryBot.create(:topic, title: 'but this is the one I made', author: viewer, created_at: Time.now)

# Poster
login_as(poster)
sign_in poster
visit '/'

expect(page).to have_content('topics')
expect(page).to have_selector('h1', text: 'topics')
first_row = page.all(:xpath, "//table/tbody/tr").first
expect(first_row).to have_content('but this is the one I made')
expect(first_row).to have_content('(unread)')

click_on 'but this is the one I made'
click_on 'home'

expect(page).to have_content('topics')
expect(page).to have_selector('h1', text: 'topics')
first_row = page.all(:xpath, "//table/tbody/tr").first
expect(first_row).to have_content('but this is the one I made')
expect(first_row).to_not have_content('(unread)')

sign_out :user

sign_out poster

# Viewer
login_as(viewer)
sign_in viewer
visit '/'

expect(page).to have_content('topics')
expect(page).to have_selector('h1', text: 'topics')
click_on 'but this is the one I made'
fill_in 'reply', with: 'hope you like notifications'
click_on 'post'

sign_out :user
sign_out viewer

# Poster
login_as(poster)
sign_in poster
visit '/'

expect(page).to have_content('topics')
expect(page).to have_selector('h1', text: 'topics')
first_row = page.all(:xpath, "//table/tbody/tr").first
expect(first_row).to have_content('but this is the one I made')
expect(first_row).to have_content('(unread)')

sign_out :user
end

it 'shows notifications for mentions and allows accessing past mentions' do
Expand All @@ -100,9 +98,9 @@
fill_in 'title', with: 'hello thread'
fill_in 'body', with: "hey @viewer"
click_on 'post'

sign_out :user

sign_out poster

# Viewer
sign_in viewer
visit '/'
Expand All @@ -121,8 +119,8 @@
first_row = page.all(:xpath, "//table/tbody/tr").first
expect(first_row).to have_content('hello thread')
expect(first_row).to_not have_content('mentioned')
sign_out :user

sign_out viewer

# Poster
sign_in poster
Expand All @@ -138,8 +136,6 @@
expect(page).to have_selector('h1', text: "topics you've been mentioned in")
first_row = page.all(:xpath, "//table/tbody/tr").first
expect(first_row).to have_content('hello thread')

sign_out :user
end

it 'allows saving of threads of interest' do
Expand All @@ -154,7 +150,7 @@

click_on 'back to topics'

expect(page).to have_content('topics')
expect(page).to have_selector('h1', text: 'topics')
first_row = page.all(:xpath, "//table/tbody/tr").first
expect(first_row).to have_content('this is a topic')
expect(first_row).to have_content('starred')
Expand All @@ -170,8 +166,6 @@
click_on '*s'
expect(page).to have_content("topics you've starred")
expect(page).to_not have_content('this is a topic')

sign_out :user
end

it 'allows pinning of threads' do
Expand Down Expand Up @@ -199,30 +193,26 @@

pinned_rows = page.all(:xpath, '//table[@id="pinned"]/tbody/tr')
expect(pinned_rows.length).to eq(0)

sign_out :user
end

it 'allows marking all threads as read' do
FactoryBot.create(:topic, title: 'this is my topic', author: poster, created_at: 1.day.ago)
FactoryBot.create(:topic, title: 'not my topic', author: viewer, created_at: Time.now)

login_as(poster)
sign_in poster

visit '/'

expect(page).to have_content('topics')
expect(page).to have_selector('h1', text: 'topics')
first_row = page.all(:xpath, "//table/tbody/tr").first
expect(first_row).to have_content('not my topic')
expect(first_row).to have_content('(unread)')

click_on 'mark all as read'

expect(page).to have_content('topics')
expect(page).to have_selector('h1', text: 'topics')
first_row = page.all(:xpath, "//table/tbody/tr").first
expect(first_row).to have_content('not my topic')
expect(first_row).to have_content('(unread)')

sign_out :user
end
end

0 comments on commit 119d621

Please sign in to comment.