Docly Developers
Knowledge base Clarification By design

Links in markup an API returns do not get the mount path

In shortDocly adds the site's mount path to links that start with "/" in pages it renders, but not to markup an API script returns: HTML that comes back inside a JSON response and is inserted by the browser keeps "/..." as is, so its links and images break while the site is published under a path.
Applies to APIPublishing
  1. What you'll see

    A list or fragment you load from /API/... and insert with innerHTML works on the site's own domain, but its links and images give 404 when the site is published under a path such as /MySite/.

  2. What's actually happening

    When Docly renders a page (.hash, display templates), it rewrites href and src values that start with / so that they include the path the folder is published under. An API script's response is not rewritten: a string of markup in the JSON reaches the browser exactly as the script built it. Built from the same helpers as the page, the markup then points at the root of the domain instead of the site.

    On a site published at the root of its own domain the two are the same, so the fault only shows on a staging address or any other path.

  3. What to do

    Prefix the paths in the API script with request.sitepath before returning the markup:

    function mounted(html) {
        const root = String(request.sitepath || "/").replace(/\/?$/, "/");
        return root == "/" ? html : html.replace(/(\s(?:href|src))="\/(?!\/)/g, '$1="' + root);
    }
    return { html: mounted(listHtml(items)) };
    • Do it once, where the response is built, not in every helper: the helpers stay usable from pages, where Docly already adds the path.
    • The same applies to docly.redirect in an API script; see Convert an existing website to Docly.
    • Test the site under a path before it moves to its own domain.

Last reviewed 26 Oct 2026