Skip to content

Commit

Permalink
fix: Use no-cache when responding to the last updated time (#106)
Browse files Browse the repository at this point in the history
This PR adds the no-cache directive to prevent the endpoint that returns
the time in which the atlas was lastly updated.
  • Loading branch information
LautaroPetaccio authored Nov 3, 2023
1 parent f9e0bc7 commit bc3c143
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
11 changes: 10 additions & 1 deletion src/controllers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,20 @@ export async function tilesInfoRequestHandler(context: {
}) {
const { map } = context.components
if (!map.isReady()) {
return { status: 503, body: 'Not ready' }
return {
status: 503,
body: 'Not ready',
headers: {
'cache-control': 'no-cache',
},
}
}

const lastUpdatedAt = map.getLastUpdatedAt()
return {
headers: {
'cache-control': 'no-cache',
},
status: 200,
body: { lastUpdatedAt },
}
Expand Down
2 changes: 1 addition & 1 deletion src/logic/last-modified-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function lastModifiedMiddleware(
staleWhileRevalidate: THREE_MINUTES,
}
): IHttpServerComponent.IRequestHandler<Context<string>> {
const cacheControlHeader = `max-age=${options.maxAge}, stale-while-revalidate=${options.staleWhileRevalidate}, public`
const cacheControlHeader = `max-age=${options.maxAge}, s-maxage=${options.maxAge}, stale-while-revalidate=${options.staleWhileRevalidate}, public`

return async (context, next): Promise<IHttpServerComponent.IResponse> => {
const lastModifiedTime = getLastModifiedTime()
Expand Down
18 changes: 12 additions & 6 deletions tests/logic/last-modified-middlware-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ describe('when handling a request with a If-Modified-Since header', () => {
...mockedResponse,
headers: {
'Last-Modified': lastModifiedUTSCString,
'Cache-Control': 'max-age=120, stale-while-revalidate=180, public',
'Cache-Control':
'max-age=120, s-maxage=120, stale-while-revalidate=180, public',
},
})
})
Expand All @@ -63,7 +64,8 @@ describe('when handling a request with a If-Modified-Since header', () => {
status: 304,
headers: {
'Last-Modified': lastModifiedUTSCString,
'Cache-Control': 'max-age=120, stale-while-revalidate=180, public',
'Cache-Control':
'max-age=120, s-maxage=120, stale-while-revalidate=180, public',
},
})
})
Expand All @@ -82,7 +84,8 @@ describe('when handling a request with a If-Modified-Since header', () => {
status: 304,
headers: {
'Last-Modified': lastModifiedUTSCString,
'Cache-Control': 'max-age=120, stale-while-revalidate=180, public',
'Cache-Control':
'max-age=120, s-maxage=120, stale-while-revalidate=180, public',
},
})
})
Expand All @@ -103,7 +106,8 @@ describe('when handling a request with a If-Modified-Since header', () => {
...mockedResponse,
headers: {
'Last-Modified': lastModifiedUTSCString,
'Cache-Control': 'max-age=120, stale-while-revalidate=180, public',
'Cache-Control':
'max-age=120, s-maxage=120, stale-while-revalidate=180, public',
},
})
})
Expand All @@ -123,7 +127,8 @@ describe('when handling a request without a If-Modified-Since header', () => {
...mockedResponse,
headers: {
'Last-Modified': lastModifiedUTSCString,
'Cache-Control': 'max-age=120, stale-while-revalidate=180, public',
'Cache-Control':
'max-age=120, s-maxage=120, stale-while-revalidate=180, public',
},
})
})
Expand All @@ -146,7 +151,8 @@ describe('when setting the max age and the stale while revalidate options', () =
...mockedResponse,
headers: {
'Last-Modified': lastModifiedUTSCString,
'Cache-Control': 'max-age=600, stale-while-revalidate=600, public',
'Cache-Control':
'max-age=600, s-maxage=600, stale-while-revalidate=600, public',
},
})
})
Expand Down

0 comments on commit bc3c143

Please sign in to comment.