Skip to content
This repository was archived by the owner on Jul 19, 2022. It is now read-only.

Commit 882505c

Browse files
authored
Merge pull request #232 from unisonweb/fix-routing-with-base-path
Unison Local Fix: Can't open definitions
2 parents 076f39e + 36670e8 commit 882505c

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/Route.elm

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,45 @@ toRoute =
104104
oneOf [ b perspective, b definition ]
105105

106106

107+
{-| In environments like Unison Local, the UI is served with a base path
108+
109+
This means that a route to a definition might look like:
110+
111+
- "/:some-token/ui/latest/terms/base/List/map"
112+
(where "/:some-token/ui/" is the base path.)
113+
114+
The base path is determined outside of the Elm app using the <base> tag in the
115+
<head> section of the document. The Browser uses this tag to prefix all links.
116+
117+
The base path must end in a slash for links to work correctly, but our parser
118+
expects a path to starts with a slash. When parsing the URL we thus pre-process
119+
the path to strip the base path and ensure a slash prefix before we parse.
120+
121+
-}
107122
fromUrl : String -> Url -> Route
108123
fromUrl basePath url =
109124
let
110-
stripBasePath u =
125+
stripBasePath path =
111126
if basePath == "/" then
112-
u
127+
path
113128

114129
else
115-
{ u | path = String.replace basePath "" u.path }
130+
String.replace basePath "" path
131+
132+
ensureSlashPrefix path =
133+
if String.startsWith "/" path then
134+
path
135+
136+
else
137+
"/" ++ path
116138

117139
parse url_ =
118140
Result.withDefault (Perspective (ByCodebase Relative)) (Parser.run toRoute url_)
119141
in
120142
url
121-
|> stripBasePath
122143
|> .path
144+
|> stripBasePath
145+
|> ensureSlashPrefix
123146
|> parse
124147

125148

tests/RouteTests.elm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ fromUrlBasePath =
188188
\_ ->
189189
let
190190
url =
191-
mkUrl "/latest/terms/@abc123"
191+
mkUrl "/some-token/ui/latest/terms/@abc123"
192192

193193
basePath =
194194
"/some-token/ui/"

0 commit comments

Comments
 (0)