From cdd30795402b47d3772b3dc2315b57f73007a4af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Fern=C3=A1ndez-Capel?= Date: Wed, 21 Feb 2024 16:12:12 +0000 Subject: [PATCH] Ensure that page refreshes do not trigger a snapshot cache (#1196) Fixes a bug introduced in https://github.com/hotwired/turbo/pull/1146 `exemptPageFromPreview()` adds a `` tag to the `` setting `turbo-cache-control` to `no-preview`. However, since the MorphRenderer now inherits from the PageRenderer, it can update meta tags in the head and remove the `turbo-cache-control` tag. This means that the snapshot cache will be used for the next visit, which is not what we want. Specifying `shouldCacheSnapshot: false` in the `visit` options ensures that the snapshot cache is not used for the refresh visit. --- src/core/session.js | 3 +-- src/tests/functional/page_refresh_tests.js | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/session.js b/src/core/session.js index eb0c9880c..cdb978348 100644 --- a/src/core/session.js +++ b/src/core/session.js @@ -110,8 +110,7 @@ export class Session { refresh(url, requestId) { const isRecentRequest = requestId && this.recentRequests.has(requestId) if (!isRecentRequest) { - this.cache.exemptPageFromPreview() - this.visit(url, { action: "replace" }) + this.visit(url, { action: "replace", shouldCacheSnapshot: false }) } } diff --git a/src/tests/functional/page_refresh_tests.js b/src/tests/functional/page_refresh_tests.js index 06a041787..6619ae58e 100644 --- a/src/tests/functional/page_refresh_tests.js +++ b/src/tests/functional/page_refresh_tests.js @@ -33,6 +33,7 @@ test("async page refresh with turbo-stream", async ({ page }) => { await expect(page.locator("#title")).not.toHaveText("Updated") await expect(page.locator("#title")).toHaveText("Page to be refreshed") + expect(await noNextEventNamed(page, "turbo:before-cache")).toBeTruthy() }) test("dispatches a turbo:before-morph-element and turbo:morph-element event for each morphed element", async ({ page }) => {