diff --git a/.gitignore b/.gitignore index 324cfb8..a770971 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dist/*.adv.js /node_modules/ /public/ +/gostatic \ No newline at end of file diff --git a/twinspark.js b/twinspark.js index 6e38b5e..d66a422 100644 --- a/twinspark.js +++ b/twinspark.js @@ -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, @@ -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 @@ -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)); diff --git a/www/api.md b/www/api.md index bcba88d..669530e 100644 --- a/www/api.md +++ b/www/api.md @@ -82,6 +82,7 @@ title: API ts-swap-push "Push" some HTML, replace: selector to <= selector from ts-history New browser history URL ts-title New page title in case of history push +ts-location Redirect to target URL diff --git a/www/examples/155-redirect-from-server.html b/www/examples/155-redirect-from-server.html new file mode 100644 index 0000000..1d3c235 --- /dev/null +++ b/www/examples/155-redirect-from-server.html @@ -0,0 +1,28 @@ +id: server-redirect +title: Redirect from server +tags: ts-location +---- +

+ Redirect to target url, pass to response headers ts-location parameter. +

+ + +
+
+
Demo
+ + +
+ +
+ +
+ + +
\ No newline at end of file