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
+1
View File
@@ -40,6 +40,7 @@ function getPermittedDevOrigins() {
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
reactStrictMode: true, reactStrictMode: true,
transpilePackages: ['lenis', 'three', 'framer-motion'],
allowedDevOrigins: getPermittedDevOrigins(), allowedDevOrigins: getPermittedDevOrigins(),
images: { images: {
unoptimized: true, unoptimized: true,
+209
View File
@@ -0,0 +1,209 @@
'use client';
import React, { useEffect, useRef } from 'react';
import * as THREE from 'three';
interface HeroCrystalCanvasProps {
className?: string;
}
/**
* Interactive 3D WebGL Crystal Prism Background Canvas
* - Features: Icosahedron optical crystal, dual orbital gimbal rings, floating starlight particles
* - IntersectionObserver pauses RAF loop when scrolled out of view (0% CPU/GPU offscreen)
* - Safe WebGL context loss handling and automatic resource disposal
*/
export function HeroCrystalCanvas({ className }: HeroCrystalCanvasProps) {
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const container = containerRef.current;
if (!container) return;
let width = container.clientWidth || 800;
let height = container.clientHeight || 800;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100);
camera.position.z = 4.8;
let renderer: THREE.WebGLRenderer | null = null;
try {
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true, powerPreference: 'high-performance' });
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
container.appendChild(renderer.domElement);
} catch {
return;
}
// Master Group for smooth coordinated rotation
const masterGroup = new THREE.Group();
scene.add(masterGroup);
// 1. Central Optical Translucent Crystal (Icosahedron, mobile GPU safe)
const crystalGeo = new THREE.IcosahedronGeometry(1.25, 0);
const crystalMat = new THREE.MeshStandardMaterial({
color: 0x3b82f6,
emissive: 0x1d4ed8,
emissiveIntensity: 0.12,
roughness: 0.18,
metalness: 0.1,
transparent: true,
opacity: 0.14,
});
const crystal = new THREE.Mesh(crystalGeo, crystalMat);
masterGroup.add(crystal);
// Wireframe Fine Lines for the Crystal
const wireGeo = new THREE.WireframeGeometry(crystalGeo);
const wireMat = new THREE.LineBasicMaterial({
color: 0x60a5fa,
transparent: true,
opacity: 0.22,
});
const wireframe = new THREE.LineSegments(wireGeo, wireMat);
crystal.add(wireframe);
// 2. Dual Gyro Gimbal Orbital Rings
// Ring 1 (Cyan orbital ring)
const ring1Geo = new THREE.TorusGeometry(2.15, 0.007, 16, 120);
const ring1Mat = new THREE.MeshBasicMaterial({
color: 0x38bdf8,
transparent: true,
opacity: 0.20,
});
const ring1 = new THREE.Mesh(ring1Geo, ring1Mat);
ring1.rotation.x = Math.PI / 3.2;
masterGroup.add(ring1);
// Ring 2 (Indigo tilted orbital ring)
const ring2Geo = new THREE.TorusGeometry(1.8, 0.007, 16, 100);
const ring2Mat = new THREE.MeshBasicMaterial({
color: 0x818cf8,
transparent: true,
opacity: 0.16,
});
const ring2 = new THREE.Mesh(ring2Geo, ring2Mat);
ring2.rotation.y = Math.PI / 4;
ring2.rotation.z = Math.PI / 6;
masterGroup.add(ring2);
// 3. Ambient Starlight Particles
const particleCount = 40;
const particleGeo = new THREE.BufferGeometry();
const posArray = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount * 3; i++) {
posArray[i] = (Math.random() - 0.5) * 7.5;
}
particleGeo.setAttribute('position', new THREE.BufferAttribute(posArray, 3));
const particleMat = new THREE.PointsMaterial({
size: 0.03,
color: 0x93c5fa,
transparent: true,
opacity: 0.35,
});
const particles = new THREE.Points(particleGeo, particleMat);
masterGroup.add(particles);
// 4. Studio Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 1.2);
scene.add(ambientLight);
const cyanLight = new THREE.PointLight(0x06b6d4, 2.2, 12);
cyanLight.position.set(3.5, 3, 3);
scene.add(cyanLight);
const indigoLight = new THREE.PointLight(0x6366f1, 1.8, 12);
indigoLight.position.set(-3.5, -2, 2.5);
scene.add(indigoLight);
let animId = 0;
const clock = new THREE.Clock();
let isVisible = true;
// IntersectionObserver to pause loop when scrolled out of view (0% CPU/GPU offscreen)
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
isVisible = entry.isIntersecting;
});
},
{ threshold: 0.05 }
);
observer.observe(container);
const animate = () => {
animId = requestAnimationFrame(animate);
if (!isVisible) return;
const elapsed = clock.getElapsedTime();
// Fluid crystal rotation
crystal.rotation.x = elapsed * 0.12;
crystal.rotation.y = elapsed * 0.16;
// Gimbal ring orbital movements
ring1.rotation.z = elapsed * 0.14;
ring1.rotation.x = Math.PI / 3.2 + Math.sin(elapsed * 0.25) * 0.08;
ring2.rotation.y = -elapsed * 0.11;
ring2.rotation.z = Math.PI / 6 + Math.cos(elapsed * 0.2) * 0.1;
// Particle constellation subtle drift
particles.rotation.y = elapsed * 0.03;
particles.rotation.x = elapsed * 0.02;
if (renderer) {
try {
renderer.render(scene, camera);
} catch {
cancelAnimationFrame(animId);
}
}
};
animate();
const canvas = renderer.domElement;
const handleContextLost = (e: Event) => {
e.preventDefault();
cancelAnimationFrame(animId);
};
canvas.addEventListener('webglcontextlost', handleContextLost, false);
const handleResize = () => {
if (!container || !renderer) return;
const w = container.clientWidth;
const h = container.clientHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
};
window.addEventListener('resize', handleResize, { passive: true });
return () => {
cancelAnimationFrame(animId);
observer.disconnect();
canvas.removeEventListener('webglcontextlost', handleContextLost);
window.removeEventListener('resize', handleResize);
if (renderer && renderer.domElement && container.contains(renderer.domElement)) {
container.removeChild(renderer.domElement);
}
crystalGeo.dispose();
crystalMat.dispose();
wireGeo.dispose();
wireMat.dispose();
ring1Geo.dispose();
ring1Mat.dispose();
ring2Geo.dispose();
ring2Mat.dispose();
particleGeo.dispose();
particleMat.dispose();
renderer?.dispose();
};
}, []);
return <div ref={containerRef} className={className} />;
}
+6 -200
View File
@@ -2,7 +2,12 @@
import React, { useState, useEffect, useRef } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import Link from 'next/link'; import Link from 'next/link';
import * as THREE from 'three'; import dynamic from 'next/dynamic';
const Hero3DCanvas = dynamic(
() => import('@/components/3d/HeroCrystalCanvas').then((mod) => mod.HeroCrystalCanvas),
{ ssr: false, loading: () => null }
);
import { import {
Sparkles, Sparkles,
ArrowRight, ArrowRight,
@@ -420,202 +425,3 @@ export function HeroSection({
</section> </section>
); );
} }
// -------------------------------------------------------------
// Interactive 3D WebGL Crystal Prism Background Canvas
// (Optimized with IntersectionObserver: 0% GPU when offscreen)
// -------------------------------------------------------------
function Hero3DCanvas({ className }: { className?: string }) {
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const container = containerRef.current;
if (!container) return;
let width = container.clientWidth || 800;
let height = container.clientHeight || 800;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100);
camera.position.z = 4.8;
let renderer: THREE.WebGLRenderer | null = null;
try {
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true, powerPreference: 'high-performance' });
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
container.appendChild(renderer.domElement);
} catch (e) {
return;
}
// Master Group for smooth coordinated rotation
const masterGroup = new THREE.Group();
scene.add(masterGroup);
// 1. Central Optical Translucent Crystal (Icosahedron, mobile GPU safe)
const crystalGeo = new THREE.IcosahedronGeometry(1.25, 0);
const crystalMat = new THREE.MeshStandardMaterial({
color: 0x3b82f6,
emissive: 0x1d4ed8,
emissiveIntensity: 0.12,
roughness: 0.18,
metalness: 0.1,
transparent: true,
opacity: 0.14,
});
const crystal = new THREE.Mesh(crystalGeo, crystalMat);
masterGroup.add(crystal);
// Wireframe Fine Lines for the Crystal
const wireGeo = new THREE.WireframeGeometry(crystalGeo);
const wireMat = new THREE.LineBasicMaterial({
color: 0x60a5fa,
transparent: true,
opacity: 0.22,
});
const wireframe = new THREE.LineSegments(wireGeo, wireMat);
crystal.add(wireframe);
// 2. Dual Gyro Gimbal Orbital Rings (Sleek Apple/Quantum Aesthetic)
// Ring 1 (Cyan orbital ring)
const ring1Geo = new THREE.TorusGeometry(2.15, 0.007, 16, 120);
const ring1Mat = new THREE.MeshBasicMaterial({
color: 0x38bdf8,
transparent: true,
opacity: 0.20,
});
const ring1 = new THREE.Mesh(ring1Geo, ring1Mat);
ring1.rotation.x = Math.PI / 3.2;
masterGroup.add(ring1);
// Ring 2 (Indigo tilted orbital ring)
const ring2Geo = new THREE.TorusGeometry(1.8, 0.007, 16, 100);
const ring2Mat = new THREE.MeshBasicMaterial({
color: 0x818cf8,
transparent: true,
opacity: 0.16,
});
const ring2 = new THREE.Mesh(ring2Geo, ring2Mat);
ring2.rotation.y = Math.PI / 4;
ring2.rotation.z = Math.PI / 6;
masterGroup.add(ring2);
// 3. Subtle Constellation of Ambient Starlight Particles
const particleCount = 40;
const particleGeo = new THREE.BufferGeometry();
const posArray = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount * 3; i++) {
posArray[i] = (Math.random() - 0.5) * 7.5;
}
particleGeo.setAttribute('position', new THREE.BufferAttribute(posArray, 3));
const particleMat = new THREE.PointsMaterial({
size: 0.03,
color: 0x93c5fa,
transparent: true,
opacity: 0.35,
});
const particles = new THREE.Points(particleGeo, particleMat);
masterGroup.add(particles);
// 4. Studio Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 1.2);
scene.add(ambientLight);
const cyanLight = new THREE.PointLight(0x06b6d4, 2.2, 12);
cyanLight.position.set(3.5, 3, 3);
scene.add(cyanLight);
const indigoLight = new THREE.PointLight(0x6366f1, 1.8, 12);
indigoLight.position.set(-3.5, -2, 2.5);
scene.add(indigoLight);
let animId = 0;
let clock = new THREE.Clock();
let isVisible = true;
// IntersectionObserver to pause loop when scrolled out of view (0% CPU/GPU offscreen)
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
isVisible = entry.isIntersecting;
});
},
{ threshold: 0.05 }
);
observer.observe(container);
const animate = () => {
animId = requestAnimationFrame(animate);
if (!isVisible) return;
const elapsed = clock.getElapsedTime();
// Fluid crystal rotation
crystal.rotation.x = elapsed * 0.12;
crystal.rotation.y = elapsed * 0.16;
// Gimbal ring orbital movements
ring1.rotation.z = elapsed * 0.14;
ring1.rotation.x = Math.PI / 3.2 + Math.sin(elapsed * 0.25) * 0.08;
ring2.rotation.y = -elapsed * 0.11;
ring2.rotation.z = Math.PI / 6 + Math.cos(elapsed * 0.2) * 0.1;
// Particle constellation subtle drift
particles.rotation.y = elapsed * 0.03;
particles.rotation.x = elapsed * 0.02;
if (renderer) {
try {
renderer.render(scene, camera);
} catch {
cancelAnimationFrame(animId);
}
}
};
animate();
const canvas = renderer.domElement;
const handleContextLost = (e: Event) => {
e.preventDefault();
cancelAnimationFrame(animId);
};
canvas.addEventListener('webglcontextlost', handleContextLost, false);
const handleResize = () => {
if (!container || !renderer) return;
const w = container.clientWidth;
const h = container.clientHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
};
window.addEventListener('resize', handleResize, { passive: true });
return () => {
cancelAnimationFrame(animId);
observer.disconnect();
canvas.removeEventListener('webglcontextlost', handleContextLost);
window.removeEventListener('resize', handleResize);
if (renderer && renderer.domElement && container.contains(renderer.domElement)) {
container.removeChild(renderer.domElement);
}
crystalGeo.dispose();
crystalMat.dispose();
wireGeo.dispose();
wireMat.dispose();
ring1Geo.dispose();
ring1Mat.dispose();
ring2Geo.dispose();
ring2Mat.dispose();
particleGeo.dispose();
particleMat.dispose();
renderer?.dispose();
};
}, []);
return <div ref={containerRef} className={className} />;
}
+40 -26
View File
@@ -1,61 +1,72 @@
'use client'; 'use client';
import React, { useEffect } from 'react'; import React, { useEffect } from 'react';
import Lenis from 'lenis';
interface SmoothScrollProviderProps { interface SmoothScrollProviderProps {
children: React.ReactNode; children: React.ReactNode;
} }
/** /**
* High-performance, lightweight Lenis Smooth Scrolling Provider. * Universal Smooth Scrolling Provider.
* Synchronizes with display refresh rate (60-120fps) via requestAnimationFrame. * - On iOS / Android / Mobile touch devices: Keeps native hardware-accelerated 120Hz momentum
* Features: * scrolling intact, completely avoiding WebKit JS scroll conflicts and parser overhead.
* - Fluid exponential easing (Apple/Linear quality) * - On Desktop & Trackpads: Dynamically loads high-performance exponential easing Lenis engine.
* - Native momentum & touch compatibility * - Handles anchor link smooth scrolling safely across all devices.
* - Smooth scroll interception for internal anchor links (#our-tools, #recommended-apps, etc.)
* - Automatic offset handling for sticky floating navigation
* - Respects prefers-reduced-motion
*/ */
export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) { export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) {
useEffect(() => { useEffect(() => {
// Only run on client-side and non-reduced-motion environments // Only run in client browser
if (typeof window === 'undefined') return; if (typeof window === 'undefined') return;
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; let lenis: any = null;
if (prefersReducedMotion) return;
let lenis: Lenis | null = null;
let animId = 0; let animId = 0;
let isDisposed = 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);
const prefersReducedMotion =
window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// 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 { try {
// Initialize ultra-lightweight Lenis instance
lenis = new Lenis({ lenis = new Lenis({
duration: 1.15, duration: 1.15,
easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), // Silky exponential ease-out easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
orientation: 'vertical', orientation: 'vertical',
gestureOrientation: 'vertical', gestureOrientation: 'vertical',
smoothWheel: true, smoothWheel: true,
syncTouch: false, // Preserves native 120Hz iOS/Android touch momentum without interference syncTouch: false,
wheelMultiplier: 0.95, wheelMultiplier: 0.95,
touchMultiplier: 1.0, touchMultiplier: 1.0,
infinite: false, infinite: false,
}); });
// Expose lenis instance globally for programmatic scrolling
(window as any).__lenis = lenis; (window as any).__lenis = lenis;
// Locked high-frequency RAF loop
const raf = (time: number) => { const raf = (time: number) => {
if (isDisposed) return;
lenis?.raf(time); lenis?.raf(time);
animId = requestAnimationFrame(raf); animId = requestAnimationFrame(raf);
}; };
animId = requestAnimationFrame(raf); animId = requestAnimationFrame(raf);
} catch { } catch {
// Fallback silently to native smooth scrolling // 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) => { const handleAnchorClick = (e: MouseEvent) => {
try { try {
const target = e.target as Node | null; const target = e.target as Node | null;
@@ -73,9 +84,9 @@ export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) {
const targetElement = document.querySelector(href); const targetElement = document.querySelector(href);
if (targetElement) { if (targetElement) {
e.preventDefault(); e.preventDefault();
if (lenis) { if (lenis && typeof lenis.scrollTo === 'function') {
lenis.scrollTo(targetElement as HTMLElement, { lenis.scrollTo(targetElement as HTMLElement, {
offset: -80, // Offset for floating glass navbar offset: -80,
duration: 1.25, duration: 1.25,
easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), easing: (t: number) => Math.min(1, 1.001 - Math.pow(2, -10 * t)),
}); });
@@ -84,16 +95,19 @@ export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) {
} }
} }
} catch { } catch {
// Fallback gracefully to default navigation if querySelector fails // Fallback gracefully
} }
}; };
document.addEventListener('click', handleAnchorClick, { capture: true }); document.addEventListener('click', handleAnchorClick, { capture: true });
return () => { return () => {
cancelAnimationFrame(animId); isDisposed = true;
if (animId) cancelAnimationFrame(animId);
document.removeEventListener('click', handleAnchorClick, { capture: true }); document.removeEventListener('click', handleAnchorClick, { capture: true });
lenis?.destroy(); if (lenis && typeof lenis.destroy === 'function') {
lenis.destroy();
}
delete (window as any).__lenis; delete (window as any).__lenis;
}; };
}, []); }, []);