Skip to content

mobile resources on ios not loaded by obelisk's static when using a staticFiles derivation #1135

Description

@o1lo01ol1o

obelisk's static did not handle our staticFiles derivation in nix on ios builds. To fix this, I wrote the following stub. However, the in-module CPP splice breaks HLS when imported and so it should probably be factored into conditional source files.

I've reproduced the inline version here so it can be incorporated upstream without our domain specific cruft.

import Common.FrontendSite (FrontendSite (..), toFrontendSite)
import Control.Monad.IO.Class (liftIO)
import qualified Data.Text as T
import Language.Haskell.TH (Exp, Q)
import Obelisk.Generated.Static (static)

#if !defined(ghcjs_HOST_OS) && !defined(linux_HOST_OS)
import Language.Javascript.JSaddle.WKWebView (mainBundleResourcePath)
import qualified Data.ByteString.Char8 as BS8
#endif

-- | Wrap Obelisk's 'static' to produce a path adjusted for mobile builds.
--
-- On iOS/Android (mobile builds) paths must not be absolute (leading '/'),
-- otherwise WKWebView/Android WebView may fail to resolve bundled assets.
-- We detect mobile via 'Environment' (Stage ∈ {Staging, Prod} with explicit
-- Host) and drop any leading '/'. Web builds keep absolute paths.
staticWithEnvJSM :: FilePath -> Q Exp
staticWithEnvJSM fp =
  [|
    \env -> do
      let p = $(static fp) :: T.Text
      case toFrontendSite env of
        OnMobile _ _ -> do
          let p0 = T.dropWhile (== '/') p
              p1 = if T.any (== '/') p0 then p0 else ("static/" <> p0)
#if !defined(ghcjs_HOST_OS) && !defined(linux_HOST_OS)
          mbr <- liftIO mainBundleResourcePath
          let br = maybe "" (\bs -> T.pack ("file://" <> BS8.unpack bs <> "/")) mbr
          pure (br <> p1)
#else
          pure p1
#endif
        OnWeb _ -> pure p
    |]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions