Navigation without reload (useSpa)

When a domain has useSpa checked, internal links no longer reload the page: bspkSpa (in assets/js/bspk.js) fetches the target URL, compares it zone by zone and swaps only what changed. Navigation feels smoother for the visitor.

The server knows nothing about it: every URL is still served as a complete page. F5, shared links, crawlers and new tabs all keep working. The one thing to remember when you write front-end code: swapped content never goes through DOMContentLoaded or $(document).ready again. Initialise on the bspk:spa:fin event too.

When it is on

The server writes data-spa="1" on <body> (in bspk/tpl-page1.html and tpl-page2) only if all of these hold:

  • E_WebDomain.useSpa = True;
  • the user does not have BSPK_CAN_EDIT_PAGE: an editor always gets full page loads, because the dev panel keeps state tied to the page (tree, selection, undo/redo);
  • the page is not shown inside a panel iframe (?modal= in the URL).

bspkSpa.start() runs at load and does nothing without data-spa="1". To test the behaviour, stay logged out, or use an account that cannot edit the page.

Which clicks are intercepted

The browser handles the click itself (classic load) when:

  • a modifier key or a non-left button is used;
  • target is not _self;
  • the link has download or data-no-spa;
  • the link goes to another origin or a non-http(s) protocol;
  • it is an anchor on the current page;
  • the URL has a file extension (pdf, zip, csv, xls(x), doc(x), images, mp4, webm, txt, json);
  • the path starts with /bspk-file, /bspk-icons, /bweb/login, /bweb/logout or /bweb/call-action.

Put data-no-spa on any link that must reload the page: a page that relies on a full init you cannot replay, a link to another domain served by the same host…

What one navigation does

  1. The bspk:spa:debut {url} event is dispatched; the class bspk-spa-charge is added on <html> (thin progress bar and progress cursor, styles injected inline, not Tailwind).
  2. fetch(url) with the header X-Bspk-Spa: 1; a previous navigation still in flight is aborted.
  3. Falls back to a real navigation (location.href = …) when the response was redirected to another URL (expired session, language redirect), the status is not OK, the target page has no data-spa="1", or anything throws.
  4. The <head> is updated without being replaced: title, meta description / keywords / robots, og:*, canonical, hreflang links, <html lang>. Page stylesheets are added if missing, never removed.
  5. Zones are swapped:
    • every [data-spa-zone] (header, sidebar-left, sidebar-right, footer in the page shells) is replaced only if its HTML differs; a zone missing from the target is removed, a new one is inserted next to #mainContentParent;
    • #mainContentParent is always replaced;
    • the <body> attributes data-pageuuid, data-ws, data-ws-indicator, data-presence, data-can-edit, data-scope-page, data-scope-record and data-spa are copied from the target (presence follows the new page).
  6. history.pushState({bspkSpa: true}), then scroll to top.
  7. The bspk:spa:fin {url, zones} event is dispatched.

Back/forward (popstate) replays the navigation only for history entries carrying bspkSpa.

Events, and what each is for

EventdetailWhen
bspk:spa:debut{url}before the fetch
bspk:spa:avant-remplacement{zone}before a zone's innerHTML is replaced: tear down what you mounted in it
bspk:spa:apres-remplacement{zone}after the zone's inline <script>s were re-run; the URL is not updated yet
bspk:spa:fin{url, zones}after pushState: the place to (re)initialise

zones lists what changed: a zone name, contenu for #mainContentParent, +name for an inserted zone, -name for a removed one.

Use fin, not apres-remplacement, for anything that calls 4D: the server takes the page language from the Referer of /bweb/call-action, and the address only changes at pushState.

What is replayed for you

  • inline <script>s of swapped zones are re-created so they execute;
  • TomSelect instances inside a swapped zone are destroyed before the swap;
  • on bspk:spa:fin: listboxes of swapped zones (displayListbox, scroll handler), markListenSearchZones, checkLinkedFields, repaintOnResize, plus loadTomSelect and loadDropZone (in assets/js/base.js).

Not replayed: loadQrCodeScanner, which recreates every scanner without checking whether it already runs. A page whose QR scanner starts on load must be reached by a full navigation: put data-no-spa on the links to it.

Pitfalls

  • Your own init code runs once. A site script (a host /assets/js/*.js, a code block) that initialises on DOMContentLoaded or ready does nothing after an internal click. Subscribe to the end of navigation as well:
    function initMine(root) { /* … */ }
    document.addEventListener('DOMContentLoaded', () => initMine(document));
    document.addEventListener('bspk:spa:fin', () => initMine(document));
    Make the init idempotent: an unchanged zone (same header) is not swapped, so its elements are already initialised.
  • Inline scripts run again on every swap of their zone. Listeners added to document or window from such a script pile up: guard them with a flag on window.
  • Stylesheets accumulate: the CSS of every visited page stays in <head>. If two pages' stylesheets conflict, scope their rules to their page.
  • A 301/302 redirect ends the SPA navigation for that click: the browser really navigates to the final URL. This is expected.
  • Editors never see this behaviour: to reproduce a visitor's problem, log out.

See also