diff --git a/packages/sync-service/lib/electric/plug/serve_shape_plug.ex b/packages/sync-service/lib/electric/plug/serve_shape_plug.ex index 07540ed7da..ee42697436 100644 --- a/packages/sync-service/lib/electric/plug/serve_shape_plug.ex +++ b/packages/sync-service/lib/electric/plug/serve_shape_plug.ex @@ -251,10 +251,11 @@ defmodule Electric.Plug.ServeShapePlug do defp put_resp_cache_headers(%Conn{} = conn, _) do if conn.assigns.live do - conn - |> put_resp_header("cache-control", "no-store, no-cache, must-revalidate, max-age=0") - |> put_resp_header("pragma", "no-cache") - |> put_resp_header("expires", "0") + put_resp_header( + conn, + "cache-control", + "max-age=5, stale-while-revalidate=5" + ) else put_resp_header( conn, diff --git a/packages/sync-service/test/electric/plug/serve_shape_plug_test.exs b/packages/sync-service/test/electric/plug/serve_shape_plug_test.exs index 54347614a2..4671346e65 100644 --- a/packages/sync-service/test/electric/plug/serve_shape_plug_test.exs +++ b/packages/sync-service/test/electric/plug/serve_shape_plug_test.exs @@ -354,11 +354,9 @@ defmodule Electric.Plug.ServeShapePlugTest do ] assert Plug.Conn.get_resp_header(conn, "cache-control") == [ - "no-store, no-cache, must-revalidate, max-age=0" + "max-age=5, stale-while-revalidate=5" ] - assert Plug.Conn.get_resp_header(conn, "pragma") == ["no-cache"] - assert Plug.Conn.get_resp_header(conn, "expires") == ["0"] assert Plug.Conn.get_resp_header(conn, "x-electric-chunk-last-offset") == [next_offset_str] assert Plug.Conn.get_resp_header(conn, "x-electric-schema") == [] end @@ -440,11 +438,8 @@ defmodule Electric.Plug.ServeShapePlugTest do assert Jason.decode!(conn.resp_body) == [%{"headers" => %{"control" => "up-to-date"}}] assert Plug.Conn.get_resp_header(conn, "cache-control") == [ - "no-store, no-cache, must-revalidate, max-age=0" + "max-age=5, stale-while-revalidate=5" ] - - assert Plug.Conn.get_resp_header(conn, "pragma") == ["no-cache"] - assert Plug.Conn.get_resp_header(conn, "expires") == ["0"] end test "send 409 when shape ID requested does not exist" do diff --git a/packages/typescript-client/test/cache.test.ts b/packages/typescript-client/test/cache.test.ts index c6c27797da..40c817662f 100644 --- a/packages/typescript-client/test/cache.test.ts +++ b/packages/typescript-client/test/cache.test.ts @@ -64,7 +64,7 @@ const it = testWithIssuesTable.extend<{ }) describe(`HTTP Proxy Cache`, { timeout: 30000 }, () => { - it(`should always get non-cached response in live mode`, async ({ + it(`should get a short max-age cache-conrol header in live mode`, async ({ insertIssues, proxyCacheBaseUrl, issuesTableUrl, @@ -93,14 +93,14 @@ describe(`HTTP Proxy Cache`, { timeout: 30000 }, () => { expect(liveRes.status).toBe(200) expect(getCacheStatus(liveRes)).toBe(CacheStatus.MISS) - // Second request still gets non-cached response + // Second request gets a cached response const cachedRes = await fetch( `${proxyCacheBaseUrl}/v1/shape/${issuesTableUrl}?${searchParams.toString()}`, {} ) expect(cachedRes.status).toBe(200) - expect(getCacheStatus(cachedRes)).toBe(CacheStatus.MISS) + expect(getCacheStatus(cachedRes)).toBe(CacheStatus.HIT) }) it(`should get cached response on second request`, async ({ diff --git a/website/electric-api.yaml b/website/electric-api.yaml index 098d32f318..e456ec6b94 100644 --- a/website/electric-api.yaml +++ b/website/electric-api.yaml @@ -134,7 +134,7 @@ paths: description: |- Cache control header as a string of comma separated directives. - Supported directives are: `max-age`, `stale-while-revalidate`, `no-store`, `no-cache`, `must-revalidate`. + Supported directives are: `max-age`, `stale-while-revalidate`. etag: schema: type: string @@ -143,12 +143,6 @@ paths: Etag header specifying the shape ID and offset for efficient caching. In the format `{shape_id}:{start_offset}:{end_offset}`. - pragma: - schema: - type: string - enum: - - no-cache - description: Pragma header for backwards compatibility with HTTP/1.0 caches (that don't have a Cache-Control HTTP/1.1 header). x-electric-chunk-last-offset: schema: type: string