4 Commits

Author SHA1 Message Date
typist
5cef15c032 0.0.32
Some checks failed
Build and Push Docker Image / build (push) Failing after 1m12s
2025-10-30 09:44:05 +08:00
typist
970d1ac3ed feat: conditionally inject Cloudflare Analytics script
- Updated index.html to include a placeholder for Cloudflare Web Analytics, which is now only injected in production mode.
- Modified vite.config.ts to implement an HTML transform plugin that dynamically adds the Cloudflare script based on the environment.
2025-10-30 09:43:50 +08:00
typist
0d6334592d 0.0.31
Some checks failed
Build and Push Docker Image / build (push) Failing after 1m26s
2025-10-30 09:39:49 +08:00
typist
c501bb7dd4 feat: enhance currency caching strategy
- Updated the currency tool to include a timestamp for cached data.
- Implemented a 12-hour validity check for cached exchange rates to improve data accuracy and user experience.
- Ensured that outdated cache is still displayed while fetching new data in the background.
2025-10-30 09:39:34 +08:00
4 changed files with 29 additions and 13 deletions

View File

@@ -65,8 +65,8 @@
<div id="root"></div> <div id="root"></div>
<script type="module" src="/src/main.tsx"></script> <script type="module" src="/src/main.tsx"></script>
<!-- Cloudflare Web Analytics --> <!-- Cloudflare Web Analytics - Only in Production -->
<script defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "2aecdc025eb043bc89ce931b54a80054"}'></script> <!--CLOUDFLARE_ANALYTICS_PLACEHOLDER-->
<!-- End Cloudflare Web Analytics --> <!-- End Cloudflare Web Analytics -->
</body> </body>
</html> </html>

View File

@@ -1,7 +1,7 @@
{ {
"name": "litek", "name": "litek",
"private": true, "private": true,
"version": "0.0.30", "version": "0.0.32",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",

View File

@@ -99,18 +99,20 @@ const Tool: FC = () => {
const cached = localStorage.getItem(RATES_CACHE_KEY); const cached = localStorage.getItem(RATES_CACHE_KEY);
if (cached) { if (cached) {
try { try {
const { rates: cachedRates, date: cachedDate } = JSON.parse(cached); const { rates: cachedRates, date: cachedDate, fetchedAt } = JSON.parse(cached);
// Check if cached data is from today const now = Date.now();
const today = new Date().toISOString().split('T')[0]; // YYYY-MM-DD const twelveHoursInMs = 12 * 60 * 60 * 1000;
if (cachedDate === today) { // Strategy: Use cache if it's recent enough (within 12 hours)
// Cache is valid (same date), use it directly // This handles all edge cases: weekends, holidays, timezone differences
// Exchange rates update once per day, so 12-hour cache is reasonable
if (fetchedAt && (now - fetchedAt < twelveHoursInMs)) {
setRates(cachedRates); setRates(cachedRates);
setLoading(false); setLoading(false);
return; return;
} else { } else {
// Cache is outdated, show old data first then update in background // Cache is older than 12 hours, show it first then update in background
setRates(cachedRates); setRates(cachedRates);
setLoading(false); setLoading(false);
// Continue to fetch new data below // Continue to fetch new data below
@@ -134,11 +136,15 @@ const Tool: FC = () => {
const allRates: Record<string, number> = { USD: 1, ...data.rates }; const allRates: Record<string, number> = { USD: 1, ...data.rates };
const apiDate = data.date; // Date from API (YYYY-MM-DD format) const apiDate = data.date; // Date from API (YYYY-MM-DD format)
// 3. Update state and cache // 3. Update state and cache (with timestamp)
setRates(allRates); setRates(allRates);
localStorage.setItem( localStorage.setItem(
RATES_CACHE_KEY, RATES_CACHE_KEY,
JSON.stringify({ rates: allRates, date: apiDate }) JSON.stringify({
rates: allRates,
date: apiDate,
fetchedAt: Date.now() // Timestamp when we fetched the data
})
); );
} catch (error) { } catch (error) {
// If cache exists, continue using it even if network fails // If cache exists, continue using it even if network fails

View File

@@ -5,10 +5,20 @@ import tailwindcss from "@tailwindcss/vite"
import { VitePWA } from 'vite-plugin-pwa' import { VitePWA } from 'vite-plugin-pwa'
// https://vite.dev/config/ // https://vite.dev/config/
export default defineConfig({ export default defineConfig(({ mode }) => ({
plugins: [ plugins: [
react(), react(),
tailwindcss(), tailwindcss(),
// HTML 替换插件 - 仅在生产环境注入 Cloudflare Analytics
{
name: 'html-transform',
transformIndexHtml(html) {
const cloudflareScript = mode === 'production'
? `<script defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "2aecdc025eb043bc89ce931b54a80054"}'></script>`
: '';
return html.replace('<!--CLOUDFLARE_ANALYTICS_PLACEHOLDER-->', cloudflareScript);
}
},
VitePWA({ VitePWA({
registerType: 'autoUpdate', registerType: 'autoUpdate',
includeAssets: ['lite.svg', 'robots.txt', 'sitemap.xml'], includeAssets: ['lite.svg', 'robots.txt', 'sitemap.xml'],
@@ -130,4 +140,4 @@ export default defineConfig({
}, },
chunkSizeWarningLimit: 500, chunkSizeWarningLimit: 500,
}, },
}) }))