on master help sign in
ios: hash based routing

On ios due to sandboxing when loading the page from a file adapt the path to be based on the hash to avoid crashes.

commitd29fab5e71cb632e6e4024eaa1a1b5c41f1e20e3 authorluigy <luigy@outlook.com> dateThu 4 Oct 2018, 08:39 (8 years ago) parent69a3ef0
1 file changed+30−4
lib/route/src/Obelisk/Route/Frontend.hs+30−4
lib/route/src/Obelisk/Route/Frontend.hs+30−4
@@ -74,6 +74,10 @@ import Language.Javascript.JSaddle --TODO: Get rid of this - other platforms can
7474import Reflex.Dom.Core
7575import qualified GHCJS.DOM.Types as DOM
7676import Network.URI
77+#if defined(ios_HOST_OS)
78+import Data.Maybe (fromMaybe)
79+import qualified Data.List as L
80+#endif
7781
7882import Unsafe.Coerce
7983
@@ -263,7 +267,7 @@ runRouteViewT routeEncoder a = do
263267 -- is nonempty, but begins with a character that isn't '?'. Since we don't expect
264268 -- this ever to happen, we'll just handle it by failing completely with 'error'.
265269 route :: Dynamic t r
266− route = fmap (errorLeft . tryDecode theEncoder . (uriPath &&& uriQuery) . _historyItem_uri) historyState
270+ route = fmap (errorLeft . tryDecode theEncoder . (adaptedUriPath &&& uriQuery) . _historyItem_uri) historyState
267271 where
268272 errorLeft (Left e) = error (T.unpack e)
269273 errorLeft (Right x) = x
@@ -284,10 +288,32 @@ runRouteViewT routeEncoder a = do
284288 -- we can change this function later to accommodate.
285289 -- See: https://github.com/whatwg/html/issues/2174
286290 , _historyStateUpdate_title = ""
287− , _historyStateUpdate_uri = Just $ (_historyItem_uri currentHistoryState)
288− { uriPath = newPath
289− , uriQuery = newQuery
291+ , _historyStateUpdate_uri = Just $ setAdaptedUriPath newPath $ (_historyItem_uri currentHistoryState)
292+ { uriQuery = newQuery
290293 }
291294 }
292295 setState = attachWith f ((,) <$> current historyState <*> current route) changeState
293296 return result
297+
298+-- On ios due to sandboxing when loading the page from a file adapt the
299+-- path to be based on the hash.
300+
301+adaptedUriPath :: URI -> String
302+#if defined(ios_HOST_OS)
303+adaptedUriPath = hashToPath . uriFragment
304+
305+hashToPath :: String -> String
306+hashToPath = ('/' :) . fromMaybe "" . L.stripPrefix "#"
307+#else
308+adaptedUriPath = uriPath
309+#endif
310+
311+setAdaptedUriPath :: String -> URI -> URI
312+#if defined(ios_HOST_OS)
313+setAdaptedUriPath s u = u { uriFragment = pathToHash s }
314+
315+pathToHash :: String -> String
316+pathToHash = ('#' :) . fromMaybe "" . L.stripPrefix "/"
317+#else
318+setAdaptedUriPath s u = u { uriPath = s }
319+#endif