1
- use app:: { get_error_pages, get_locales, get_routes , APP_ROUTE } ;
1
+ use app:: { get_error_pages, get_locales, get_templates_map , APP_ROOT } ;
2
2
use perseus:: router:: { RouteInfo , RouteVerdict } ;
3
- use perseus:: { app_shell, detect_locale, ClientTranslationsManager , DomNode } ;
3
+ use perseus:: shell:: get_render_cfg;
4
+ use perseus:: { app_shell, create_app_route, detect_locale, ClientTranslationsManager , DomNode } ;
4
5
use std:: cell:: RefCell ;
5
6
use std:: rc:: Rc ;
6
- use sycamore:: context:: { ContextProvider , ContextProviderProps } ;
7
7
use sycamore:: prelude:: { template, StateHandle } ;
8
8
use sycamore_router:: { HistoryIntegration , Router , RouterProps } ;
9
9
use wasm_bindgen:: { prelude:: wasm_bindgen, JsValue } ;
@@ -18,7 +18,7 @@ pub fn run() -> Result<(), JsValue> {
18
18
. unwrap ( )
19
19
. document ( )
20
20
. unwrap ( )
21
- . query_selector ( APP_ROUTE )
21
+ . query_selector ( APP_ROOT )
22
22
. unwrap ( )
23
23
. unwrap ( ) ;
24
24
@@ -27,42 +27,42 @@ pub fn run() -> Result<(), JsValue> {
27
27
Rc :: new ( RefCell :: new ( ClientTranslationsManager :: new ( & get_locales ( ) ) ) ) ;
28
28
// Get the error pages in an `Rc` so we aren't creating hundreds of them
29
29
let error_pages = Rc :: new ( get_error_pages ( ) ) ;
30
- // Get the routes in an `Rc` as well
31
- let routes = Rc :: new ( get_routes :: < DomNode > ( ) ) ;
30
+
31
+ // Create the router we'll use for this app, based on the user's app definition
32
+ create_app_route ! {
33
+ name => AppRoute ,
34
+ // The render configuration is injected verbatim into the HTML shell, so it certainly should be present
35
+ render_cfg => get_render_cfg( ) . expect( "render configuration invalid or not injected" ) ,
36
+ templates => get_templates_map( ) ,
37
+ locales => get_locales( )
38
+ }
32
39
33
40
sycamore:: render_to (
34
41
|| {
35
42
template ! {
36
- // We provide the routes in context (can't provide them directly because of Sycamore trait constraints)
37
- // BUG: context doesn't exist when link clicked first time, works second time...
38
- ContextProvider ( ContextProviderProps {
39
- value: Rc :: clone( & routes) ,
40
- children: || template! {
41
- Router ( RouterProps :: new( HistoryIntegration :: new( ) , move |route: StateHandle <RouteVerdict <DomNode >>| {
42
- match route. get( ) . as_ref( ) {
43
- // Perseus' custom routing system is tightly coupled to the template system, and returns exactly what we need for the app shell!
44
- RouteVerdict :: Found ( RouteInfo {
45
- path,
46
- template_fn,
47
- locale
48
- } ) => app_shell(
49
- path. clone( ) ,
50
- template_fn. clone( ) ,
51
- locale. clone( ) ,
52
- // We give the app shell a translations manager and let it get the `Rc<Translator>` itself (because it can do async safely)
53
- Rc :: clone( & translations_manager) ,
54
- Rc :: clone( & error_pages)
55
- ) ,
56
- // If the user is using i18n, then they'll want to detect the locale on any paths missing a locale
57
- // Those all go to the same system that redirects to the appropriate locale
58
- RouteVerdict :: LocaleDetection ( path) => detect_locale( path. clone( ) , get_locales( ) ) ,
59
- // We handle the 404 for the user for convenience
60
- // To get a translator here, we'd have to go async and dangerously check the URL
61
- RouteVerdict :: NotFound => get_error_pages( ) . get_template_for_page( "" , & 404 , "not found" , None ) ,
62
- }
63
- } ) )
43
+ Router ( RouterProps :: new( HistoryIntegration :: new( ) , move |route: StateHandle <AppRoute <DomNode >>| {
44
+ match & route. get( ) . as_ref( ) . 0 {
45
+ // Perseus' custom routing system is tightly coupled to the template system, and returns exactly what we need for the app shell!
46
+ RouteVerdict :: Found ( RouteInfo {
47
+ path,
48
+ template,
49
+ locale
50
+ } ) => app_shell(
51
+ path. clone( ) ,
52
+ template. clone( ) ,
53
+ locale. clone( ) ,
54
+ // We give the app shell a translations manager and let it get the `Rc<Translator>` itself (because it can do async safely)
55
+ Rc :: clone( & translations_manager) ,
56
+ Rc :: clone( & error_pages)
57
+ ) ,
58
+ // If the user is using i18n, then they'll want to detect the locale on any paths missing a locale
59
+ // Those all go to the same system that redirects to the appropriate locale
60
+ RouteVerdict :: LocaleDetection ( path) => detect_locale( path. clone( ) , get_locales( ) ) ,
61
+ // We handle the 404 for the user for convenience
62
+ // To get a translator here, we'd have to go async and dangerously check the URL
63
+ RouteVerdict :: NotFound => get_error_pages( ) . get_template_for_page( "" , & 404 , "not found" , None ) ,
64
64
}
65
- } )
65
+ } ) )
66
66
}
67
67
} ,
68
68
& root,
0 commit comments