Skip to content

Commit 7acc7d9

Browse files
adhami3310masenf
andauthored
fix various issues with frontend_path (#5698)
* fix various issues with frontend_path * fix tests --------- Co-authored-by: Masen Furer <[email protected]>
1 parent 8c20566 commit 7acc7d9

File tree

9 files changed

+38
-21
lines changed

9 files changed

+38
-21
lines changed

reflex/.templates/jinja/web/vite.config.js.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ function alwaysUseReactDomServerNode() {
2525
}
2626

2727
export default defineConfig((config) => ({
28-
base: "{{base}}",
2928
plugins: [
3029
alwaysUseReactDomServerNode(),
3130
reactRouter(),
3231
safariCacheBustPlugin(),
3332
],
3433
build: {
34+
assetsDir: "{{base}}assets".slice(1),
3535
rollupOptions: {
3636
jsx: {},
3737
output: {

reflex/.templates/web/utils/state.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -969,17 +969,7 @@ export const useEventLoop = (
969969
useEffect(() => {
970970
if (!sentHydrate.current) {
971971
queueEvents(
972-
initial_events().map((e) => ({
973-
...e,
974-
router_data: {
975-
pathname: location.pathname,
976-
query: {
977-
...Object.fromEntries(searchParams.entries()),
978-
...params.current,
979-
},
980-
asPath: location.pathname + location.search,
981-
},
982-
})),
972+
initial_events(),
983973
socket,
984974
true,
985975
navigate,

reflex/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ def router(self) -> Callable[[str], str | None]:
862862
"""
863863
from reflex.route import get_router
864864

865-
return get_router(list(self._unevaluated_pages))
865+
return get_router(list(dict.fromkeys([*self._unevaluated_pages, *self._pages])))
866866

867867
def get_load_events(self, path: str) -> list[IndividualEventType[()]]:
868868
"""Get the load events for a route.

reflex/route.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from collections.abc import Callable
77

88
from reflex import constants
9+
from reflex.config import get_config
910

1011

1112
def verify_route_validity(route: str) -> None:
@@ -211,6 +212,9 @@ def get_route(path: str) -> str | None:
211212
Returns:
212213
The first matching route, or None if no match is found.
213214
"""
215+
config = get_config()
216+
if config.frontend_path:
217+
path = path.removeprefix(config.frontend_path)
214218
path = "/" + path.removeprefix("/").removesuffix("/")
215219
if path == "/index":
216220
path = "/"

reflex/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2491,7 +2491,7 @@ def on_load_internal(self) -> list[Event | EventSpec | event.EventCallback] | No
24912491
# Cache the app reference for subsequent calls.
24922492
if type(self)._app_ref is None:
24932493
type(self)._app_ref = app
2494-
load_events = app.get_load_events(self.router._page.path)
2494+
load_events = app.get_load_events(self.router.url.path)
24952495
if not load_events:
24962496
self.is_hydrated = True
24972497
return None # Fast path for navigation with no on_load events defined.

reflex/utils/build.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import os
66
import zipfile
7-
from pathlib import Path
7+
from pathlib import Path, PosixPath
88

99
from rich.progress import MofNCompleteColumn, Progress, TimeElapsedColumn
1010

1111
from reflex import constants
12+
from reflex.config import get_config
1213
from reflex.utils import console, js_runtimes, path_ops, prerequisites, processes
1314
from reflex.utils.exec import is_in_app_harness
1415

@@ -205,6 +206,19 @@ def build():
205206
wdir / constants.Dirs.STATIC / "404.html",
206207
)
207208

209+
config = get_config()
210+
211+
if frontend_path := config.frontend_path.strip("/"):
212+
frontend_path = PosixPath(frontend_path)
213+
first_part = frontend_path.parts[0]
214+
for child in list((wdir / constants.Dirs.STATIC).iterdir()):
215+
if child.is_dir() and child.name == first_part:
216+
continue
217+
path_ops.mv(
218+
child,
219+
wdir / constants.Dirs.STATIC / frontend_path / child.name,
220+
)
221+
208222

209223
def setup_frontend(
210224
root: Path,

reflex/utils/frontend_skeleton.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,12 @@ def update_react_router_config(prerender_routes: bool = False):
149149

150150

151151
def _update_react_router_config(config: Config, prerender_routes: bool = False):
152+
basename = "/" + (config.frontend_path or "").strip("/")
153+
if not basename.endswith("/"):
154+
basename += "/"
155+
152156
react_router_config = {
153-
"basename": "/" + (config.frontend_path or "").removeprefix("/"),
157+
"basename": basename,
154158
"future": {
155159
"unstable_optimizeDeps": True,
156160
},

tests/units/test_app.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,12 @@ def _event(name, val, **kwargs):
10611061
token=kwargs.pop("token", token),
10621062
name=name,
10631063
router_data=kwargs.pop(
1064-
"router_data", {"pathname": "/" + route, "query": {arg_name: val}}
1064+
"router_data",
1065+
{
1066+
"pathname": "/" + route,
1067+
"query": {arg_name: val},
1068+
"asPath": "/test/something",
1069+
},
10651070
),
10661071
payload=kwargs.pop("payload", {}),
10671072
**kwargs,

tests/units/test_prerequisites.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
frontend_path="/test",
4444
),
4545
False,
46-
'export default {"basename": "/test", "future": {"unstable_optimizeDeps": true}, "ssr": false};',
46+
'export default {"basename": "/test/", "future": {"unstable_optimizeDeps": true}, "ssr": false};',
4747
),
4848
(
4949
Config(
@@ -67,21 +67,21 @@ def test_update_react_router_config(config, export, expected_output):
6767
app_name="test",
6868
frontend_path="",
6969
),
70-
'base: "/",',
70+
'assetsDir: "/assets".slice(1),',
7171
),
7272
(
7373
Config(
7474
app_name="test",
7575
frontend_path="/test",
7676
),
77-
'base: "/test/",',
77+
'assetsDir: "/test/assets".slice(1),',
7878
),
7979
(
8080
Config(
8181
app_name="test",
8282
frontend_path="/test/",
8383
),
84-
'base: "/test/",',
84+
'assetsDir: "/test/assets".slice(1),',
8585
),
8686
],
8787
)

0 commit comments

Comments
 (0)