Skip to content

Commit

Permalink
update 404 CACHE to 10 min
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcibotari committed May 28, 2024
1 parent 270b71a commit cea63dc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions functions/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FIREBASE_CONFIG, FirebaseConfig } from './models';
export const BATCH_MAX = 500;
// TIME
export const MINUTE = 60;
export const TEN_MINUTES = 10 * MINUTE;
export const HOUR = 60 * MINUTE;
export const DAY = 24 * HOUR;
// HTTP
Expand Down
22 changes: 17 additions & 5 deletions functions/src/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sharp from 'sharp';
import { logger } from 'firebase-functions';
import { HttpsError, onRequest } from 'firebase-functions/v2/https';
import { Query } from 'firebase-admin/firestore';
import { bucket, CACHE_ASSET_MAX_AGE, CACHE_MAX_AGE, CACHE_SHARE_MAX_AGE, firestoreService } from './config';
import { bucket, CACHE_ASSET_MAX_AGE, CACHE_MAX_AGE, CACHE_SHARE_MAX_AGE, firestoreService, TEN_MINUTES } from './config';
import { AssetFile, Content, ContentKind, ContentLink, Space } from './models';
import {
contentCachePath,
Expand Down Expand Up @@ -237,11 +237,17 @@ expressApp.get('/api/v1/spaces/:spaceId/contents/slugs/*', async (req, res) => {
.send(content.toString());
})
.catch(() => {
res.status(404).send(new HttpsError('not-found', 'File not found, Publish first.'));
res
.status(404)
.header('Cache-Control', `public, max-age=${TEN_MINUTES}, s-maxage=${TEN_MINUTES}`)
.send(new HttpsError('not-found', 'File not found, on path. Please Publish again.'));
});
}
} else {
res.status(404).send(new HttpsError('not-found', 'File not found, Publish first.'));
res
.status(404)
.header('Cache-Control', `public, max-age=${TEN_MINUTES}, s-maxage=${TEN_MINUTES}`)
.send(new HttpsError('not-found', 'File not found, Publish first.'));
return;
}
});
Expand Down Expand Up @@ -311,11 +317,17 @@ expressApp.get('/api/v1/spaces/:spaceId/contents/:contentId', async (req, res) =
.send(content.toString());
})
.catch(() => {
res.status(404).send(new HttpsError('not-found', 'File not found, Publish first.'));
res
.status(404)
.header('Cache-Control', `public, max-age=${TEN_MINUTES}, s-maxage=${TEN_MINUTES}`)
.send(new HttpsError('not-found', 'File not found, on path. Please Publish again.'));
});
}
} else {
res.status(404).send(new HttpsError('not-found', 'File not found, Publish first.'));
res
.status(404)
.header('Cache-Control', `public, max-age=${TEN_MINUTES}, s-maxage=${TEN_MINUTES}`)
.send(new HttpsError('not-found', 'File not found, Publish first.'));
return;
}
});
Expand Down

0 comments on commit cea63dc

Please sign in to comment.