From 5ca6a75ff7c635f17227770eb9aa5fcbebc705c2 Mon Sep 17 00:00:00 2001 From: vldmrk Date: Thu, 15 Aug 2024 19:59:04 +0100 Subject: [PATCH 1/2] Add redirect controlled from server --- .gitignore | 1 + Makefile | 2 +- twinspark.js | 15 +++++++++++- www/api.md | 1 + www/examples/155-redirect-from-server.html | 28 ++++++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 www/examples/155-redirect-from-server.html 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/Makefile b/Makefile index 3966605..cbad815 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ serve: darkhttpd . --port 3000 --addr 127.0.0.1 w: - gostatic -w gostatic.conf + ./gostatic -w gostatic.conf test: CHROMIUM_BIN=$(which chrome) ./headless-tests.js public diff --git a/twinspark.js b/twinspark.js index c86eadf..e0ef928 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 @@ -1432,6 +1440,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 9b5c4ec..fea9da9 100644 --- a/www/api.md +++ b/www/api.md @@ -81,6 +81,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 From ace2617f80cf3b72ef91c49a808700204aee8773 Mon Sep 17 00:00:00 2001 From: vldmrk Date: Fri, 23 Aug 2024 18:55:56 +0100 Subject: [PATCH 2/2] Revert Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cbad815..3966605 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ serve: darkhttpd . --port 3000 --addr 127.0.0.1 w: - ./gostatic -w gostatic.conf + gostatic -w gostatic.conf test: CHROMIUM_BIN=$(which chrome) ./headless-tests.js public