pub struct SpaMiddleware { /* private fields */ }Expand description
SPA static file middleware.
- Matches
/{filename}against the disk root, then against embedded files - Serves files with MIME types inferred by the host
- Falls back to
index.htmlfor unknown paths (SPA routing) - Handles GET and HEAD; other methods pass through silently
Implementations§
Source§impl SpaMiddleware
impl SpaMiddleware
Sourcepub fn new(source: impl Into<SpaSource>) -> SpaMiddleware
pub fn new(source: impl Into<SpaSource>) -> SpaMiddleware
Create a new SPA middleware with default index “index.html”.
The disk root is resolved relative to the current working directory.
If the directory doesn’t exist at that path, the middleware searches
upward through ancestor directories and their immediate subdirectories,
matching the strategy used by config::load_appsettings.
Sourcepub fn with_index(
source: impl Into<SpaSource>,
index: impl Into<String>,
) -> SpaMiddleware
pub fn with_index( source: impl Into<SpaSource>, index: impl Into<String>, ) -> SpaMiddleware
Create a new SPA middleware with a custom index file name.
Sourcepub fn from_source(source: SpaSource) -> SpaMiddleware
pub fn from_source(source: SpaSource) -> SpaMiddleware
Create a SPA middleware from an already-built source.
Sourcepub fn resolved_root(&self) -> &Path
pub fn resolved_root(&self) -> &Path
The disk directory actually consulted, after resolution.
Sourcepub fn embedded(&self) -> Option<&EmbeddedAssets>
pub fn embedded(&self) -> Option<&EmbeddedAssets>
The embedded files this middleware may serve, if any.
Present even when EMBED_ENV disabled them at runtime.
Source§impl SpaMiddleware
impl SpaMiddleware
Sourcepub async fn try_index_fallback(
&self,
ctx: &mut dyn IHttpContext,
) -> Result<bool, Error>
pub async fn try_index_fallback( &self, ctx: &mut dyn IHttpContext, ) -> Result<bool, Error>
Serve index.html for a client-side route that did not match the router.
Called by the host after routing misses, so:
- an API path (
/api/*) is never answered with the app shell; - a request that asks for data (
Accept: application/json,*/*, or noAccept) gets a proper 404 instead of200 text/html; - the fallback cannot leak its cache policy into a handler response, because no handler ran.
Returns true when the fallback was written into ctx.