Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redirect resolver controlled by server #46

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading