Skip to content

Commit

Permalink
Merge pull request #14569 from artsy/cleanup-relay-cache-header
Browse files Browse the repository at this point in the history
chore: remove unused x-relay cache headers
  • Loading branch information
damassi authored Sep 25, 2024
2 parents e38f9f5 + f4c0f3e commit 06faccd
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,6 @@ describe("cacheHeaderMiddleware", () => {
jest.clearAllMocks()
})

it("should add x-relay-cache-config header", async () => {
next.mockResolvedValue({ status: 200 })

const middleware = cacheHeaderMiddleware()(next)
const res = await middleware(req)

expect(req.fetchOpts.headers["x-relay-cache-config"]).toBe(
JSON.stringify(req.cacheConfig)
)
expect(next).toHaveBeenCalledWith(req)
expect(res).toEqual({ status: 200 })
})

it("should add x-relay-cache-path header", async () => {
jest.spyOn(window as any, "location", "get").mockImplementation(() => ({
pathname: "/foo",
}))

next.mockResolvedValue({ status: 200 })

const middleware = cacheHeaderMiddleware()(next)
const res = await middleware(req)

expect(req.fetchOpts.headers["x-relay-cache-path"]).toBe("/foo")
expect(next).toHaveBeenCalledWith(req)
expect(res).toEqual({ status: 200 })
})

describe("Cache-Control headers", () => {
describe("allowing a request to be cached by the CDN", () => {
it("allows caching even when logged in if the @cacheable directive was used", async () => {
Expand Down
19 changes: 0 additions & 19 deletions src/System/Relay/middleware/cacheHeaderMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import {
} from "System/Relay/isRequestCacheable"
import { findRoutesByPath } from "System/Router/Utils/routeUtils"

export const RELAY_CACHE_CONFIG_HEADER_KEY = "x-relay-cache-config"
export const RELAY_CACHE_PATH_HEADER_KEY = "x-relay-cache-path"

interface CacheHeaderMiddlewareProps {
url?: string | null
user: User
Expand Down Expand Up @@ -59,21 +56,6 @@ export const cacheHeaderMiddleware = (props?: CacheHeaderMiddlewareProps) => {
return next => async req => {
const url = isServer ? props?.url : window.location.pathname

const cacheHeaders = {
[RELAY_CACHE_CONFIG_HEADER_KEY]: JSON.stringify(req.cacheConfig),
}

/**
* We need to dynamically set a path header, because after the SSR pass we
* only have access to the initial req.url _entrypoint_; from there our
* client-side SPA intializes, which manages URL updates. Setting headers
* at the relay network level lets us configure queries to be sent to the
* graphql backend and CDN.
*/
if (url) {
cacheHeaders[RELAY_CACHE_PATH_HEADER_KEY] = url
}

const cacheControlHeader = (() => {
const foundRoute = findRoutesByPath({ path: url ?? "" })[0]

Expand Down Expand Up @@ -102,7 +84,6 @@ export const cacheHeaderMiddleware = (props?: CacheHeaderMiddlewareProps) => {

req.fetchOpts.headers = {
...req.fetchOpts.headers,
...cacheHeaders,
...cacheControlHeader,
}

Expand Down

0 comments on commit 06faccd

Please sign in to comment.