Skip to content

Commit

Permalink
Fix relative URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
danpalmer committed Nov 5, 2023
1 parent c1b4101 commit 17286c3
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
23 changes: 12 additions & 11 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -3506,6 +3506,18 @@ return (function () {
selectOverride = xhr.getResponseHeader("HX-Reselect");
}

// if we need to save history, do so, before swapping so that relative resources have the correct base URL
if (historyUpdate.type) {
triggerEvent(getDocument().body, 'htmx:beforeHistoryUpdate', mergeObjects({ history: historyUpdate }, responseInfo));
if (historyUpdate.type === "push") {
pushUrlIntoHistory(historyUpdate.path);
triggerEvent(getDocument().body, 'htmx:pushedIntoHistory', {path: historyUpdate.path});
} else {
replaceUrlInHistory(historyUpdate.path);
triggerEvent(getDocument().body, 'htmx:replacedInHistory', {path: historyUpdate.path});
}
}

var settleInfo = makeSettleInfo(target);
selectAndSwap(swapSpec.swapStyle, target, elt, serverResponse, settleInfo, selectOverride);

Expand Down Expand Up @@ -3555,17 +3567,6 @@ return (function () {
triggerEvent(elt, 'htmx:afterSettle', responseInfo);
});

// if we need to save history, do so
if (historyUpdate.type) {
triggerEvent(getDocument().body, 'htmx:beforeHistoryUpdate', mergeObjects({ history: historyUpdate }, responseInfo));
if (historyUpdate.type === "push") {
pushUrlIntoHistory(historyUpdate.path);
triggerEvent(getDocument().body, 'htmx:pushedIntoHistory', {path: historyUpdate.path});
} else {
replaceUrlInHistory(historyUpdate.path);
triggerEvent(getDocument().body, 'htmx:replacedInHistory', {path: historyUpdate.path});
}
}
if (responseInfo.pathInfo.anchor) {
var anchorTarget = getDocument().getElementById(responseInfo.pathInfo.anchor);
if(anchorTarget) {
Expand Down
1 change: 0 additions & 1 deletion test/attributes/hx-boost.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,3 @@ describe("hx-boost attribute", function() {
});

});

10 changes: 10 additions & 0 deletions test/manual/hxboost_relative_resources/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<script type="application/javascript" src="../../../src/htmx.js"></script>
<title>hx-boost - Relative Resources Page 1</title>
</head>
<body style="padding:20px;font-family: sans-serif" hx-boost="true">
<a href="nested/page2.html">To Page 2</a>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions test/manual/hxboost_relative_resources/nested/page2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<script type="application/javascript" src="../../../../src/htmx.js"></script>
<title>hx-boost - Relative Resources Page 2</title>
</head>
<body style="padding:20px;font-family: sans-serif" hx-boost="true">
<a href="../index.html">Back To Page 1</a>
<p>Image should be displayed below</p>
<img src="img.png" />
</body>
</html>
6 changes: 6 additions & 0 deletions test/manual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ <h2>Functionality</h2>
<li><a href="history_style">History Style</a></li>
</ul>
</li>
<li>Boost Tests
<ul>
<li><a href="hxboost_relative_resources">Relative Resources</a></li>
<li><a href="hxboost_template_parsing">Template Parsing</a></li>
</ul>
</li>
</ul>
<h2>Perf</h2>
<li><a href="manual-perf.html">Manual Perf Test</a></li>
Expand Down

0 comments on commit 17286c3

Please sign in to comment.