Use ServeDir for fullstack dev static assets #4983
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Without this patch, simply instantiating the Jumpstart template in fullstack mode and making any hot-patch change results in a 404 on fetch and a client WASM panic.
This changes the fullstack server static asset routing so that, in debug builds, directories under the
publictree (including/wasm) are served viatower_http::services::ServeDirinstead of being eagerly expanded into a fixed set of routes at startup.Today
dioxus_server::DioxusRouterExt::serve_static_assetswalks thepublicdirectory once at launch viaserve_dir_cached, registering a separate route for every file it sees. That works fine for pre-baked assets, but it breaks down when dx/subsecond writes new files intopublicduring a dev session. In a fullstack web app, wasm hot patches are emitted as timestamped files likepublic/wasm/lib<name>-patch-<ts>.wasm; the CLI tells the browser to fetch/wasm/lib<name>-patch-<ts>.wasmimmediately, but the fullstack server never registered a route for that path, so the fetch returns 404 even though the file already exists on disk.Non-fullstack dev builds do not have this problem because the dx devserver serves
publicdirectly usingServeDir, which consults the filesystem at request time. Fullstack dev, by contrast, proxies asset requests to the inner server, and that server only knows about whatever was inpublicat the moment its router was built.This change narrows the gap by making
serve_static_assetsdynamic for directories in debug builds while preserving the existing behaviour elsewhere:cfg(debug_assertions)),serve_dir_cachednow usesServeDir::new(&path)when it encounters a directory. This means/wasm,/assets, and any other subdirectories are backed by a live directory listing, so new files like wasm patch modules and freshly emitted hashed CSS become immediately visible without restarting the fullstack server or rebuilding its router.serve_dir_cachedcontinues to recurse and register one route per discovered file, still usingServeFile::precompressed_br()andcache_response_foreverfor hashed, cache-busted filenames.This is a no-op for release builds, but in debug fullstack it's necessary for wasm hot patches and other dynamically-created assets that could 404 until the server happened to be restarted. It also aligns fullstack dev behaviour more closely with the plain web devserver, which already uses
ServeDirfor thepublictree.