dioxus_isrg/
config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#![allow(non_snake_case)]

#[cfg(not(target_arch = "wasm32"))]
use crate::fs_cache::PathMapFn;

use crate::memory_cache::InMemoryCache;
use crate::IncrementalRenderer;

use std::{
    path::{Path, PathBuf},
    time::Duration,
};

/// A configuration for the incremental renderer.
#[derive(Clone)]
pub struct IncrementalRendererConfig {
    static_dir: PathBuf,
    memory_cache_limit: usize,
    invalidate_after: Option<Duration>,
    clear_cache: bool,
    pre_render: bool,

    #[cfg(not(target_arch = "wasm32"))]
    map_path: Option<PathMapFn>,
}

impl Default for IncrementalRendererConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl IncrementalRendererConfig {
    /// Create a new incremental renderer configuration.
    pub fn new() -> Self {
        Self {
            static_dir: PathBuf::from("./static"),
            memory_cache_limit: 10000,
            invalidate_after: None,
            clear_cache: true,
            pre_render: false,
            #[cfg(not(target_arch = "wasm32"))]
            map_path: None,
        }
    }

    /// Clear the cache on startup (default: true)
    pub fn clear_cache(mut self, clear_cache: bool) -> Self {
        self.clear_cache = clear_cache;
        self
    }

    /// Set a mapping from the route to the file path. This will override the default mapping configured with `static_dir`.
    /// The function should return the path to the folder to store the index.html file in.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn map_path<F: Fn(&str) -> PathBuf + Send + Sync + 'static>(mut self, map_path: F) -> Self {
        self.map_path = Some(std::sync::Arc::new(map_path));
        self
    }

    /// Set the static directory.
    pub fn static_dir<P: AsRef<Path>>(mut self, static_dir: P) -> Self {
        self.static_dir = static_dir.as_ref().to_path_buf();
        self
    }

    /// Set the memory cache limit.
    pub const fn memory_cache_limit(mut self, memory_cache_limit: usize) -> Self {
        self.memory_cache_limit = memory_cache_limit;
        self
    }

    /// Set the invalidation time.
    pub fn invalidate_after(mut self, invalidate_after: Duration) -> Self {
        self.invalidate_after = Some(invalidate_after);
        self
    }

    /// Set whether to include hydration ids in the pre-rendered html.
    pub fn pre_render(mut self, pre_render: bool) -> Self {
        self.pre_render = pre_render;
        self
    }

    /// Build the incremental renderer.
    pub fn build(self) -> IncrementalRenderer {
        let mut renderer = IncrementalRenderer {
            #[cfg(not(target_arch = "wasm32"))]
            file_system_cache: crate::fs_cache::FileSystemCache::new(
                self.static_dir.clone(),
                self.map_path,
                self.invalidate_after,
            ),
            memory_cache: InMemoryCache::new(self.memory_cache_limit, self.invalidate_after),
            invalidate_after: self.invalidate_after,
        };

        if self.clear_cache {
            renderer.invalidate_all();
        }

        renderer
    }
}