Skip to content

Commit

Permalink
fix: 404 should be cached
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Feb 27, 2024
1 parent caafccd commit f16ec75
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/sjs-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function sjsRequestHandler(
buildTarget,
false,
);
if (CACHE_CLIENT_REDIRECT && isRedirect(response.status)) {
if (CACHE_CLIENT_REDIRECT && isRedirect(response)) {
const fastResponse = await createFastPathResponse(
response,
performance,
Expand Down Expand Up @@ -133,7 +133,7 @@ export async function sjsRequestHandler(
buildTarget,
CACHE,
);
if (CACHE && CACHE_CLIENT_REDIRECT && isRedirect(response.status)) {
if (CACHE && CACHE_CLIENT_REDIRECT && isRedirect(response)) {
const fastResponse = await createFastPathResponse(
response,
performance,
Expand Down
12 changes: 8 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ export const isJsResponse = (response: Response): boolean => {
));
};

export const isRedirect = (status: number): boolean => {
export const isRedirect = ({ status }: { status: number }): boolean => {
return status >= 300 && status < 400;
};

export const isOk = (status: number): boolean => {
export const isOk = ({ status }: { status: number }): boolean => {
return status >= 200 && status < 300;
};

export const isNotFound = ({ status }: { status: number }): boolean => {
return status === 404;
};

export const calcExpires = (headers: Headers): string => {
const DEFAULT = '600';
const cacheControl = Object.fromEntries(
Expand Down Expand Up @@ -134,8 +138,8 @@ export const createFinalResponse = async (
if (!headers.has('access-control-allow-origin')) {
headers.set('access-control-allow-origin', '*');
}
const isActualRedirect = isRedirect(status) && !isFastPathRedirect;
const isCacheable = isOk(status) || isActualRedirect;
const isActualRedirect = isRedirect(responseProps) && !isFastPathRedirect;
const isCacheable = isNotFound(responseProps) || isOk(responseProps) || isActualRedirect;
const willCache = shouldCache && isCacheable;
if (willCache) {
performance.mark('cache-write');
Expand Down

0 comments on commit f16ec75

Please sign in to comment.