Update site content and structure, including localization adjustments for addresses, removal of unused files, and enhancements to the layout and styling for better user experience.
This commit is contained in:
133
src/utils/preload.ts
Normal file
133
src/utils/preload.ts
Normal file
@@ -0,0 +1,133 @@
|
||||
// Preload utility for improving perceived performance
|
||||
export function initPreloading() {
|
||||
// Track preloaded URLs to avoid duplicate requests
|
||||
const preloadedUrls = new Set<string>();
|
||||
|
||||
// Function to preload a URL
|
||||
function preloadUrl(url: string) {
|
||||
if (preloadedUrls.has(url)) return;
|
||||
|
||||
try {
|
||||
// Create a link element for preloading
|
||||
const link = document.createElement('link');
|
||||
link.rel = 'prefetch';
|
||||
link.href = url;
|
||||
link.as = 'document';
|
||||
|
||||
// Add to head
|
||||
document.head.appendChild(link);
|
||||
|
||||
// Mark as preloaded
|
||||
preloadedUrls.add(url);
|
||||
|
||||
console.log(`Preloaded: ${url}`);
|
||||
} catch (error) {
|
||||
console.warn(`Failed to preload ${url}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// Function to handle link hover
|
||||
function handleLinkHover(event: Event) {
|
||||
const target = event.target as HTMLElement;
|
||||
const link = target.closest('a');
|
||||
|
||||
if (!link) return;
|
||||
|
||||
const href = link.getAttribute('href');
|
||||
if (!href) return;
|
||||
|
||||
// Skip external links, anchors, and special protocols
|
||||
if (href.startsWith('http') || href.startsWith('mailto:') || href.startsWith('tel:') || href.startsWith('#')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert relative URLs to absolute
|
||||
let url: string;
|
||||
try {
|
||||
url = new URL(href, window.location.origin).href;
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
// Skip if it's the current page
|
||||
if (url === window.location.href) return;
|
||||
|
||||
// Preload with a small delay to avoid preloading on accidental hovers
|
||||
setTimeout(() => {
|
||||
preloadUrl(url);
|
||||
}, 100);
|
||||
}
|
||||
|
||||
// Add event listeners to all links
|
||||
function addPreloadListeners() {
|
||||
// Use event delegation for better performance
|
||||
document.addEventListener('mouseenter', handleLinkHover, {
|
||||
capture: true,
|
||||
passive: true
|
||||
});
|
||||
|
||||
// Also preload on touchstart for mobile devices
|
||||
document.addEventListener('touchstart', handleLinkHover, {
|
||||
capture: true,
|
||||
passive: true
|
||||
});
|
||||
}
|
||||
|
||||
// Initialize preloading
|
||||
addPreloadListeners();
|
||||
|
||||
// Re-add listeners when new content is loaded (for SPA-like behavior)
|
||||
const observer = new MutationObserver((mutations) => {
|
||||
mutations.forEach((mutation) => {
|
||||
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
|
||||
// New content added, ensure listeners are active
|
||||
addPreloadListeners();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
observer.observe(document.body, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
|
||||
return {
|
||||
preloadUrl,
|
||||
preloadedUrls
|
||||
};
|
||||
}
|
||||
|
||||
// Preload specific important pages immediately
|
||||
export function preloadCriticalPages() {
|
||||
const criticalPages = [
|
||||
'/en/',
|
||||
'/nl/',
|
||||
'/de/',
|
||||
'/fr/',
|
||||
'/en/about',
|
||||
'/nl/about',
|
||||
'/de/about',
|
||||
'/fr/about',
|
||||
'/en/contact',
|
||||
'/nl/contact',
|
||||
'/de/contact',
|
||||
'/fr/contact',
|
||||
'/en/blog',
|
||||
'/nl/blog',
|
||||
'/de/blog',
|
||||
'/fr/blog'
|
||||
];
|
||||
|
||||
criticalPages.forEach(page => {
|
||||
const url = new URL(page, window.location.origin).href;
|
||||
if (url !== window.location.href) {
|
||||
setTimeout(() => {
|
||||
const link = document.createElement('link');
|
||||
link.rel = 'prefetch';
|
||||
link.href = url;
|
||||
link.as = 'document';
|
||||
document.head.appendChild(link);
|
||||
}, 1000); // Delay to not interfere with initial page load
|
||||
}
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user