Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/sync-service/lib/electric/plug/serve_shape_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/typescript-client/test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ({
Expand Down
8 changes: 1 addition & 7 deletions website/electric-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down