Skip to content

Commit

Permalink
Add redirect resolver controlled by server (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
vldmr-k authored Aug 26, 2024
1 parent f29f70e commit c6a59c6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/*.adv.js
/node_modules/
/public/
/gostatic
15 changes: 14 additions & 1 deletion twinspark.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@
var headers = {'ts-title': xhr.getResponseHeader('ts-title'),
'ts-history': xhr.getResponseHeader('ts-history'),
'ts-swap': xhr.getResponseHeader('ts-swap'),
'ts-swap-push': xhr.getResponseHeader('ts-swap-push')};
'ts-swap-push': xhr.getResponseHeader('ts-swap-push'),
'ts-location': xhr.getResponseHeader('ts-location')};

return resolve({ok: xhr.status >= 200 && xhr.status <= 299,
status: xhr.status,
Expand Down Expand Up @@ -1328,6 +1329,13 @@
return swap(origins, replyParent, res);
}

// Handler redirect response, when response headers has params `ts-location`
// `targetUrl` - target url getting from server response headers `ts-location`
function redirectResponse(targetUrl, res) {
location.href = targetUrl;
return res;
}


/// Making request for fragments

Expand Down Expand Up @@ -1443,6 +1451,11 @@

if (res.ok) {
res = /** @type !Response */ (res);

if(res.headers['ts-location']) {
return redirectResponse(res.headers['ts-location'], res);
}

let redirected = (res.url &&
(res.url != new URL(fullurl, location.href).href));

Expand Down
1 change: 1 addition & 0 deletions www/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ title: API
<tr><td><code>ts-swap-push</code></td> <td>"Push" some HTML, <code>replace: selector to <= selector from</code></td></tr>
<tr><td><code>ts-history</code></td> <td>New browser history URL</td></tr>
<tr><td><code>ts-title</code></td> <td>New page title in case of history push</td></tr>
<tr><td><code>ts-location</code></td> <td>Redirect to target URL</td></tr>

</table>

Expand Down
28 changes: 28 additions & 0 deletions www/examples/155-redirect-from-server.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
id: server-redirect
title: Redirect from server
tags: ts-location
----
<p>
Redirect to target url, pass to response headers <code>ts-location</code> parameter.
</p>


<div class="card example">
<div class="card-header">
<h5 class="d-inline mr-2">Demo</h5>
<button class="btn btn-link btn-sm reset">Reset</button>
<button class="btn btn-link btn-sm source">View Source</button>
</div>

<div class="card-body" style="margin-bottom: 1.2rem" id="redirect-example">
<button class="btn" ts-req="/redirect" >
Click to redirect
</button>
</div>

<script>
XHRMock.get("/redirect",
{body: prev(),
headers: {'ts-location': 'https://github.com/piranha/twinspark-js'}});
</script>
</div>

0 comments on commit c6a59c6

Please sign in to comment.