Respect `base_path` when pre-rendering static routes
This pull request fixes https://github.com/DioxusLabs/dioxus/issues/3942 by passing the base_path from the AppBundle to pre_render_static_routes and using it to construct the URL accordingly.
I would like to add a test harness to verify that the base_path is always respected. However, I'd appreciate some guidance on how best to integrate such tests if that's something you'd like to see.
While investigating this issue and testing the example app from https://github.com/DioxusLabs/dioxus/issues/3942 (https://github.com/Gisleburt/dioxus-base-path-issue), I also encountered two additional problems that might be unrelated:
Sometimes content is duplicated or even triplicated.
When a non-existent page is requested, the bundle throws an error but then returns a 200 status and renders the homepage without duplicated content.
The error:
thread '<unnamed>' panicked at /home/leon/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/dioxus-router-0.6.3/src/contexts/router.rs:243:17:
route's display implementation must be parsable by FromStr
2025-04-05T13:11:19.269111Z ERROR dioxus_core::any_props: Panic while rendering component `dioxus_router::components::outlet::Outlet<base_path_issue::Route>`: Any { .. }
2025-04-05T13:11:19.269118Z ERROR dioxus_core::scope_arena: Error while rendering component `dioxus_router::components::outlet::Outlet<base_path_issue::Route>`:
Encountered error: CapturedPanic
In scope: ScopeId(5)
Backtrace: disabled backtrace
Context:
Any feedback or guidance on my fix, the test harness for base_path, and whether you think the other two problems are related or if I should open new issues for them (I couldn't find any open Issues mentioning them) would be much appreciated.
The fallback route issue sounds like https://github.com/DioxusLabs/dioxus/issues/3644. I don't think it is related. Content doubling could be caused by a hydration issue caused by #3644
We use playwright to test regressions for some similar issues. You can add the playwright test to this folder. You will need to add a new crate with the base path, and a playwright js test file. The fullstack test might be a good starting point
I've added a Playwright test for serving an app with base_path.
However, I struggled adding a test for the bundling and SSG case. When I run the commented-out command in playwright.config.js manually, everything works as expected and I can visit the site at 127.0.0.1:8080/base-path. But when Playwright executes it, I always encounter the error: "Failed to parse static routes from the server." Do you have any ideas on why that might be happening?
Additionally, is there a way to specify the port? I tried using --port as with serve, but that didn't seem to work; using 8080 might not be ideal.
I've also added test-results to the .gitignore (please let me know if the intent was to publish them) and rebased the changes upon the main branch.
I've added a Playwright test for serving an app with
base_path.However, I struggled adding a test for the bundling and SSG case. When I run the commented-out command in
playwright.config.jsmanually, everything works as expected and I can visit the site at127.0.0.1:8080/base-path. But when Playwright executes it, I always encounter the error: "Failed to parse static routes from the server." Do you have any ideas on why that might be happening?
You might need to pull in the latest commits from main. https://github.com/DioxusLabs/dioxus/commit/12c6938346709a42079e23186d1d7e748cdddbbb#diff-62798fdae610a4cce6fb5d11419a6306a2381d67743e0ea2a50c1729470e8e6b fixes the post body the CLI sends to the server for SSG.
Additionally, is there a way to specify the port? I tried using
--portas withserve, but that didn't seem to work; using8080might not be ideal.
--port should work for ssg. It seems like it is working for me with the existing ssg playwright test command. What error did you get?
You might need to pull in the latest commits from main. https://github.com/DioxusLabs/dioxus/commit/12c6938346709a42079e23186d1d7e748cdddbbb#diff-62798fdae610a4cce6fb5d11419a6306a2381d67743e0ea2a50c1729470e8e6b fixes the post body the CLI sends to the server for SSG.
My branch is already up-to-date with main. I found that it actually works when I comment out the "web" test; therefore, it seems that port collisions are the problem.
--portshould work for ssg. It seems like it is working for me with the existing ssg playwright test command. What error did you get?
The --port option appears to only work with serve, not with bundle. Also, the server created by bundle doesn't seem to accept a --port option, right? Since the issue was specifically mentioned bundle, I wanted to create a test for that scenario. I could assign the "web" test a different port to free up port 9999.
I also noticed that running serve in SSG mode with a base_path fails ("Failed to parse static routes from the server.") when the --release flag is not set. So far I haven't been able to find the cause of this behavior in debug mode; any insight would be appreciated.
You might need to pull in the latest commits from main. 12c6938#diff-62798fdae610a4cce6fb5d11419a6306a2381d67743e0ea2a50c1729470e8e6b fixes the post body the CLI sends to the server for SSG.
My branch is already up-to-date with
main. I found that it actually works when I comment out the "web" test; therefore, it seems that port collisions are the problem.
--portshould work for ssg. It seems like it is working for me with the existing ssg playwright test command. What error did you get?The
--portoption appears to only work withserve, not withbundle. Also, the server created bybundledoesn't seem to accept a--portoption, right? Since the issue was specifically mentionedbundle, I wanted to create a test for that scenario. I could assign the "web" test a different port to free up port9999.
Oh, it looks like ssg does hardcode the port 9999 as the default here. You can set the PORT environment variable to override the internal SSG port. Or we could change the CLI to look for a random free port like we do for the normal backend proxy with this method if the default port is busy
I also noticed that running
servein SSG mode with abase_pathfails ("Failed to parse static routes from the server.") when the--releaseflag is not set. So far I haven't been able to find the cause of this behavior in debug mode; any insight would be appreciated.
I have seen that error with some older versions of the dioxus CLI. The default server function encoding changed which means if your CLI is older than the last couple of commits to main, it will try to send an empty post body. The new JSON server function encoding will reject that as invalid which causes the request to fail. On the latest commit in the main branch running ssg in debug mode with the playwright command works on my macbook: cargo run --package dioxus-cli --release -- serve --bin nested-suspense-ssg --force-sequential --platform web --ssg --addr "127.0.0.1" --open
I also noticed that running
servein SSG mode with abase_pathfails ("Failed to parse static routes from the server.") when the--releaseflag is not set. So far I haven't been able to find the cause of this behavior in debug mode; any insight would be appreciated.I have seen that error with some older versions of the dioxus CLI. The default server function encoding changed which means if your CLI is older than the last couple of commits to main, it will try to send an empty post body. The new JSON server function encoding will reject that as invalid which causes the request to fail. On the latest commit in the main branch running ssg in debug mode with the playwright command works on my macbook:
cargo run --package dioxus-cli --release -- serve --bin nested-suspense-ssg --force-sequential --platform web --ssg --addr "127.0.0.1" --open
Running nested-suspense-ssg also works for me. So I guess there must be something about SSG and base_path together that breaks things in debug mode.
I'm a bit short on time right now, but I'll look into it further and improve the port situation as soon as possible. If it's super urgent, feel free to keep working on it, or close this PR and let someone else try.
With #4111, #4276, and several other base_path- and bundle-related PRs merged, I decided to take another look.
Although my changes to pre_render.rs make base_path work with SSG in some cases, there seem to be additional underlying issues that cause problems in other scenarios.
Using this test project and Dioxus from this PR, I tried every combination of run/bundle, debug/release, and no-SSG/SSG (see the README). It appears that run with SSG does not work properly (issue with the Router?), and bundle in debug seems to just ignore the base_path.
EDIT: The issues remain the same with Dioxus 0.7.0