fix(hosting): preview link rewriting prepends storefront base_url

CTA buttons in section macros use short paths like /contact which
resolve to site root instead of the storefront path. The preview
JS now detects short paths (not starting with /platforms/, /storefront/,
etc.) and prepends the store's base_url before adding _preview token.

Example: /contact → /platforms/hosting/storefront/batirenovation-strasbourg/contact?_preview=...

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-03 20:03:04 +02:00
parent 91fb4b0757
commit 297b8a8d5a

View File

@@ -221,20 +221,30 @@
</div>
<style>header { margin-top: 48px !important; }</style>
<script>
// Propagate preview token to all internal links
document.addEventListener('DOMContentLoaded', function() {
// Propagate preview token to all internal links (runs repeatedly to catch Alpine-rendered content)
(function() {
var params = new URLSearchParams(window.location.search);
var token = params.get('_preview');
if (!token) return;
document.querySelectorAll('a[href]').forEach(function(a) {
var href = a.getAttribute('href');
if (href && (href.startsWith('/') || href.startsWith(window.location.origin)) && !href.startsWith('//')) {
var baseUrl = '{{ base_url | default("/") }}'.replace(/\/$/, '');
function fixLinks() {
document.querySelectorAll('a[href]').forEach(function(a) {
if (a.dataset.previewFixed) return;
var href = a.getAttribute('href');
if (!href || href.startsWith('http') || href.startsWith('//') || href.startsWith('#') || href.startsWith('mailto:') || href.startsWith('tel:')) return;
// Short paths like /contact from CMS sections → prepend base_url
if (href.match(/^\/[a-z]/) && !href.startsWith('/platforms/') && !href.startsWith('/storefront/') && !href.startsWith('/api/') && !href.startsWith('/admin/') && !href.startsWith('/store/')) {
href = baseUrl + href;
}
var url = new URL(href, window.location.origin);
url.searchParams.set('_preview', token);
a.setAttribute('href', url.pathname + url.search);
}
});
});
a.dataset.previewFixed = '1';
});
}
document.addEventListener('DOMContentLoaded', fixLinks);
setInterval(fixLinks, 1000);
})();
</script>
{% endif %}