mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Add Cache-Control headers to a bunch of routes
This commit is contained in:
parent
66f7853c3b
commit
871222ee4e
4 changed files with 98 additions and 22 deletions
98
src/app.ts
98
src/app.ts
|
|
@ -199,15 +199,39 @@ app.use(
|
||||||
|
|
||||||
app.get('/metrics', metricsController);
|
app.get('/metrics', metricsController);
|
||||||
|
|
||||||
app.get('/.well-known/nodeinfo', nodeInfoController);
|
app.get(
|
||||||
|
'/.well-known/nodeinfo',
|
||||||
|
cacheControlMiddleware({ maxAge: 300, staleWhileRevalidate: 300, staleIfError: 21600, public: true }),
|
||||||
|
nodeInfoController,
|
||||||
|
);
|
||||||
app.get('/.well-known/nostr.json', nostrController);
|
app.get('/.well-known/nostr.json', nostrController);
|
||||||
|
|
||||||
app.get('/nodeinfo/:version', nodeInfoSchemaController);
|
app.get(
|
||||||
app.get('/manifest.webmanifest', manifestController);
|
'/nodeinfo/:version',
|
||||||
|
cacheControlMiddleware({ maxAge: 300, staleWhileRevalidate: 300, staleIfError: 21600, public: true }),
|
||||||
|
nodeInfoSchemaController,
|
||||||
|
);
|
||||||
|
app.get(
|
||||||
|
'/manifest.webmanifest',
|
||||||
|
cacheControlMiddleware({ maxAge: 5, staleWhileRevalidate: 5, staleIfError: 21600, public: true }),
|
||||||
|
manifestController,
|
||||||
|
);
|
||||||
|
|
||||||
app.get('/api/v1/instance', instanceV1Controller);
|
app.get(
|
||||||
app.get('/api/v2/instance', instanceV2Controller);
|
'/api/v1/instance',
|
||||||
app.get('/api/v1/instance/extended_description', instanceDescriptionController);
|
cacheControlMiddleware({ maxAge: 5, staleWhileRevalidate: 5, staleIfError: 21600, public: true }),
|
||||||
|
instanceV1Controller,
|
||||||
|
);
|
||||||
|
app.get(
|
||||||
|
'/api/v2/instance',
|
||||||
|
cacheControlMiddleware({ maxAge: 5, staleWhileRevalidate: 5, staleIfError: 21600, public: true }),
|
||||||
|
instanceV2Controller,
|
||||||
|
);
|
||||||
|
app.get(
|
||||||
|
'/api/v1/instance/extended_description',
|
||||||
|
cacheControlMiddleware({ maxAge: 5, staleWhileRevalidate: 5, staleIfError: 21600, public: true }),
|
||||||
|
instanceDescriptionController,
|
||||||
|
);
|
||||||
|
|
||||||
app.get('/api/v1/apps/verify_credentials', appCredentialsController);
|
app.get('/api/v1/apps/verify_credentials', appCredentialsController);
|
||||||
app.post('/api/v1/apps', createAppController);
|
app.post('/api/v1/apps', createAppController);
|
||||||
|
|
@ -296,12 +320,28 @@ app.get('/api/v1/preferences', preferencesController);
|
||||||
app.get('/api/v1/search', searchController);
|
app.get('/api/v1/search', searchController);
|
||||||
app.get('/api/v2/search', searchController);
|
app.get('/api/v2/search', searchController);
|
||||||
|
|
||||||
app.get('/api/pleroma/frontend_configurations', frontendConfigController);
|
app.get(
|
||||||
|
'/api/pleroma/frontend_configurations',
|
||||||
|
cacheControlMiddleware({ maxAge: 5, staleWhileRevalidate: 5, staleIfError: 21600, public: true }),
|
||||||
|
frontendConfigController,
|
||||||
|
);
|
||||||
|
|
||||||
app.get('/api/v1/trends/statuses', rateLimitMiddleware(8, Time.seconds(30)), trendingStatusesController);
|
app.get('/api/v1/trends/statuses', rateLimitMiddleware(8, Time.seconds(30)), trendingStatusesController);
|
||||||
app.get('/api/v1/trends/links', trendingLinksController);
|
app.get(
|
||||||
app.get('/api/v1/trends/tags', trendingTagsController);
|
'/api/v1/trends/links',
|
||||||
app.get('/api/v1/trends', trendingTagsController);
|
cacheControlMiddleware({ maxAge: 300, staleWhileRevalidate: 300, staleIfError: 21600, public: true }),
|
||||||
|
trendingLinksController,
|
||||||
|
);
|
||||||
|
app.get(
|
||||||
|
'/api/v1/trends/tags',
|
||||||
|
cacheControlMiddleware({ maxAge: 300, staleWhileRevalidate: 300, staleIfError: 21600, public: true }),
|
||||||
|
trendingTagsController,
|
||||||
|
);
|
||||||
|
app.get(
|
||||||
|
'/api/v1/trends',
|
||||||
|
cacheControlMiddleware({ maxAge: 300, staleWhileRevalidate: 300, staleIfError: 21600, public: true }),
|
||||||
|
trendingTagsController,
|
||||||
|
);
|
||||||
|
|
||||||
app.get('/api/v1/suggestions', suggestionsV1Controller);
|
app.get('/api/v1/suggestions', suggestionsV1Controller);
|
||||||
app.get('/api/v2/suggestions', suggestionsV2Controller);
|
app.get('/api/v2/suggestions', suggestionsV2Controller);
|
||||||
|
|
@ -345,7 +385,11 @@ app.post(
|
||||||
captchaVerifyController,
|
captchaVerifyController,
|
||||||
);
|
);
|
||||||
|
|
||||||
app.get('/api/v1/ditto/zap_splits', getZapSplitsController);
|
app.get(
|
||||||
|
'/api/v1/ditto/zap_splits',
|
||||||
|
cacheControlMiddleware({ maxAge: 5, staleWhileRevalidate: 5, public: true }),
|
||||||
|
getZapSplitsController,
|
||||||
|
);
|
||||||
app.get('/api/v1/ditto/:id{[0-9a-f]{64}}/zap_splits', statusZapSplitsController);
|
app.get('/api/v1/ditto/:id{[0-9a-f]{64}}/zap_splits', statusZapSplitsController);
|
||||||
|
|
||||||
app.put('/api/v1/admin/ditto/zap_splits', requireRole('admin'), updateZapSplitsController);
|
app.put('/api/v1/admin/ditto/zap_splits', requireRole('admin'), updateZapSplitsController);
|
||||||
|
|
@ -409,12 +453,36 @@ app.get('/timeline/*', frontendController);
|
||||||
|
|
||||||
// Known static file routes
|
// Known static file routes
|
||||||
app.get('/sw.js', publicFiles);
|
app.get('/sw.js', publicFiles);
|
||||||
app.get('/favicon.ico', publicFiles, staticFiles);
|
app.get(
|
||||||
app.get('/images/*', publicFiles, staticFiles);
|
'/favicon.ico',
|
||||||
app.get('/instance/*', publicFiles);
|
cacheControlMiddleware({ maxAge: 5, staleWhileRevalidate: 5, staleIfError: 21600, public: true }),
|
||||||
|
publicFiles,
|
||||||
|
staticFiles,
|
||||||
|
);
|
||||||
|
app.get(
|
||||||
|
'/images/*',
|
||||||
|
cacheControlMiddleware({ maxAge: 5, staleWhileRevalidate: 5, staleIfError: 21600, public: true }),
|
||||||
|
publicFiles,
|
||||||
|
staticFiles,
|
||||||
|
);
|
||||||
|
app.get(
|
||||||
|
'/instance/*',
|
||||||
|
cacheControlMiddleware({ maxAge: 5, staleWhileRevalidate: 5, staleIfError: 21600, public: true }),
|
||||||
|
publicFiles,
|
||||||
|
);
|
||||||
|
|
||||||
// Packs contains immutable static files
|
// Packs contains immutable static files
|
||||||
app.get('/packs/*', cacheControlMiddleware({ maxAge: 31536000, public: true, immutable: true }), publicFiles);
|
app.get(
|
||||||
|
'/packs/*',
|
||||||
|
cacheControlMiddleware({
|
||||||
|
maxAge: 31536000,
|
||||||
|
staleWhileRevalidate: 86400,
|
||||||
|
staleIfError: 21600,
|
||||||
|
public: true,
|
||||||
|
immutable: true,
|
||||||
|
}),
|
||||||
|
publicFiles,
|
||||||
|
);
|
||||||
|
|
||||||
// Site index
|
// Site index
|
||||||
app.get('/', frontendController, indexController);
|
app.get('/', frontendController, indexController);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,13 @@
|
||||||
import { type Context } from '@hono/hono';
|
import { Handler } from '@hono/hono';
|
||||||
|
|
||||||
const emptyArrayController = (c: Context) => c.json([]);
|
const emptyArrayController: Handler = (c) => {
|
||||||
const notImplementedController = (c: Context) => Promise.resolve(c.json({ error: 'Not implemented' }, 404));
|
c.header('Cache-Control', 'max-age=300, public, stale-while-revalidate=60');
|
||||||
|
return c.json([]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const notImplementedController: Handler = (c) => {
|
||||||
|
c.header('Cache-Control', 'max-age=300, public, stale-while-revalidate=60');
|
||||||
|
return c.json({ error: 'Not implemented' }, 404);
|
||||||
|
};
|
||||||
|
|
||||||
export { emptyArrayController, notImplementedController };
|
export { emptyArrayController, notImplementedController };
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ export const frontendController: AppMiddleware = async (c, next) => {
|
||||||
try {
|
try {
|
||||||
const entities = await getEntities(params ?? {});
|
const entities = await getEntities(params ?? {});
|
||||||
const meta = renderMetadata(c.req.url, entities);
|
const meta = renderMetadata(c.req.url, entities);
|
||||||
|
c.header('Cache-Control', 'max-age=30, public, stale-while-revalidate=30');
|
||||||
return c.html(content.replace(META_PLACEHOLDER, meta));
|
return c.html(content.replace(META_PLACEHOLDER, meta));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(`Error building meta tags: ${e}`);
|
console.log(`Error building meta tags: ${e}`);
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ const emptyResult: NostrJson = { names: {}, relays: {} };
|
||||||
const nostrController: AppController = async (c) => {
|
const nostrController: AppController = async (c) => {
|
||||||
// If there are no query parameters, this will always return an empty result.
|
// If there are no query parameters, this will always return an empty result.
|
||||||
if (!Object.entries(c.req.queries()).length) {
|
if (!Object.entries(c.req.queries()).length) {
|
||||||
c.header('Cache-Control', 'max-age=31536000, public, immutable');
|
c.header('Cache-Control', 'max-age=31536000, public, immutable, stale-while-revalidate=86400');
|
||||||
return c.json(emptyResult);
|
return c.json(emptyResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -26,14 +26,14 @@ const nostrController: AppController = async (c) => {
|
||||||
|
|
||||||
if (!name || !pointer) {
|
if (!name || !pointer) {
|
||||||
// Not found, cache for 5 minutes.
|
// Not found, cache for 5 minutes.
|
||||||
c.header('Cache-Control', 'max-age=300, public');
|
c.header('Cache-Control', 'max-age=300, public, stale-while-revalidate=30');
|
||||||
return c.json(emptyResult);
|
return c.json(emptyResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { pubkey, relays = [] } = pointer;
|
const { pubkey, relays = [] } = pointer;
|
||||||
|
|
||||||
// It's found, so cache for 12 hours.
|
// It's found, so cache for 6 hours.
|
||||||
c.header('Cache-Control', 'max-age=43200, public');
|
c.header('Cache-Control', 'max-age=21600, public, stale-while-revalidate=3600');
|
||||||
|
|
||||||
return c.json(
|
return c.json(
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue