diff --git a/datasette.yaml b/datasette.yaml index 57e8096..8b26215 100644 --- a/datasette.yaml +++ b/datasette.yaml @@ -144,3 +144,5 @@ databases: record_id extra_css_urls: - /static/styles.css +extra_js_urls: + - /static/fix-links.js diff --git a/static/fix-links.js b/static/fix-links.js new file mode 100644 index 0000000..cca86a8 --- /dev/null +++ b/static/fix-links.js @@ -0,0 +1,37 @@ +"use strict"; + +document.addEventListener("DOMContentLoaded", () => { + const production_base_url = new URL("https://backoffice.seattleflu.org/switchboard"); + + /* Fix broken links generated by Datasette when deployed at a subpath instead + * of the web root. See . + * This is kinda terrible, but at least it makes the links work. It seemed + * more palatable than configuring Apache (our reverse proxy) to rewrite all + * HTML from Datasette using mod_proxy_html. + * -trs, 2 July 2020 + */ + if (document.location.origin !== production_base_url.origin) { + console.debug(`fix-links: skipping page; origin ${document.location.origin} is not ${production_base_url.origin}`) + return; + } + + const broken_link = url => + url.origin !== production_base_url.origin + && url.origin.match(/localhost|127[.]0[.]0[.]1/); + + for (const anchor of document.querySelectorAll("a[href]")) { + if (broken_link(anchor)) { + console.debug(`fix-links: fixing ${anchor.href}`); + + anchor.protocol = production_base_url.protocol; + anchor.hostname = production_base_url.hostname; + anchor.port = production_base_url.port; + anchor.pathname = production_base_url.pathname + anchor.pathname; + + console.debug(`fix-links: fixed ${anchor.href}`); + } + else { + console.debug(`fix-links: skipping ${anchor.href}; origin is not localhost`); + } + } +});