Module Webview_desktop.Locate_assetsSource

Locating on-disk assets.

Filesystem helpers for finding an application's HTML, CSS, JavaScript and images relative to the running executable, independently of the current working directory — so that a page loads the same whether the program was started by dune exec, from a Dock icon, or from another directory.

  let index =
    Filename.concat (Webview_desktop.Locate_assets.assets_dir "web")
      "index.html"
  in
  Webview.navigate w ("file://" ^ index)

There are two places those files can be, and which one depends on how the application was installed rather than on anything it can decide for itself. assets_dir finds them beside the binary, which is where a build tree stages them and where a .deb, a .dmg or a Windows installer puts them; in_opam_share finds them in <prefix>/share/<package>, which is where opam install puts them. An application that is distributed both ways asks the second and falls back on the first.

Sourceval exe_dir : unit -> string

Absolute path to the directory containing the running executable.

Sourceval assets_dir : string -> string

assets_dir path is the assets directory path names, resolved against exe_dir rather than against the current working directory.

path therefore says where the assets sit relative to the executable, and nothing is assumed about their name or depth:

  • assets_dir "web" — a web/ directory beside the binary, the usual layout;
  • assets_dir "../web" — a directory next to the one holding the binary, for instance when several executables share one set of assets;
  • assets_dir "/opt/myapp/web" — an absolute path, returned unchanged.

Resolving against the executable is what lets a build tree be copied or moved and keep working, and it is where a build puts the assets: both the files staged from your sources and any generated ones, such as a js_of_ocaml bundle, which exist nowhere else.

So the assets have to have been built. With dune that means dune build; dune exec <exe> on its own builds the executable and nothing beside it, and the page will not be found. Staging them is a matter of attaching a glob_files dependency on the directory to the all alias, next to the executable stanza.

The result is not normalised: it is exe_dir and path joined with Filename.concat, so "../web" keeps its ... Both the filesystem and the file:// URLs built from it resolve that on their own.

Sourceval in_opam_share : package:string -> string -> string option

in_opam_share ~package name is where an opam install of your own application put name: <prefix>/share/<package>/<name>, with <prefix> read off the running executable, which opam installed as <prefix>/bin/<exe>.

This is the second place an application's assets can be, and the reason assets_dir is not enough on its own. A dune project that installs its page with

  (install
   (package myapp)
   (section share)
   (files (web/index.html as web/index.html) ...))

gets its binary in <prefix>/bin and its page in <prefix>/share/myapp/web — two directories apart, where a build tree has them side by side. Taking the prefix from the executable rather than from a path baked in at build time is what lets a switch be moved or copied with both halves still finding each other.

The two cases together are usually one function:

  let data_path name =
    match Locate_assets.in_opam_share ~package:"myapp" name with
    | Some path -> path (* installed: <prefix>/share/myapp/<name> *)
    | None -> Locate_assets.assets_dir name (* beside the binary *)
  in
  let index = Filename.concat (data_path "web") "index.html" in
  Webview.navigate w ("file://" ^ index)

None means the executable is not in a directory named bin, and so was not installed into a prefix. That single test is what distinguishes an installed binary from every other layout an owebview application is distributed in — a dune build tree, a macOS .app bundle, the private directory a .deb or .rpm unpacks into, the directory a Windows installer writes — and in all of those the assets are beside the binary, which is assets_dir's case. The name is compared case-insensitively, for a prefix spelled Bin on a system that does not mind.

It is not restricted to opam, despite the name: dune install --prefix /usr/local produces the same two directories, and so does any package that follows the same convention.

Whether name exists is not tested, deliberately. A Some path that is not there is a broken install rather than a different layout, so the path is worth reporting as it is — a fallback would only send the user looking somewhere the file was never going to be. The result is not normalised, for the same reason as in assets_dir.