From 6b028fdbda598d83a3dbd1d3ca97cf04375163c8 Mon Sep 17 00:00:00 2001 From: Ben Date: Sun, 17 May 2026 21:45:59 -0700 Subject: [PATCH] Refactor cache control settings and improve robots.txt handling; update definition retrieval to synchronous method --- hdyc-svelte/src/hooks.server.ts | 5 ++++- .../components/QuickConversionExample.svelte | 12 ++++-------- .../lib/components/QuickDefinitionCard.svelte | 18 ++++++++---------- hdyc-svelte/src/lib/data/unitDefinitions.ts | 9 +++++++++ hdyc-svelte/src/routes/+page.ts | 4 +--- hdyc-svelte/src/routes/robots.txt/+server.ts | 16 ++++++++++++++++ hdyc-svelte/src/routes/sitemap.xml/+server.ts | 4 +++- hdyc-svelte/static/robots.txt | 4 ---- 8 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 hdyc-svelte/src/routes/robots.txt/+server.ts delete mode 100644 hdyc-svelte/static/robots.txt diff --git a/hdyc-svelte/src/hooks.server.ts b/hdyc-svelte/src/hooks.server.ts index 02656a2..5b5af5d 100644 --- a/hdyc-svelte/src/hooks.server.ts +++ b/hdyc-svelte/src/hooks.server.ts @@ -12,7 +12,8 @@ const MIME_TYPES: Record = { '.otf': 'font/otf' }; -const HTML_CACHE_CONTROL = 'public, max-age=0, s-maxage=3600, stale-while-revalidate=86400'; +const HTML_CACHE_CONTROL = 'no-cache, max-age=0, must-revalidate, no-transform'; +const EDGE_HTML_CACHE_CONTROL = 'max-age=300, stale-while-revalidate=86400'; const IMMUTABLE_ASSET_CACHE_CONTROL = 'public, max-age=31536000, immutable'; const ASSET_404_CACHE_CONTROL = 'no-store'; const LONG_CACHE_EXTENSIONS = new Set([ @@ -71,6 +72,8 @@ export const handle: Handle = async ({ event, resolve }) => { // bundle hashes after each deployment. if (contentType.includes('text/html')) { response.headers.set('cache-control', HTML_CACHE_CONTROL); + response.headers.set('cdn-cache-control', EDGE_HTML_CACHE_CONTROL); + response.headers.set('cloudflare-cdn-cache-control', EDGE_HTML_CACHE_CONTROL); } return response; diff --git a/hdyc-svelte/src/lib/components/QuickConversionExample.svelte b/hdyc-svelte/src/lib/components/QuickConversionExample.svelte index 1407027..d459a61 100644 --- a/hdyc-svelte/src/lib/components/QuickConversionExample.svelte +++ b/hdyc-svelte/src/lib/components/QuickConversionExample.svelte @@ -39,14 +39,10 @@ {#if supportsExample && result}

How to convert {config.labels.in1} to {config.labels.in2}

- {#if hasOffset} -

- Formula: {config.labels.in2} = ({config.labels.in1} x {formattedFactorValue}) {offset > 0 ? '+' : '-'} {formatConversionValue(Math.abs(offset))} -

- {:else} -

- 1 {config.labels.in1} = {formattedFactorValue} {config.labels.in2} -

+

+ 1 {config.labels.in1} = {formattedFactorValue}{hasOffset ? ` + ${formattedOffsetValue}` : ''} {config.labels.in2} +

+ {#if !hasOffset}

1 {config.labels.in2} = {formattedReverseValue} {config.labels.in1}

diff --git a/hdyc-svelte/src/lib/components/QuickDefinitionCard.svelte b/hdyc-svelte/src/lib/components/QuickDefinitionCard.svelte index 26308f9..f20ffcb 100644 --- a/hdyc-svelte/src/lib/components/QuickDefinitionCard.svelte +++ b/hdyc-svelte/src/lib/components/QuickDefinitionCard.svelte @@ -1,20 +1,18 @@
@@ -22,11 +20,11 @@
{label1} -

{def1 ?? `Definition pending for ${label1}.`}

+

{def1}

{label2} -

{def2 ?? `Definition pending for ${label2}.`}

+

{def2}

diff --git a/hdyc-svelte/src/lib/data/unitDefinitions.ts b/hdyc-svelte/src/lib/data/unitDefinitions.ts index ee76e6a..0c24054 100644 --- a/hdyc-svelte/src/lib/data/unitDefinitions.ts +++ b/hdyc-svelte/src/lib/data/unitDefinitions.ts @@ -100,6 +100,9 @@ const normalizeLabel = (label?: string): string | undefined => { const categoryPriority = [...Object.keys(domainDefinitions)]; const specificDefinitions: Array<[RegExp, string]> = [ + [/\btons? of tnt\b/i, 'measures explosive energy release. One ton of TNT is conventionally defined as 4.184 gigajoules of energy.'], + [/\batomic time units?\b/i, 'measures time on an atomic scale. One atomic unit of time is about 2.418884326505e-17 seconds.'], + [/\bastronomical units?\b/i, 'measures large distances in astronomy. One astronomical unit is the average Earth-Sun distance, exactly 149,597,870,700 meters.'], [/\bwatts?\b|\bkilowatts?\b|\bmegawatts?\b|\bhorsepower\b/i, 'measures the rate at which energy is transferred or converted. It is used for engines, appliances, electrical loads, and heat-transfer rates.'], [/\bcalories?\b|\bjoules?\b|\bbtu\b|\bkilocalories?\b|\btherms?\b|\bwatt hours?\b|\bkilowatt hours?\b/i, 'measures energy, heat, or work. These units are used for food energy, physics calculations, heating systems, and stored electricity.'], [/\bvolts?\b/i, 'measures electric potential difference. Voltage describes the electrical pressure that pushes current through a circuit.'], @@ -177,3 +180,9 @@ export async function getDefinition(label: string, category?: string): Promise + new Response(robots, { + headers: { + 'Content-Type': 'text/plain; charset=utf-8', + 'Cache-Control': 'no-cache, max-age=0, must-revalidate, no-transform', + 'CDN-Cache-Control': 'max-age=300, stale-while-revalidate=86400', + 'Cloudflare-CDN-Cache-Control': 'max-age=300, stale-while-revalidate=86400' + } + }); diff --git a/hdyc-svelte/src/routes/sitemap.xml/+server.ts b/hdyc-svelte/src/routes/sitemap.xml/+server.ts index d810d21..a23595a 100644 --- a/hdyc-svelte/src/routes/sitemap.xml/+server.ts +++ b/hdyc-svelte/src/routes/sitemap.xml/+server.ts @@ -33,7 +33,9 @@ ${calculatorUrls.join('\n')} return new Response(sitemap, { headers: { 'Content-Type': 'application/xml', - 'Cache-Control': 'max-age=0, s-maxage=3600' + 'Cache-Control': 'no-cache, max-age=0, must-revalidate, no-transform', + 'CDN-Cache-Control': 'max-age=300, stale-while-revalidate=86400', + 'Cloudflare-CDN-Cache-Control': 'max-age=300, stale-while-revalidate=86400' } }); }; diff --git a/hdyc-svelte/static/robots.txt b/hdyc-svelte/static/robots.txt deleted file mode 100644 index 99801e1..0000000 --- a/hdyc-svelte/static/robots.txt +++ /dev/null @@ -1,4 +0,0 @@ -# allow crawling everything by default -User-agent: * -Disallow: -Sitemap: https://howdoyouconvert.com/sitemap.xml