Compare commits
	
		
			9 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | ea62204d8e | ||
|   | 3bafcbafed | ||
|   | 1a00f27c6a | ||
|   | 1bcbd3e37d | ||
|   | e88770dd3f | ||
|   | 5cef15c032 | ||
|   | 970d1ac3ed | ||
|   | 0d6334592d | ||
|   | c501bb7dd4 | 
| @@ -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> | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| { | { | ||||||
|   "name": "litek", |   "name": "litek", | ||||||
|   "private": true, |   "private": true, | ||||||
|   "version": "0.0.30", |   "version": "0.0.34", | ||||||
|   "type": "module", |   "type": "module", | ||||||
|   "scripts": { |   "scripts": { | ||||||
|     "dev": "vite", |     "dev": "vite", | ||||||
|   | |||||||
| @@ -53,7 +53,6 @@ const AVAILABLE_CURRENCIES: Currency[] = [ | |||||||
| const REQUIRED_CURRENCIES = ["USD", "CNY"]; | const REQUIRED_CURRENCIES = ["USD", "CNY"]; | ||||||
|  |  | ||||||
| const STORAGE_KEY = "selectedCurrencies"; | const STORAGE_KEY = "selectedCurrencies"; | ||||||
| const RATES_CACHE_KEY = "currencyRatesCache"; |  | ||||||
|  |  | ||||||
| const Tool: FC = () => { | const Tool: FC = () => { | ||||||
|   const [amounts, setAmounts] = useState<Record<string, string>>({}); |   const [amounts, setAmounts] = useState<Record<string, string>>({}); | ||||||
| @@ -91,40 +90,13 @@ const Tool: FC = () => { | |||||||
|     } |     } | ||||||
|   }, [selectedCurrencies]); |   }, [selectedCurrencies]); | ||||||
|  |  | ||||||
|   // Fetch exchange rates with date-based caching |   // Fetch exchange rates (Service Worker handles caching) | ||||||
|   useEffect(() => { |   useEffect(() => { | ||||||
|     const fetchRates = async () => { |     const fetchRates = async () => { | ||||||
|       try { |       try { | ||||||
|         // 1. Try to load from cache |  | ||||||
|         const cached = localStorage.getItem(RATES_CACHE_KEY); |  | ||||||
|         if (cached) { |  | ||||||
|           try { |  | ||||||
|             const { rates: cachedRates, date: cachedDate } = JSON.parse(cached); |  | ||||||
|              |  | ||||||
|             // Check if cached data is from today |  | ||||||
|             const today = new Date().toISOString().split('T')[0]; // YYYY-MM-DD |  | ||||||
|              |  | ||||||
|             if (cachedDate === today) { |  | ||||||
|               // Cache is valid (same date), use it directly |  | ||||||
|               setRates(cachedRates); |  | ||||||
|               setLoading(false); |  | ||||||
|               return; |  | ||||||
|             } else { |  | ||||||
|               // Cache is outdated, show old data first then update in background |  | ||||||
|               setRates(cachedRates); |  | ||||||
|               setLoading(false); |  | ||||||
|               // Continue to fetch new data below |  | ||||||
|             } |  | ||||||
|           } catch (e) { |  | ||||||
|             console.error("Failed to parse cached rates:", e); |  | ||||||
|           } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         // 2. Fetch latest data from network |  | ||||||
|         setLoading(true); |         setLoading(true); | ||||||
|         const response = await fetch("https://api.frankfurter.app/latest?base=USD", { |         // Service Worker will handle caching with StaleWhileRevalidate strategy | ||||||
|           cache: "no-cache", |         const response = await fetch("https://api.frankfurter.app/latest?base=USD"); | ||||||
|         }); |  | ||||||
|          |          | ||||||
|         if (!response.ok) { |         if (!response.ok) { | ||||||
|           throw new Error(`HTTP error! status: ${response.status}`); |           throw new Error(`HTTP error! status: ${response.status}`); | ||||||
| @@ -132,36 +104,14 @@ const Tool: FC = () => { | |||||||
|          |          | ||||||
|         const data = await response.json(); |         const data = await response.json(); | ||||||
|         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) |  | ||||||
|          |          | ||||||
|         // 3. Update state and cache |  | ||||||
|         setRates(allRates); |         setRates(allRates); | ||||||
|         localStorage.setItem( |  | ||||||
|           RATES_CACHE_KEY, |  | ||||||
|           JSON.stringify({ rates: allRates, date: apiDate }) |  | ||||||
|         ); |  | ||||||
|       } catch (error) { |       } catch (error) { | ||||||
|         // If cache exists, continue using it even if network fails |         console.error("Failed to fetch rates:", error); | ||||||
|         const cached = localStorage.getItem(RATES_CACHE_KEY); |  | ||||||
|         if (cached) { |  | ||||||
|           try { |  | ||||||
|             const { rates: cachedRates } = JSON.parse(cached); |  | ||||||
|             setRates(cachedRates); |  | ||||||
|             toast.info("Using cached exchange rates (network request failed)"); |  | ||||||
|           } catch (e) { |  | ||||||
|             console.error("Failed to use cached rates:", e); |  | ||||||
|         if (error instanceof Error) { |         if (error instanceof Error) { | ||||||
|               toast.error(`Failed to fetch rates: ${error.message}`); |           toast.error(`Failed to fetch exchange rates: ${error.message}`); | ||||||
|         } else { |         } else { | ||||||
|               toast.error("Failed to fetch rates"); |           toast.error("Failed to fetch exchange rates"); | ||||||
|             } |  | ||||||
|           } |  | ||||||
|         } else { |  | ||||||
|           if (error instanceof Error) { |  | ||||||
|             toast.error(`Failed to fetch rates: ${error.message}`); |  | ||||||
|           } else { |  | ||||||
|             toast.error("Failed to fetch rates"); |  | ||||||
|           } |  | ||||||
|         } |         } | ||||||
|       } finally { |       } finally { | ||||||
|         setLoading(false); |         setLoading(false); | ||||||
|   | |||||||
| @@ -1,23 +0,0 @@ | |||||||
| @layer base { |  | ||||||
|   :root { |  | ||||||
|     --sidebar: oklch(0.985 0 0); |  | ||||||
|     --sidebar-foreground: oklch(0.145 0 0); |  | ||||||
|     --sidebar-primary: oklch(0.205 0 0); |  | ||||||
|     --sidebar-primary-foreground: oklch(0.985 0 0); |  | ||||||
|     --sidebar-accent: oklch(0.97 0 0); |  | ||||||
|     --sidebar-accent-foreground: oklch(0.205 0 0); |  | ||||||
|     --sidebar-border: oklch(0.922 0 0); |  | ||||||
|     --sidebar-ring: oklch(0.708 0 0); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   .dark { |  | ||||||
|     --sidebar: oklch(0.205 0 0); |  | ||||||
|     --sidebar-foreground: oklch(0.985 0 0); |  | ||||||
|     --sidebar-primary: oklch(0.488 0.243 264.376); |  | ||||||
|     --sidebar-primary-foreground: oklch(0.985 0 0); |  | ||||||
|     --sidebar-accent: oklch(0.269 0 0); |  | ||||||
|     --sidebar-accent-foreground: oklch(0.985 0 0); |  | ||||||
|     --sidebar-border: oklch(1 0 0 / 10%); |  | ||||||
|     --sidebar-ring: oklch(0.439 0 0); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
							
								
								
									
										110
									
								
								src/index.css
									
									
									
									
									
								
							
							
						
						
									
										110
									
								
								src/index.css
									
									
									
									
									
								
							| @@ -42,72 +42,72 @@ | |||||||
| } | } | ||||||
|  |  | ||||||
| :root { | :root { | ||||||
|   --radius: 0.625rem; |   --radius: 0.65rem; | ||||||
|   --background: oklch(1 0 0); |   --background: oklch(1 0 0); | ||||||
|   --foreground: oklch(0.13 0.028 261.692); |   --foreground: oklch(0.141 0.005 285.823); | ||||||
|   --card: oklch(1 0 0); |   --card: oklch(1 0 0); | ||||||
|   --card-foreground: oklch(0.13 0.028 261.692); |   --card-foreground: oklch(0.141 0.005 285.823); | ||||||
|   --popover: oklch(1 0 0); |   --popover: oklch(1 0 0); | ||||||
|   --popover-foreground: oklch(0.13 0.028 261.692); |   --popover-foreground: oklch(0.141 0.005 285.823); | ||||||
|   --primary: oklch(0.21 0.034 264.665); |   --primary: oklch(0.646 0.222 41.116); | ||||||
|   --primary-foreground: oklch(0.985 0.002 247.839); |   --primary-foreground: oklch(0.98 0.016 73.684); | ||||||
|   --secondary: oklch(0.967 0.003 264.542); |   --secondary: oklch(0.967 0.001 286.375); | ||||||
|   --secondary-foreground: oklch(0.21 0.034 264.665); |   --secondary-foreground: oklch(0.21 0.006 285.885); | ||||||
|   --muted: oklch(0.967 0.003 264.542); |   --muted: oklch(0.967 0.001 286.375); | ||||||
|   --muted-foreground: oklch(0.551 0.027 264.364); |   --muted-foreground: oklch(0.552 0.016 285.938); | ||||||
|   --accent: oklch(0.967 0.003 264.542); |   --accent: oklch(0.967 0.001 286.375); | ||||||
|   --accent-foreground: oklch(0.21 0.034 264.665); |   --accent-foreground: oklch(0.21 0.006 285.885); | ||||||
|   --destructive: oklch(0.577 0.245 27.325); |   --destructive: oklch(0.577 0.245 27.325); | ||||||
|   --border: oklch(0.928 0.006 264.531); |   --border: oklch(0.92 0.004 286.32); | ||||||
|   --input: oklch(0.928 0.006 264.531); |   --input: oklch(0.92 0.004 286.32); | ||||||
|   --ring: oklch(0.707 0.022 261.325); |   --ring: oklch(0.75 0.183 55.934); | ||||||
|   --chart-1: oklch(0.646 0.222 41.116); |   --chart-1: oklch(0.837 0.128 66.29); | ||||||
|   --chart-2: oklch(0.6 0.118 184.704); |   --chart-2: oklch(0.705 0.213 47.604); | ||||||
|   --chart-3: oklch(0.398 0.07 227.392); |   --chart-3: oklch(0.646 0.222 41.116); | ||||||
|   --chart-4: oklch(0.828 0.189 84.429); |   --chart-4: oklch(0.553 0.195 38.402); | ||||||
|   --chart-5: oklch(0.769 0.188 70.08); |   --chart-5: oklch(0.47 0.157 37.304); | ||||||
|   --sidebar: oklch(0.985 0.002 247.839); |   --sidebar: oklch(0.985 0 0); | ||||||
|   --sidebar-foreground: oklch(0.13 0.028 261.692); |   --sidebar-foreground: oklch(0.141 0.005 285.823); | ||||||
|   --sidebar-primary: oklch(0.21 0.034 264.665); |   --sidebar-primary: oklch(0.646 0.222 41.116); | ||||||
|   --sidebar-primary-foreground: oklch(0.985 0.002 247.839); |   --sidebar-primary-foreground: oklch(0.98 0.016 73.684); | ||||||
|   --sidebar-accent: oklch(0.967 0.003 264.542); |   --sidebar-accent: oklch(0.967 0.001 286.375); | ||||||
|   --sidebar-accent-foreground: oklch(0.21 0.034 264.665); |   --sidebar-accent-foreground: oklch(0.21 0.006 285.885); | ||||||
|   --sidebar-border: oklch(0.928 0.006 264.531); |   --sidebar-border: oklch(0.92 0.004 286.32); | ||||||
|   --sidebar-ring: oklch(0.707 0.022 261.325); |   --sidebar-ring: oklch(0.75 0.183 55.934); | ||||||
| } | } | ||||||
|  |  | ||||||
| .dark { | .dark { | ||||||
|   --background: oklch(0.13 0.028 261.692); |   --background: oklch(0.141 0.005 285.823); | ||||||
|   --foreground: oklch(0.985 0.002 247.839); |   --foreground: oklch(0.985 0 0); | ||||||
|   --card: oklch(0.21 0.034 264.665); |   --card: oklch(0.21 0.006 285.885); | ||||||
|   --card-foreground: oklch(0.985 0.002 247.839); |   --card-foreground: oklch(0.985 0 0); | ||||||
|   --popover: oklch(0.21 0.034 264.665); |   --popover: oklch(0.21 0.006 285.885); | ||||||
|   --popover-foreground: oklch(0.985 0.002 247.839); |   --popover-foreground: oklch(0.985 0 0); | ||||||
|   --primary: oklch(0.928 0.006 264.531); |   --primary: oklch(0.705 0.213 47.604); | ||||||
|   --primary-foreground: oklch(0.21 0.034 264.665); |   --primary-foreground: oklch(0.98 0.016 73.684); | ||||||
|   --secondary: oklch(0.278 0.033 256.848); |   --secondary: oklch(0.274 0.006 286.033); | ||||||
|   --secondary-foreground: oklch(0.985 0.002 247.839); |   --secondary-foreground: oklch(0.985 0 0); | ||||||
|   --muted: oklch(0.278 0.033 256.848); |   --muted: oklch(0.274 0.006 286.033); | ||||||
|   --muted-foreground: oklch(0.707 0.022 261.325); |   --muted-foreground: oklch(0.705 0.015 286.067); | ||||||
|   --accent: oklch(0.278 0.033 256.848); |   --accent: oklch(0.274 0.006 286.033); | ||||||
|   --accent-foreground: oklch(0.985 0.002 247.839); |   --accent-foreground: oklch(0.985 0 0); | ||||||
|   --destructive: oklch(0.704 0.191 22.216); |   --destructive: oklch(0.704 0.191 22.216); | ||||||
|   --border: oklch(1 0 0 / 10%); |   --border: oklch(1 0 0 / 10%); | ||||||
|   --input: oklch(1 0 0 / 15%); |   --input: oklch(1 0 0 / 15%); | ||||||
|   --ring: oklch(0.551 0.027 264.364); |   --ring: oklch(0.408 0.123 38.172); | ||||||
|   --chart-1: oklch(0.488 0.243 264.376); |   --chart-1: oklch(0.837 0.128 66.29); | ||||||
|   --chart-2: oklch(0.696 0.17 162.48); |   --chart-2: oklch(0.705 0.213 47.604); | ||||||
|   --chart-3: oklch(0.769 0.188 70.08); |   --chart-3: oklch(0.646 0.222 41.116); | ||||||
|   --chart-4: oklch(0.627 0.265 303.9); |   --chart-4: oklch(0.553 0.195 38.402); | ||||||
|   --chart-5: oklch(0.645 0.246 16.439); |   --chart-5: oklch(0.47 0.157 37.304); | ||||||
|   --sidebar: oklch(0.21 0.034 264.665); |   --sidebar: oklch(0.21 0.006 285.885); | ||||||
|   --sidebar-foreground: oklch(0.985 0.002 247.839); |   --sidebar-foreground: oklch(0.985 0 0); | ||||||
|   --sidebar-primary: oklch(0.488 0.243 264.376); |   --sidebar-primary: oklch(0.705 0.213 47.604); | ||||||
|   --sidebar-primary-foreground: oklch(0.985 0.002 247.839); |   --sidebar-primary-foreground: oklch(0.98 0.016 73.684); | ||||||
|   --sidebar-accent: oklch(0.278 0.033 256.848); |   --sidebar-accent: oklch(0.274 0.006 286.033); | ||||||
|   --sidebar-accent-foreground: oklch(0.985 0.002 247.839); |   --sidebar-accent-foreground: oklch(0.985 0 0); | ||||||
|   --sidebar-border: oklch(1 0 0 / 10%); |   --sidebar-border: oklch(1 0 0 / 10%); | ||||||
|   --sidebar-ring: oklch(0.551 0.027 264.364); |   --sidebar-ring: oklch(0.408 0.123 38.172); | ||||||
| } | } | ||||||
|  |  | ||||||
| @layer base { | @layer base { | ||||||
|   | |||||||
| @@ -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'], | ||||||
| @@ -44,6 +54,21 @@ export default defineConfig({ | |||||||
|               } |               } | ||||||
|             } |             } | ||||||
|           }, |           }, | ||||||
|  |           { | ||||||
|  |             // Frankfurter API 汇率缓存 | ||||||
|  |             urlPattern: /^https:\/\/api\.frankfurter\.app\/.*/i, | ||||||
|  |             handler: 'StaleWhileRevalidate', | ||||||
|  |             options: { | ||||||
|  |               cacheName: 'currency-rates-cache', | ||||||
|  |               expiration: { | ||||||
|  |                 maxEntries: 5, | ||||||
|  |                 maxAgeSeconds: 60 * 60 * 12 // 12 小时 | ||||||
|  |               }, | ||||||
|  |               cacheableResponse: { | ||||||
|  |                 statuses: [0, 200] | ||||||
|  |               } | ||||||
|  |             } | ||||||
|  |           }, | ||||||
|           { |           { | ||||||
|             // Google Fonts 样式表缓存 |             // Google Fonts 样式表缓存 | ||||||
|             urlPattern: /^https:\/\/fonts\.googleapis\.com\/.*/i, |             urlPattern: /^https:\/\/fonts\.googleapis\.com\/.*/i, | ||||||
| @@ -130,4 +155,4 @@ export default defineConfig({ | |||||||
|     }, |     }, | ||||||
|     chunkSizeWarningLimit: 500, |     chunkSizeWarningLimit: 500, | ||||||
|   }, |   }, | ||||||
| }) | })) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user