config.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #![allow(non_snake_case)]
  2. #[cfg(not(target_arch = "wasm32"))]
  3. use crate::fs_cache::PathMapFn;
  4. use crate::IncrementalRenderer;
  5. use crate::memory_cache::InMemoryCache;
  6. use std::{
  7. path::{Path, PathBuf},
  8. time::Duration,
  9. };
  10. /// A configuration for the incremental renderer.
  11. #[derive(Clone)]
  12. pub struct IncrementalRendererConfig {
  13. static_dir: PathBuf,
  14. memory_cache_limit: usize,
  15. invalidate_after: Option<Duration>,
  16. clear_cache: bool,
  17. pre_render: bool,
  18. #[cfg(not(target_arch = "wasm32"))]
  19. map_path: Option<PathMapFn>,
  20. }
  21. impl Default for IncrementalRendererConfig {
  22. fn default() -> Self {
  23. Self::new()
  24. }
  25. }
  26. impl IncrementalRendererConfig {
  27. /// Create a new incremental renderer configuration.
  28. pub fn new() -> Self {
  29. Self {
  30. static_dir: PathBuf::from("./static"),
  31. memory_cache_limit: 10000,
  32. invalidate_after: None,
  33. clear_cache: true,
  34. pre_render: false,
  35. #[cfg(not(target_arch = "wasm32"))]
  36. map_path: None,
  37. }
  38. }
  39. /// Clear the cache on startup (default: true)
  40. pub fn clear_cache(mut self, clear_cache: bool) -> Self {
  41. self.clear_cache = clear_cache;
  42. self
  43. }
  44. /// Set a mapping from the route to the file path. This will override the default mapping configured with `static_dir`.
  45. /// The function should return the path to the folder to store the index.html file in.
  46. #[cfg(not(target_arch = "wasm32"))]
  47. pub fn map_path<F: Fn(&str) -> PathBuf + Send + Sync + 'static>(mut self, map_path: F) -> Self {
  48. self.map_path = Some(std::sync::Arc::new(map_path));
  49. self
  50. }
  51. /// Set the static directory.
  52. pub fn static_dir<P: AsRef<Path>>(mut self, static_dir: P) -> Self {
  53. self.static_dir = static_dir.as_ref().to_path_buf();
  54. self
  55. }
  56. /// Set the memory cache limit.
  57. pub const fn memory_cache_limit(mut self, memory_cache_limit: usize) -> Self {
  58. self.memory_cache_limit = memory_cache_limit;
  59. self
  60. }
  61. /// Set the invalidation time.
  62. pub fn invalidate_after(mut self, invalidate_after: Duration) -> Self {
  63. self.invalidate_after = Some(invalidate_after);
  64. self
  65. }
  66. /// Set whether to include hydration ids in the pre-rendered html.
  67. pub fn pre_render(mut self, pre_render: bool) -> Self {
  68. self.pre_render = pre_render;
  69. self
  70. }
  71. /// Build the incremental renderer.
  72. pub fn build(self) -> IncrementalRenderer {
  73. let mut renderer = IncrementalRenderer {
  74. #[cfg(not(target_arch = "wasm32"))]
  75. file_system_cache: crate::fs_cache::FileSystemCache::new(
  76. self.static_dir.clone(),
  77. self.map_path,
  78. self.invalidate_after,
  79. ),
  80. memory_cache: InMemoryCache::new(self.memory_cache_limit, self.invalidate_after),
  81. invalidate_after: self.invalidate_after,
  82. };
  83. if self.clear_cache {
  84. renderer.invalidate_all();
  85. }
  86. renderer
  87. }
  88. }