MithrilJS / mithril.js

A JavaScript Framework for Building Brilliant Applications
https://mithril.js.org
MIT License
13.98k stars 925 forks source link

Mithril not respecting base href #2377

Closed volnei closed 3 days ago

volnei commented 5 years ago

Mithril Version: 2.0.0-rc3

2.0.0-rc3

Im developing a multi language app using mithril where witch language have your directory. Eg.: /en/ /pt/ etc.

When running the application using Nginx and following http://localhost/pt/ the navigator is redirected to http://localhost.

The html file is:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge; chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">

        <title>...</title>

        <base href="/pt/"/>

        <link rel="icon" href="/img/favicon.ico">
        <meta name="theme-color" content="#E64545">
        <link href="https://fonts.googleapis.com/css?family=Montserrat:100,300,400,500,600" rel="stylesheet">
    <link href="main.d819cfc3a0cfab094b29.css" rel="stylesheet"></head>
    <body>
        <main id="main"></main>
    <script type="text/javascript" src="92f025ccf65eb904ad80.bundle.js"></script><script type="text/javascript" src="main.64bdb34fc2a6a3597db4.js"></script></body>
</html>

And the mithril config is:

m.route.prefix("")
m.route(document.querySelector("#main"), "/", {
    "/": layout(site),
    "/login": layout(login)
}

But in browser, the lang part for url is not respected and its automatically redirected to /.

Can anyone help me?

Thanks

barneycarroll commented 5 years ago

Hi @volnei! When using non-prefixed route mode, you will need to include the entire path for each route. If you want to prefix your Mithril routes with whatever path the page resolved on, you can access the current path with location.pathname.

volnei commented 5 years ago

@barneycarroll thanks for your quick response!! So, I need to map and reference the /lang part in any of my routes and links?

Like this?

m.route.prefix("")
m.route(document.querySelector("#main"), "/en/", {
    "/en/": layout(site),
    "/en/login": layout(login)
}

Theres no way to make it more generic?

barneycarroll commented 5 years ago

I think so. I thought for a moment it would be possible to put the language prefix as m.route.prefix('/en'), but apparently not… 😕

volnei commented 5 years ago

Yes, I've tried this a minute ago, but not work too. Would not it be correct for mithril itself to be based on the href in base directive? React for example have a base path where we can configure this. Any change to make this topic a feature request?

ChrisGitIt commented 5 years ago

Hi @volnei ,

i'm not sure what you are going to achieve, but can't you use just language placeholders and drop the whole "base href"? So

m.route(doc_root, "/", {
  "/:lang/": ..
  "/:lang/login": ...
})

Within your app, you will get vnode.attrs.lang as an interpolated value from ":lang" definition within the routes.

dead-claudia commented 3 days ago

Closing due to age.