From 22296b6cf897a88eefc41be655d7f1ef4c36e1cf Mon Sep 17 00:00:00 2001 From: Franciszek Stachura Date: Mon, 5 Aug 2024 15:02:43 +0200 Subject: [PATCH] Fix incorrectly issued 301 redirects on latest for project roots This commit adds a small piece of Javascript code to force browsers to forget about incorrectly issued 301 redirects from "latest" versions. This code only runs for root project paths (i.e. `/linux/v6.10.2/source`, but not `/linux/v6.10.2/source/arch`) - other links with "latest" will remain broken for people who visited them (at least until they clear their cache). --- http/script.js | 19 +++++++++++++++++++ templates/layout.html | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/http/script.js b/http/script.js index 337bcb6..984fd8f 100644 --- a/http/script.js +++ b/http/script.js @@ -162,4 +162,23 @@ window.requestAnimationFrame(offsetAnchor) // in case of slow rendering very long pages. window.onload = function () { window.requestAnimationFrame(offsetAnchor) + + // fix incorrectly issued 301 redirect + // https://developer.mozilla.org/en-US/docs/Web/API/Request/cache + // https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect + // TODO: remove after 10.2024 + let path = location.pathname.split('/'); + if (path.length == 4) { + path[2] = 'latest'; + let newPath = path.join('/'); + fetch(newPath, { + cache: 'reload', + redirect: 'manual', + // this is to make sure that varnish will cache the response, + // by default fetch sends no-cache in both headers if cache='reload' + headers: {'Cache-Control': 'max-age=86400', 'Pragma': ''} + }) + .then(console.log) + .catch(console.error); + } } diff --git a/templates/layout.html b/templates/layout.html index af77b6e..670be27 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -44,7 +44,7 @@ powered by Elixir 2.2 - +