https://kotlinlang.org logo
#javascript
Title
# javascript
c

Chrys

12/01/2023, 3:34 PM
I have 3 routes rendered in a browser router. the home is correctly rendered but localhost:8080/login and localhost:8080/dashboard both render Cannot GET /login ( or /dashboard )
Copy code
val appRouter = createBrowserRouter(
    arrayOf(
        jso {
            path = "/"
            element = Home.create()
        },
        jso {
            path = "login"
            element = Login.create()

        },
        jso {
            path = "dashboard"
            element = Dashboard.create()
        }
    )
)
Any hint here?
a

Artem Kobzar

12/01/2023, 5:18 PM
You should put
login
and
dashboard
into the
children
section of the
/
path like this: https://reactrouter.com/en/main/routers/create-browser-router
Copy code
val appRouter = createBrowserRouter(
    arrayOf(
        jso {
            path = "/"
            element = Home.create(),
            children = arrayOf(
                jso {
                    path = "login"
                    element = Login.create()
                },
                jso {
                    path = "dashboard"
                    element = Dashboard.create()
                }
            )
        },
    )
)
c

Chrys

12/02/2023, 11:18 AM
I still have Cannot GET /login when I enter http://localhost:8080/login in the browser. The kotlin wrapper bom version in use is 1.0.0-pre.623
a

Artem Kobzar

12/03/2023, 7:39 AM
@turansky ^^
t

turansky

12/03/2023, 10:41 AM
If you do it without server (like Ktor) - you have only 1 available page (
index.html
usually). For other pages you need redirect from server. If you don't want to use server - you can use createHashRouter (example).
FYI - In latest wrappers you can use
Component
property
Copy code
val appRouter = createBrowserRouter(
    arrayOf(
        jso {
            path = "/"
            Component = Home,
            children = arrayOf(
                jso {
                    path = "login"
                    Component = Login
                },
                jso {
                    path = "dashboard"
                    Component = Dashboard
                }
            )
        },
    )
)
c

Chrys

12/03/2023, 6:57 PM
Thanks for the hint @turansky, we were using hash router but were not fond of it. is there any example using ktor to achieve a complete routing using browserRouter? if not, I'd just like to understand how ktor helps there and how it solves the issue. Thanks in advance
t

turansky

12/03/2023, 7:10 PM
Ktor returns default page for all "page-like" requests. On client side router decide what to show at start. In your case only client knows about
login
and
dashboard
pages. For other pages you can register page
404
in router.
c

Chrys

12/04/2023, 11:17 AM
Thanks, @turansky. Cc @DGensert
2 Views