v5 safe
This commit is contained in:
@@ -4,42 +4,17 @@ import { useLocation } from 'react-router-dom'
|
||||
export default function useLayoutInit() {
|
||||
const { pathname } = useLocation()
|
||||
|
||||
// Re-initialize theme JS on route change
|
||||
useEffect(() => {
|
||||
let destroyed = false
|
||||
let rafId = null
|
||||
|
||||
function tryInit() {
|
||||
if (destroyed) return
|
||||
|
||||
// Apply data-bg-src to elements
|
||||
document.querySelectorAll('[data-bg-src]').forEach(el => {
|
||||
const src = el.getAttribute('data-bg-src')
|
||||
if (src && !el.classList.contains('background-image')) {
|
||||
el.style.backgroundImage = `url(${src})`
|
||||
el.classList.add('background-image')
|
||||
}
|
||||
})
|
||||
|
||||
// Check if jQuery and theme init function are ready
|
||||
if (typeof window.jQuery === 'function' && typeof window.rakar_content_load_scripts === 'function') {
|
||||
try {
|
||||
// Destroy ALL existing swiper instances to prevent duplicates
|
||||
document.querySelectorAll('.swiper').forEach(el => {
|
||||
if (el.swiper) {
|
||||
try { el.swiper.destroy(true, true) } catch (e) { console.warn('useLayoutInit: swiper destroy failed:', e) }
|
||||
}
|
||||
})
|
||||
|
||||
// Run theme initialization
|
||||
window.rakar_content_load_scripts()
|
||||
|
||||
window.__layoutReady = true
|
||||
window.dispatchEvent(new Event('layout-ready'))
|
||||
return true
|
||||
} catch (e) {
|
||||
console.warn('Theme init error:', e)
|
||||
}
|
||||
if (destroyed) return false
|
||||
if (typeof window.rakar_content_load_scripts === 'function') {
|
||||
window.rakar_content_load_scripts()
|
||||
window.__layoutReady = true
|
||||
window.dispatchEvent(new Event('layout-ready'))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user