Merge pull request #30 from martadams89/css_reverse_proxy_fix
Some checks failed
Docker Image Build and Push (Conditional Arch) / build (push) Has been cancelled

Css reverse proxy fix
This commit is contained in:
Chris 2025-04-26 22:54:38 +02:00 • committed by GitHub
commit e9554552fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -28,7 +28,42 @@
const currentProtocol = window.location.protocol;
const currentHost = window.location.host;
console.log("Page loading with protocol:", currentProtocol);
function fixResources() {
console.log("Fixing resource protocols for reverse proxy compatibility");
document.querySelectorAll('link[rel="stylesheet"]').forEach(function(link) {
const href = link.getAttribute('href');
if (href && href.startsWith('http:') && window.location.protocol === 'https:') {
const newHref = href.replace('http:', 'https:');
console.log(`Fixing stylesheet: ${href} → ${newHref}`);
link.setAttribute('href', newHref);
}
});
document.querySelectorAll('script[src]').forEach(function(script) {
const src = script.getAttribute('src');
if (src && src.startsWith('http:') && window.location.protocol === 'https:') {
const newSrc = src.replace('http:', 'https:');
console.log(`Fixing script: ${src} → ${newSrc}`);
script.setAttribute('src', newSrc);
}
});
document.querySelectorAll('link[rel="preconnect"]').forEach(function(link) {
const href = link.getAttribute('href');
if (href && href.startsWith('http:') && window.location.protocol === 'https:') {
const newHref = href.replace('http:', 'https:');
console.log(`Fixing preconnect: ${href} → ${newHref}`);
link.setAttribute('href', newHref);
}
});
}
fixResources();
document.addEventListener('DOMContentLoaded', fixResources);
const origFetch = window.fetch;
window.fetch = function(url, options) {
if (url && typeof url === 'string' && url.startsWith('http')) {
@ -44,10 +79,26 @@
}
return origFetch.call(this, url, options);
};
const baseTag = document.createElement('base');
baseTag.href = currentProtocol + '//' + currentHost + '/';
document.head.appendChild(baseTag);
const preconnectDomains = ['cdn.tailwindcss.com', 'rsms.me'];
preconnectDomains.forEach(domain => {
const existingPreconnect = Array.from(document.querySelectorAll('link[rel="preconnect"]'))
.find(link => link.href.includes(domain));
if (existingPreconnect) {
const href = existingPreconnect.getAttribute('href');
if (href && href.startsWith('http') &&
new URL(href).protocol !== currentProtocol) {
const newHref = currentProtocol + '//' + new URL(href).host;
console.log(`Updating preconnect: ${href} → ${newHref}`);
existingPreconnect.setAttribute('href', newHref);
}
}
});
})();
</script>