Skip to content

Commit

Permalink
More consistency with Cache-Control header (sitemaps and more)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Jul 14, 2024
1 parent 4902590 commit 24406e8
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/app-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {join} from 'path';
import {makeLogger, errorLogger, accessLogger, makeLogInfo,
logMemoryUsage} from './logger.js';
import {httpRedirect, stopIfTimedOut, cacheControl,
CACHE_CONTROL_30DAYS,
CACHE_CONTROL_IMMUTABLE} from './common.js';
import {hebcalDownload} from './hebcal-download.js';
import {yahrzeitDownload} from './yahrzeit.js';
Expand Down Expand Up @@ -287,7 +288,7 @@ app.use(async function router(ctx, next) {
if (typeof vv === 'string' && vv[0] === 'y') {
return yahrzeitDownload(ctx);
} else if (vv === '1' || vv === 'now') {
ctx.set('Cache-Control', 'max-age=2592000');
ctx.set('Cache-Control', CACHE_CONTROL_30DAYS);
return hebcalDownload(ctx);
} else {
const status = typeof vv === 'undefined' ? 404 : 400;
Expand Down
4 changes: 3 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,9 @@ export const localeMap = {
'uk': 'uk',
};

export const CACHE_CONTROL_IMMUTABLE = cacheControl(365) + ', immutable';
export const CACHE_CONTROL_1_YEAR = cacheControl(365);
export const CACHE_CONTROL_IMMUTABLE = CACHE_CONTROL_1_YEAR + ', immutable';
export const CACHE_CONTROL_30DAYS = cacheControl(30);
export const CACHE_CONTROL_7DAYS = cacheControl(7);

/**
Expand Down
3 changes: 2 additions & 1 deletion src/complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {cacheControl} from './common.js';
import flag from 'emoji-flag';

const NOTFOUND = {error: 'Not Found'};
const CACHE_CONTROL_3DAYS = cacheControl(3);

export async function geoAutoComplete(ctx) {
ctx.lastModified = ctx.launchDate;
Expand Down Expand Up @@ -32,7 +33,7 @@ export async function geoAutoComplete(ctx) {
item.flag = flag(cc.toUpperCase());
}
}
ctx.set('Cache-Control', cacheControl(1));
ctx.set('Cache-Control', CACHE_CONTROL_3DAYS);
ctx.body = items;
} else {
ctx.status = 404;
Expand Down
9 changes: 4 additions & 5 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {HDate, HebrewCalendar, Event, ParshaEvent, Locale, months,
import dayjs from 'dayjs';
import {setDefautLangTz, httpRedirect, lgToLocale,
localeMap,
CACHE_CONTROL_7DAYS, cacheControl,
CACHE_CONTROL_7DAYS, CACHE_CONTROL_1_YEAR,
} from './common.js';
import {makeGregDate, getBeforeAfterSunsetForLocation,
getStartAndEnd,
Expand All @@ -15,7 +15,6 @@ import {pad4, makeAnchor} from '@hebcal/rest-api';
import './dayjs-locales.js';
import {gematriyaDate} from './gematriyaDate.js';

const CACHE_CONTROL_ONE_YEAR = cacheControl(365);
/**
* @param {string} val
* @return {boolean}
Expand Down Expand Up @@ -75,7 +74,7 @@ export async function hebrewDateConverter(ctx) {
ctx.body = {error: p.message};
} else if (typeof p.hdates === 'object') {
ctx.lastModified = ctx.launchDate;
ctx.set('Cache-Control', CACHE_CONTROL_ONE_YEAR);
ctx.set('Cache-Control', CACHE_CONTROL_1_YEAR);
let result = p;
const cb = empty(q.callback) ? false : q.callback.replace(/[^\w\.]/g, '');
if (cb) {
Expand All @@ -85,7 +84,7 @@ export async function hebrewDateConverter(ctx) {
} else {
if (!p.noCache) {
ctx.lastModified = ctx.launchDate;
ctx.set('Cache-Control', CACHE_CONTROL_ONE_YEAR);
ctx.set('Cache-Control', CACHE_CONTROL_1_YEAR);
}
let result = {
gy: p.gy,
Expand Down Expand Up @@ -123,7 +122,7 @@ export async function hebrewDateConverter(ctx) {
p.writeResp = false;
if (!p.noCache) {
ctx.lastModified = ctx.launchDate;
ctx.set('Cache-Control', CACHE_CONTROL_ONE_YEAR);
ctx.set('Cache-Control', CACHE_CONTROL_1_YEAR);
}
ctx.body = await ctx.render('converter-xml', p);
}
Expand Down
33 changes: 19 additions & 14 deletions src/dailyLearning.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from 'dayjs';
import {localeMap, httpRedirect} from './common.js';
import {localeMap, httpRedirect, CACHE_CONTROL_1_YEAR} from './common.js';
import {isoDateStringToDate} from './dateUtil.js';
import {basename} from 'path';
import {HDate, HebrewCalendar, months, OmerEvent, Locale} from '@hebcal/core';
Expand Down Expand Up @@ -39,19 +39,7 @@ export function dailyLearningApp(ctx) {
httpRedirect(ctx, `/learning/${d.format('YYYY-MM-DD')}`, 302);
return;
} else if (rpath === '/learning/sitemap.txt') {
const prefix = 'https://www.hebcal.com/learning';
let body = '';
for (let i = -1; i < 4; i++) {
const year = currentYear + i;
const startD = dayjs(new Date(year, 0, 1));
const endD = dayjs(new Date(year + 1, 0, 1));
for (let d = startD; d.isBefore(endD); d = d.add(1, 'day')) {
body += prefix + '/' + d.format('YYYY-MM-DD') + '\n';
}
}
ctx.lastModified = ctx.launchDate;
ctx.type = 'text/plain';
ctx.body = body;
dailyLearningSitemap(ctx);
return;
}
const dt = isoDateStringToDate(basename(rpath));
Expand Down Expand Up @@ -151,3 +139,20 @@ export function dailyLearningApp(ctx) {
il,
});
}

function dailyLearningSitemap(ctx) {
const prefix = 'https://www.hebcal.com/learning';
let body = '';
for (let i = -1; i < 4; i++) {
const year = currentYear + i;
const startD = dayjs(new Date(year, 0, 1));
const endD = dayjs(new Date(year + 1, 0, 1));
for (let d = startD; d.isBefore(endD); d = d.add(1, 'day')) {
body += prefix + '/' + d.format('YYYY-MM-DD') + '\n';
}
}
ctx.lastModified = ctx.launchDate;
ctx.set('Cache-Control', CACHE_CONTROL_1_YEAR);
ctx.type = 'text/plain';
ctx.body = body;
}
5 changes: 1 addition & 4 deletions src/omerApp.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import dayjs from 'dayjs';
import {cacheControl, httpRedirect} from './common.js';
import {httpRedirect, CACHE_CONTROL_1_YEAR, CACHE_CONTROL_30DAYS} from './common.js';
import {getDefaultHebrewYear} from './dateUtil.js';
import {basename, dirname} from 'path';
import {HDate, months, OmerEvent, HebrewCalendar, Locale} from '@hebcal/core';
import {makeDownloadProps} from './makeDownloadProps.js';
import {getHolidayMeta} from './getHolidayMeta.js';

const CACHE_CONTROL_30DAYS = cacheControl(30);
const CACHE_CONTROL_1_YEAR = cacheControl(365);

export async function omerApp(rpath, ctx) {
if (rpath === '/omer/sitemap.txt') {
const prefix = 'https://www.hebcal.com/omer';
Expand Down
6 changes: 4 additions & 2 deletions src/shabbat-browse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {basename} from 'path';
import utc from 'dayjs/plugin/utc.js';
import timezone from 'dayjs/plugin/timezone.js';
import {langTzDefaults, CACHE_CONTROL_7DAYS,
CACHE_CONTROL_30DAYS,
queryDefaultCandleMins} from './common.js';
import {expiresSaturdayNight} from './dateUtil.js';
import flag from 'emoji-flag';
Expand Down Expand Up @@ -132,14 +133,15 @@ export async function shabbatBrowse(ctx) {
const base = base0.endsWith('.xml') ? base0.substring(0, base0.length - 4) : base0;
if (rpath === '/shabbat/browse/') {
ctx.lastModified = ctx.launchDate;
ctx.set('Cache-Control', 'max-age=2592000');
ctx.set('Cache-Control', CACHE_CONTROL_30DAYS);
return ctx.render('shabbat-browse', {
title: 'Shabbat candle-lighting times for world cities - Hebcal',
continents: Object.values(continents),
});
}
if (rpath === '/shabbat/browse/sitemap.xml') {
ctx.type = 'text/xml';
ctx.lastModified = new Date();
ctx.set('Cache-Control', CACHE_CONTROL_7DAYS);
ctx.body = await ctx.render('shabbat-browse-sitemap', {
writeResp: false,
Expand Down Expand Up @@ -196,7 +198,7 @@ async function countryPage(ctx, countryCode) {
const countryUrlToken = makeAnchor(countryName);
listItems.forEach((a1) => a1.href = countryUrlToken + '-' + a1.id);
ctx.lastModified = ctx.launchDate;
ctx.set('Cache-Control', 'max-age=2592000');
ctx.set('Cache-Control', CACHE_CONTROL_30DAYS);
return render(ctx, 'shabbat-browse-admin1', {
title: `${countryName} Shabbat Times - Hebcal`,
countryCode,
Expand Down
6 changes: 3 additions & 3 deletions src/sitemapZips.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Database from 'better-sqlite3';
import {cacheControl} from './common.js';
import {CACHE_CONTROL_30DAYS} from './common.js';

export async function sitemapZips(ctx) {
const db = new Database('zips.sqlite3', {fileMustExist: true});
Expand All @@ -9,8 +9,8 @@ export async function sitemapZips(ctx) {
.map((r) => `https://www.hebcal.com/shabbat?zip=${r.ZipCode}&b=18&M=on&lg=s\n`)
.join('');
db.close();
ctx.lastModified = ctx.launchDate;
ctx.set('Cache-Control', cacheControl(30));
ctx.lastModified = new Date();
ctx.set('Cache-Control', CACHE_CONTROL_30DAYS);
ctx.type = 'text/plain';
ctx.body = body;
}
4 changes: 2 additions & 2 deletions src/zmanim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Zmanim, TimedEvent, HDate, flags,
HebrewCalendar, HebrewDateEvent, Locale} from '@hebcal/core';
import {empty} from './empty.js';
import {getLocationFromQuery,
CACHE_CONTROL_7DAYS,
CACHE_CONTROL_7DAYS, CACHE_CONTROL_30DAYS,
lgToLocale, eTagFromOptions} from './common.js';
import {nowInTimezone, getStartAndEnd} from './dateUtil.js';
import createError from 'http-errors';
Expand Down Expand Up @@ -75,7 +75,7 @@ export async function getZmanim(ctx) {
}
const {isRange, startD, endD} = myGetStartAndEnd(ctx, q, loc.getTzid());
if (isRange || !empty(q.date)) {
ctx.set('Cache-Control', 'max-age=2592000');
ctx.set('Cache-Control', CACHE_CONTROL_30DAYS);
ctx.lastModified = new Date();
} else {
expires(ctx);
Expand Down

0 comments on commit 24406e8

Please sign in to comment.