Fix SX history, OOB header swaps, cross-service nav components
- Always re-fetch on popstate (drop LRU cache) for fresh content on back/forward - Save/restore scroll position via pushState - Add id="root-header-child" to ~app-body so OOB swaps can target it - Fix OOB renderers: nest root-row inside root-header-child swap instead of separate OOB that clobbers it - Fix 3+ header rows dropped: wrap all headers in single fragment instead of concatenating outside (<> ...) - Strip <script data-components> from text/sx responses before renderToString - Fall back to location.assign for cross-origin pushState (SecurityError) - Move blog/sx/nav.sx to shared/sx/templates/ so all services have nav components Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -122,7 +122,7 @@
|
||||
this._advance(m[0].length);
|
||||
var raw = m[0].slice(1, -1);
|
||||
return raw.replace(/\\n/g, "\n").replace(/\\t/g, "\t")
|
||||
.replace(/\\"/g, '"').replace(/\\\\/g, "\\");
|
||||
.replace(/\\"/g, '"').replace(/\\[/]/g, "/").replace(/\\\\/g, "\\");
|
||||
}
|
||||
|
||||
// Keyword
|
||||
@@ -1115,8 +1115,14 @@
|
||||
}
|
||||
var open = "<" + tag + attrs.join("") + ">";
|
||||
if (VOID_ELEMENTS[tag]) return open;
|
||||
var isRawText = (tag === "script" || tag === "style");
|
||||
var inner = [];
|
||||
for (var ci = 0; ci < children.length; ci++) inner.push(renderStr(children[ci], env));
|
||||
for (var ci = 0; ci < children.length; ci++) {
|
||||
var child = children[ci];
|
||||
if (isRawText && typeof child === "string") inner.push(child);
|
||||
else if (isRawText && isSym(child)) inner.push(String(sxEval(child, env)));
|
||||
else inner.push(renderStr(child, env));
|
||||
}
|
||||
return open + inner.join("") + "</" + tag + ">";
|
||||
}
|
||||
|
||||
@@ -1413,7 +1419,7 @@
|
||||
var PROCESSED = "_sxBound";
|
||||
var VERBS = ["get", "post", "put", "delete", "patch"];
|
||||
var DEFAULT_SWAP = "outerHTML";
|
||||
var HISTORY_MAX = 20;
|
||||
|
||||
|
||||
function dispatch(el, name, detail) {
|
||||
var evt = new CustomEvent(name, { bubbles: true, cancelable: true, detail: detail || {} });
|
||||
@@ -1627,7 +1633,12 @@
|
||||
// Check for text/sx content type
|
||||
var ct = resp.headers.get("Content-Type") || "";
|
||||
if (ct.indexOf("text/sx") >= 0) {
|
||||
try { text = Sx.renderToString(text); }
|
||||
try {
|
||||
// Strip and load any <script type="text/sx" data-components> blocks
|
||||
text = text.replace(/<script[^>]*type="text\/sx"[^>]*data-components[^>]*>([\s\S]*?)<\/script>/gi,
|
||||
function (_, defs) { Sx.loadComponents(defs); return ""; });
|
||||
text = Sx.renderToString(text.trim());
|
||||
}
|
||||
catch (err) {
|
||||
console.error("sx.js render error:", err);
|
||||
return;
|
||||
@@ -1696,10 +1707,15 @@
|
||||
|
||||
// History
|
||||
var pushUrl = el.getAttribute("sx-push-url");
|
||||
if (pushUrl === "true") {
|
||||
history.pushState({ sxUrl: url }, "", url);
|
||||
} else if (pushUrl && pushUrl !== "false") {
|
||||
history.pushState({ sxUrl: pushUrl }, "", pushUrl);
|
||||
if (pushUrl === "true" || (pushUrl && pushUrl !== "false")) {
|
||||
var pushTarget = pushUrl === "true" ? url : pushUrl;
|
||||
try {
|
||||
history.pushState({ sxUrl: pushTarget, scrollY: window.scrollY }, "", pushTarget);
|
||||
} catch (e) {
|
||||
// Cross-origin pushState not allowed — full navigation
|
||||
location.assign(pushTarget);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
dispatch(el, "sx:afterSwap", { target: target });
|
||||
@@ -1905,39 +1921,12 @@
|
||||
|
||||
// ---- History manager --------------------------------------------------
|
||||
|
||||
var _historyCache = {};
|
||||
var _historyCacheKeys = [];
|
||||
|
||||
function _cacheCurrentPage() {
|
||||
var key = location.href;
|
||||
var main = document.getElementById("main-panel");
|
||||
if (!main) return;
|
||||
_historyCache[key] = main.innerHTML;
|
||||
// LRU eviction
|
||||
var idx = _historyCacheKeys.indexOf(key);
|
||||
if (idx >= 0) _historyCacheKeys.splice(idx, 1);
|
||||
_historyCacheKeys.push(key);
|
||||
while (_historyCacheKeys.length > HISTORY_MAX) {
|
||||
delete _historyCache[_historyCacheKeys.shift()];
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
window.addEventListener("popstate", function (e) {
|
||||
var url = location.href;
|
||||
// Try cache first
|
||||
if (_historyCache[url]) {
|
||||
var main = document.getElementById("main-panel");
|
||||
if (main) {
|
||||
main.innerHTML = _historyCache[url];
|
||||
Sx.processScripts(main);
|
||||
Sx.hydrate(main);
|
||||
SxEngine.process(main);
|
||||
dispatch(document.body, "sx:afterSettle", { target: main });
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Fetch fresh
|
||||
var main = document.getElementById("main-panel");
|
||||
if (!main) { location.reload(); return; }
|
||||
|
||||
var histOpts = {
|
||||
headers: { "SX-Request": "true", "SX-History-Restore": "true" }
|
||||
};
|
||||
@@ -1948,24 +1937,31 @@
|
||||
histOpts.credentials = "include";
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
fetch(url, histOpts).then(function (resp) {
|
||||
return resp.text();
|
||||
}).then(function (text) {
|
||||
var ct = "";
|
||||
// Response content-type is lost here, check for sx
|
||||
// Strip and load any <script type="text/sx" data-components> blocks
|
||||
var hadScript = false;
|
||||
text = text.replace(/<script[^>]*type="text\/sx"[^>]*data-components[^>]*>([\s\S]*?)<\/script>/gi,
|
||||
function (_, defs) { hadScript = true; Sx.loadComponents(defs); return ""; });
|
||||
if (hadScript) text = text.trim();
|
||||
if (text.charAt(0) === "(") {
|
||||
try { text = Sx.renderToString(text); } catch (e) { /* not sx */ }
|
||||
try { text = Sx.renderToString(text); } catch (e) {}
|
||||
}
|
||||
var parser = new DOMParser();
|
||||
var doc = parser.parseFromString(text, "text/html");
|
||||
var newMain = doc.getElementById("main-panel");
|
||||
var main = document.getElementById("main-panel");
|
||||
if (main && newMain) {
|
||||
if (newMain) {
|
||||
main.innerHTML = newMain.innerHTML;
|
||||
_activateScripts(main);
|
||||
Sx.processScripts(main);
|
||||
Sx.hydrate(main);
|
||||
SxEngine.process(main);
|
||||
dispatch(document.body, "sx:afterSettle", { target: main });
|
||||
window.scrollTo(0, e.state && e.state.scrollY || 0);
|
||||
} else {
|
||||
location.reload();
|
||||
}
|
||||
}).catch(function () {
|
||||
location.reload();
|
||||
@@ -2057,10 +2053,7 @@
|
||||
init();
|
||||
}
|
||||
|
||||
// Cache current page before navigation
|
||||
document.addEventListener("sx:beforeRequest", function () {
|
||||
if (typeof SxEngine._cacheCurrentPage === "function") SxEngine._cacheCurrentPage();
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user