Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: random hash for tailwind assets (#59)

* fix: random hash for tailwind assets

* chore: changeset

authored by

Erika and committed by
GitHub
371c3e39 d69679ae

+34
+5
.sampo/changesets/majestic-seer-vipunen.md
··· 1 + --- 2 + maudit: patch 3 + --- 4 + 5 + Makes it so styles using Tailwind always have a different hash between builds in order to avoid stale content
+29
crates/maudit/src/assets.rs
··· 346 346 } 347 347 HashAssetType::Style(opts) => { 348 348 buf.push(opts.tailwind as u8); 349 + 350 + if opts.tailwind { 351 + // If Tailwind is enabled for that style, we can't calculate the hash purely from the file content, as the final output will depend on the Tailwind configuration and the content of the entire project. So we'll add a random thing to the hash to ensure always a new hash. The way to fix this would be to calculate the hash based on the final output CSS after Tailwind processing, but that'd require holding a lot of pages in memory, transforming after we're done with bundling all assets, etc. Which: Does not feel good. Until a better solution, the hash changing on each build when Tailwind is involved will have to do. 352 + buf.extend_from_slice(&Instant::now().elapsed().as_nanos().to_le_bytes()); 353 + } 349 354 } 350 355 HashAssetType::Script => { /* No extra options for scripts yet */ } 351 356 } ··· 641 646 assert_ne!( 642 647 hash1, hash2, 643 648 "Different content should produce different hashes" 649 + ); 650 + } 651 + 652 + #[test] 653 + fn test_tailwind_hash_changes_every_time() { 654 + let temp_dir = setup_temp_dir(); 655 + let style_path = temp_dir.join("tailwind_style.css"); 656 + std::fs::write(&style_path, "body { background: blue; }").unwrap(); 657 + 658 + let mut page_assets = RouteAssets::new(&RouteAssetsOptions::default()); 659 + 660 + // Add the same tailwind style multiple times with small delays 661 + let style1 = 662 + page_assets.add_style_with_options(&style_path, StyleOptions { tailwind: true }); 663 + 664 + // Small delay to ensure different timestamp 665 + std::thread::sleep(std::time::Duration::from_millis(1)); 666 + 667 + let style2 = 668 + page_assets.add_style_with_options(&style_path, StyleOptions { tailwind: true }); 669 + 670 + assert_ne!( 671 + style1.hash, style2.hash, 672 + "Tailwind styles should produce different hashes due to time-based component" 644 673 ); 645 674 } 646 675 }