fix(ios): resolve iOS Safari WebKit SyntaxError: Unexpected token '{' via SWC package transpilation and dynamic loader

This commit is contained in:
2026-11-05 12:16:41 +05:30
parent 35a615f4ea
commit 583a1fe5a0
4 changed files with 273 additions and 243 deletions
+57 -43
View File
@@ -1,61 +1,72 @@
'use client';
import React, { useEffect } from 'react';
import Lenis from 'lenis';
interface SmoothScrollProviderProps {
children: React.ReactNode;
}
/**
* High-performance, lightweight Lenis Smooth Scrolling Provider.
* Synchronizes with display refresh rate (60-120fps) via requestAnimationFrame.
* Features:
* - Fluid exponential easing (Apple/Linear quality)
* - Native momentum & touch compatibility
* - Smooth scroll interception for internal anchor links (#our-tools, #recommended-apps, etc.)
* - Automatic offset handling for sticky floating navigation
* - Respects prefers-reduced-motion
* Universal Smooth Scrolling Provider.
* - On iOS / Android / Mobile touch devices: Keeps native hardware-accelerated 120Hz momentum
* scrolling intact, completely avoiding WebKit JS scroll conflicts and parser overhead.
* - On Desktop & Trackpads: Dynamically loads high-performance exponential easing Lenis engine.
* - Handles anchor link smooth scrolling safely across all devices.
*/
export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) {
useEffect(() => {
// Only run on client-side and non-reduced-motion environments
// Only run in client browser
if (typeof window === 'undefined') return;
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (prefersReducedMotion) return;
let lenis: Lenis | null = null;
let lenis: any = null;
let animId = 0;
let isDisposed = false;
try {
// Initialize ultra-lightweight Lenis instance
lenis = new Lenis({
duration: 1.15,
easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), // Silky exponential ease-out
orientation: 'vertical',
gestureOrientation: 'vertical',
smoothWheel: true,
syncTouch: false, // Preserves native 120Hz iOS/Android touch momentum without interference
wheelMultiplier: 0.95,
touchMultiplier: 1.0,
infinite: false,
});
// Detect mobile / touch devices: iOS (iPhone, iPad, iPod) and touch screens
const isTouchDevice =
'ontouchstart' in window ||
navigator.maxTouchPoints > 0 ||
(window.matchMedia && window.matchMedia('(pointer: coarse)').matches && !window.matchMedia('(pointer: fine)').matches);
// Expose lenis instance globally for programmatic scrolling
(window as any).__lenis = lenis;
const prefersReducedMotion =
window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Locked high-frequency RAF loop
const raf = (time: number) => {
lenis?.raf(time);
animId = requestAnimationFrame(raf);
};
animId = requestAnimationFrame(raf);
} catch {
// Fallback silently to native smooth scrolling
// Only load Lenis for non-touch desktop environments that haven't requested reduced motion
if (!isTouchDevice && !prefersReducedMotion) {
import('lenis')
.then(({ default: Lenis }) => {
if (isDisposed) return;
try {
lenis = new Lenis({
duration: 1.15,
easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
orientation: 'vertical',
gestureOrientation: 'vertical',
smoothWheel: true,
syncTouch: false,
wheelMultiplier: 0.95,
touchMultiplier: 1.0,
infinite: false,
});
(window as any).__lenis = lenis;
const raf = (time: number) => {
if (isDisposed) return;
lenis?.raf(time);
animId = requestAnimationFrame(raf);
};
animId = requestAnimationFrame(raf);
} catch {
// Graceful fallback to native scrolling
}
})
.catch(() => {
// Graceful fallback if dynamic import fails
});
}
// Intercept in-page anchor links for seamless animated scrolling (safe on text nodes on mobile)
// Universal anchor link handler (safe on text nodes and SVG elements across mobile & desktop)
const handleAnchorClick = (e: MouseEvent) => {
try {
const target = e.target as Node | null;
@@ -73,9 +84,9 @@ export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) {
const targetElement = document.querySelector(href);
if (targetElement) {
e.preventDefault();
if (lenis) {
if (lenis && typeof lenis.scrollTo === 'function') {
lenis.scrollTo(targetElement as HTMLElement, {
offset: -80, // Offset for floating glass navbar
offset: -80,
duration: 1.25,
easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
});
@@ -84,16 +95,19 @@ export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) {
}
}
} catch {
// Fallback gracefully to default navigation if querySelector fails
// Fallback gracefully
}
};
document.addEventListener('click', handleAnchorClick, { capture: true });
return () => {
cancelAnimationFrame(animId);
isDisposed = true;
if (animId) cancelAnimationFrame(animId);
document.removeEventListener('click', handleAnchorClick, { capture: true });
lenis?.destroy();
if (lenis && typeof lenis.destroy === 'function') {
lenis.destroy();
}
delete (window as any).__lenis;
};
}, []);