fix(client): eliminate ssr: false in Client Component and add safe WebGL exception handling to prevent client-side exception

This commit is contained in:
2026-11-03 13:46:51 +05:30
parent 8590608713
commit ea722c09be
3 changed files with 243 additions and 219 deletions
+234 -207
View File
@@ -1,6 +1,6 @@
'use client';
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import * as THREE from 'three';
interface Hero3DCanvasProps {
@@ -8,241 +8,268 @@ interface Hero3DCanvasProps {
}
export function Hero3DCanvas({ className = '' }: Hero3DCanvasProps) {
const [mounted, setMounted] = useState<boolean>(false);
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
setMounted(true);
}, []);
useEffect(() => {
if (!mounted) return;
const container = containerRef.current;
if (!container) return;
// Check prefers-reduced-motion
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Check prefers-reduced-motion safely
let prefersReducedMotion = false;
try {
prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
} catch {
prefersReducedMotion = false;
}
// Dimensions
let width = container.clientWidth || 340;
let height = container.clientHeight || 340;
// Scene, Camera, WebGLRenderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100);
camera.position.z = 6.2;
let renderer: THREE.WebGLRenderer | null = null;
let animFrameId: number = 0;
let observer: IntersectionObserver | null = null;
const renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
powerPreference: 'high-performance',
});
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
container.appendChild(renderer.domElement);
// Geometries & Materials for cleanup
let crystalGeo: THREE.OctahedronGeometry | null = null;
let crystalMat: THREE.MeshPhysicalMaterial | null = null;
let edgesGeo: THREE.EdgesGeometry | null = null;
let edgesMat: THREE.LineBasicMaterial | null = null;
let innerGeo: THREE.OctahedronGeometry | null = null;
let innerMat: THREE.MeshPhysicalMaterial | null = null;
let dotGeo: THREE.BufferGeometry | null = null;
let dotMat: THREE.PointsMaterial | null = null;
let particlesGeo: THREE.BufferGeometry | null = null;
let particlesMat: THREE.PointsMaterial | null = null;
// Root Group for Mouse Parallax
const rootGroup = new THREE.Group();
scene.add(rootGroup);
try {
// Scene, Camera, WebGLRenderer with Safe Error Handling
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100);
camera.position.z = 6.2;
// 1. Outer Translucent Diamond Crystal Prism (Octahedron)
const crystalGeo = new THREE.OctahedronGeometry(1.8, 0);
const crystalMat = new THREE.MeshPhysicalMaterial({
color: 0x3b82f6, // Electric Blue
roughness: 0.1,
metalness: 0.1,
transmission: 0.7, // Glass-like transparency
opacity: 0.65,
transparent: true,
emissive: 0x1d4ed8,
emissiveIntensity: 0.2,
});
const crystalMesh = new THREE.Mesh(crystalGeo, crystalMat);
rootGroup.add(crystalMesh);
// Subtle Glowing Edge Highlights on Crystal
const edgesGeo = new THREE.EdgesGeometry(crystalGeo);
const edgesMat = new THREE.LineBasicMaterial({
color: 0x60a5fa,
transparent: true,
opacity: 0.8,
});
const edgesLines = new THREE.LineSegments(edgesGeo, edgesMat);
rootGroup.add(edgesLines);
// 2. Inner Nested Core Prism (Counter-rotating)
const innerGeo = new THREE.OctahedronGeometry(1.0, 0);
const innerMat = new THREE.MeshPhysicalMaterial({
color: 0x6366f1, // Indigo
roughness: 0.2,
metalness: 0.8,
emissive: 0x4338ca,
emissiveIntensity: 0.5,
transparent: true,
opacity: 0.8,
});
const innerMesh = new THREE.Mesh(innerGeo, innerMat);
rootGroup.add(innerMesh);
// Vertex Spark Points
const vertexPositions = crystalGeo.attributes.position;
const dotGeo = new THREE.BufferGeometry();
dotGeo.setAttribute('position', vertexPositions);
const dotMat = new THREE.PointsMaterial({
color: 0x93c5fd,
size: 0.14,
transparent: true,
opacity: 0.9,
blending: THREE.AdditiveBlending,
});
const vertexPoints = new THREE.Points(dotGeo, dotMat);
rootGroup.add(vertexPoints);
// 3. Delicate Floating Neural Particles (35 Points)
const particleCount = prefersReducedMotion ? 12 : 36;
const particlePositions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount; i++) {
const radius = 2.2 + Math.random() * 1.4;
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos(2 * Math.random() - 1);
particlePositions[i * 3] = radius * Math.sin(phi) * Math.cos(theta);
particlePositions[i * 3 + 1] = radius * Math.sin(phi) * Math.sin(theta);
particlePositions[i * 3 + 2] = radius * Math.cos(phi);
}
const particlesGeo = new THREE.BufferGeometry();
particlesGeo.setAttribute('position', new THREE.BufferAttribute(particlePositions, 3));
const particlesMat = new THREE.PointsMaterial({
color: 0xa5b4fc,
size: 0.08,
transparent: true,
opacity: 0.7,
blending: THREE.AdditiveBlending,
});
const particlesMesh = new THREE.Points(particlesGeo, particlesMat);
rootGroup.add(particlesMesh);
// Lights
const ambientLight = new THREE.AmbientLight(0xffffff, 0.9);
scene.add(ambientLight);
const keyLight = new THREE.PointLight(0x38bdf8, 3, 10);
keyLight.position.set(3, 3, 3);
scene.add(keyLight);
const fillLight = new THREE.PointLight(0xa855f7, 2, 10);
fillLight.position.set(-3, -2, 2);
scene.add(fillLight);
// Smooth Mouse Tracking with Damped Inertia
let mouseX = 0;
let mouseY = 0;
let targetRotX = 0;
let targetRotY = 0;
const handleMouseMove = (e: MouseEvent) => {
const rect = container.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
mouseX = (e.clientX - centerX) / (window.innerWidth * 0.5);
mouseY = (e.clientY - centerY) / (window.innerHeight * 0.5);
targetRotY = mouseX * 0.75;
targetRotX = mouseY * 0.55;
};
window.addEventListener('mousemove', handleMouseMove, { passive: true });
// Handle Resize
const handleResize = () => {
if (!container) return;
width = container.clientWidth || 340;
height = container.clientHeight || 340;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
powerPreference: 'high-performance',
});
renderer.setSize(width, height);
};
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
container.appendChild(renderer.domElement);
window.addEventListener('resize', handleResize);
// Root Group for Mouse Parallax
const rootGroup = new THREE.Group();
scene.add(rootGroup);
// IntersectionObserver to pause loop when scrolled out of view
let isVisible = true;
let animFrameId: number;
// 1. Outer Translucent Diamond Crystal Prism (Octahedron)
crystalGeo = new THREE.OctahedronGeometry(1.8, 0);
crystalMat = new THREE.MeshPhysicalMaterial({
color: 0x3b82f6, // Electric Blue
roughness: 0.1,
metalness: 0.1,
transmission: 0.7,
opacity: 0.65,
transparent: true,
emissive: 0x1d4ed8,
emissiveIntensity: 0.2,
});
const crystalMesh = new THREE.Mesh(crystalGeo, crystalMat);
rootGroup.add(crystalMesh);
const observer = new IntersectionObserver(
(entries) => {
const [entry] = entries;
isVisible = entry.isIntersecting;
if (isVisible && !animFrameId) {
animate();
// Subtle Glowing Edge Highlights on Crystal
edgesGeo = new THREE.EdgesGeometry(crystalGeo);
edgesMat = new THREE.LineBasicMaterial({
color: 0x60a5fa,
transparent: true,
opacity: 0.8,
});
const edgesLines = new THREE.LineSegments(edgesGeo, edgesMat);
rootGroup.add(edgesLines);
// 2. Inner Nested Core Prism (Counter-rotating)
innerGeo = new THREE.OctahedronGeometry(1.0, 0);
innerMat = new THREE.MeshPhysicalMaterial({
color: 0x6366f1, // Indigo
roughness: 0.2,
metalness: 0.8,
emissive: 0x4338ca,
emissiveIntensity: 0.5,
transparent: true,
opacity: 0.8,
});
const innerMesh = new THREE.Mesh(innerGeo, innerMat);
rootGroup.add(innerMesh);
// Vertex Spark Points
const vertexPositions = crystalGeo.attributes.position;
dotGeo = new THREE.BufferGeometry();
dotGeo.setAttribute('position', vertexPositions);
dotMat = new THREE.PointsMaterial({
color: 0x93c5fd,
size: 0.14,
transparent: true,
opacity: 0.9,
blending: THREE.AdditiveBlending,
});
const vertexPoints = new THREE.Points(dotGeo, dotMat);
rootGroup.add(vertexPoints);
// 3. Delicate Floating Neural Particles (35 Points)
const particleCount = prefersReducedMotion ? 12 : 36;
const particlePositions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount; i++) {
const radius = 2.2 + Math.random() * 1.4;
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos(2 * Math.random() - 1);
particlePositions[i * 3] = radius * Math.sin(phi) * Math.cos(theta);
particlePositions[i * 3 + 1] = radius * Math.sin(phi) * Math.sin(theta);
particlePositions[i * 3 + 2] = radius * Math.cos(phi);
}
particlesGeo = new THREE.BufferGeometry();
particlesGeo.setAttribute('position', new THREE.BufferAttribute(particlePositions, 3));
particlesMat = new THREE.PointsMaterial({
color: 0xa5b4fc,
size: 0.08,
transparent: true,
opacity: 0.7,
blending: THREE.AdditiveBlending,
});
const particlesMesh = new THREE.Points(particlesGeo, particlesMat);
rootGroup.add(particlesMesh);
// Lights
const ambientLight = new THREE.AmbientLight(0xffffff, 0.9);
scene.add(ambientLight);
const keyLight = new THREE.PointLight(0x38bdf8, 3, 10);
keyLight.position.set(3, 3, 3);
scene.add(keyLight);
const fillLight = new THREE.PointLight(0xa855f7, 2, 10);
fillLight.position.set(-3, -2, 2);
scene.add(fillLight);
// Mouse Tracking with Damped Inertia
let mouseX = 0;
let mouseY = 0;
let targetRotX = 0;
let targetRotY = 0;
const handleMouseMove = (e: MouseEvent) => {
const rect = container.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
mouseX = (e.clientX - centerX) / (window.innerWidth * 0.5);
mouseY = (e.clientY - centerY) / (window.innerHeight * 0.5);
targetRotY = mouseX * 0.75;
targetRotX = mouseY * 0.55;
};
window.addEventListener('mousemove', handleMouseMove, { passive: true });
// Handle Resize
const handleResize = () => {
if (!container || !renderer) return;
width = container.clientWidth || 340;
height = container.clientHeight || 340;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
};
window.addEventListener('resize', handleResize);
// IntersectionObserver
let isVisible = true;
observer = new IntersectionObserver(
(entries) => {
const [entry] = entries;
isVisible = entry.isIntersecting;
if (isVisible && !animFrameId) {
animate();
}
},
{ threshold: 0.05 }
);
observer.observe(container);
const clock = new THREE.Clock();
const animate = () => {
if (!isVisible || !renderer) {
animFrameId = 0;
return;
}
},
{ threshold: 0.05 }
);
observer.observe(container);
let clock = new THREE.Clock();
animFrameId = requestAnimationFrame(animate);
const animate = () => {
if (!isVisible) {
animFrameId = 0;
return;
}
const elapsedTime = clock.getElapsedTime();
animFrameId = requestAnimationFrame(animate);
rootGroup.rotation.y += (targetRotY - rootGroup.rotation.y) * 0.045;
rootGroup.rotation.x += (targetRotX - rootGroup.rotation.x) * 0.045;
const elapsedTime = clock.getElapsedTime();
if (!prefersReducedMotion) {
crystalMesh.rotation.y += 0.004;
crystalMesh.rotation.x += 0.002;
edgesLines.rotation.y += 0.004;
edgesLines.rotation.x += 0.002;
vertexPoints.rotation.y += 0.004;
vertexPoints.rotation.z += 0.002;
// Damped lerp to mouse position
rootGroup.rotation.y += (targetRotY - rootGroup.rotation.y) * 0.045;
rootGroup.rotation.x += (targetRotX - rootGroup.rotation.x) * 0.045;
innerMesh.rotation.y -= 0.006;
innerMesh.rotation.z += 0.003;
const breath = 1.0 + Math.sin(elapsedTime * 2.2) * 0.06;
innerMesh.scale.set(breath, breath, breath);
if (!prefersReducedMotion) {
// Outer crystal slow graceful spin
crystalMesh.rotation.y += 0.004;
crystalMesh.rotation.x += 0.002;
edgesLines.rotation.y += 0.004;
edgesLines.rotation.x += 0.002;
vertexPoints.rotation.y += 0.004;
vertexPoints.rotation.z += 0.002;
particlesMesh.rotation.y -= 0.0015;
particlesMesh.rotation.x += 0.001;
}
// Inner core counter-rotates and breathes gently
innerMesh.rotation.y -= 0.006;
innerMesh.rotation.z += 0.003;
const breath = 1.0 + Math.sin(elapsedTime * 2.2) * 0.06;
innerMesh.scale.set(breath, breath, breath);
renderer.render(scene, camera);
};
// Ambient particles orbit slowly
particlesMesh.rotation.y -= 0.0015;
particlesMesh.rotation.x += 0.001;
}
animate();
renderer.render(scene, camera);
};
return () => {
if (animFrameId) cancelAnimationFrame(animFrameId);
window.removeEventListener('mousemove', handleMouseMove);
window.removeEventListener('resize', handleResize);
if (observer) observer.disconnect();
animate();
return () => {
if (animFrameId) cancelAnimationFrame(animFrameId);
window.removeEventListener('mousemove', handleMouseMove);
window.removeEventListener('resize', handleResize);
observer.disconnect();
crystalGeo.dispose();
crystalMat.dispose();
edgesGeo.dispose();
edgesMat.dispose();
innerGeo.dispose();
innerMat.dispose();
dotGeo.dispose();
dotMat.dispose();
particlesGeo.dispose();
particlesMat.dispose();
renderer.dispose();
if (renderer.domElement && renderer.domElement.parentNode) {
renderer.domElement.parentNode.removeChild(renderer.domElement);
}
};
}, []);
if (crystalGeo) crystalGeo.dispose();
if (crystalMat) crystalMat.dispose();
if (edgesGeo) edgesGeo.dispose();
if (edgesMat) edgesMat.dispose();
if (innerGeo) innerGeo.dispose();
if (innerMat) innerMat.dispose();
if (dotGeo) dotGeo.dispose();
if (dotMat) dotMat.dispose();
if (particlesGeo) particlesGeo.dispose();
if (particlesMat) particlesMat.dispose();
if (renderer) {
renderer.dispose();
if (renderer.domElement && renderer.domElement.parentNode) {
renderer.domElement.parentNode.removeChild(renderer.domElement);
}
}
};
} catch (err) {
console.warn('Three.js / WebGL not supported or failed to initialize, continuing safely:', err);
}
}, [mounted]);
return (
<div
+1 -7
View File
@@ -2,17 +2,11 @@
import React from 'react';
import Link from 'next/link';
import dynamic from 'next/dynamic';
import { Sparkles, ArrowRight, Smartphone, ShieldCheck, Zap, Layers, Cpu } from 'lucide-react';
import { resolveLocalised, ToolItem, AppItem, VariantSpec } from '@/types';
import { TryItWidget } from './TryItWidget';
import { TiltCard } from '@/components/ui/TiltCard';
// Lazy-load Three.js 3D canvas with ssr: false for ultra-fast initial load & SEO safety
const Hero3DCanvas = dynamic(
() => import('@/components/3d/Hero3DCanvas').then((mod) => mod.Hero3DCanvas),
{ ssr: false }
);
import { Hero3DCanvas } from '@/components/3d/Hero3DCanvas';
interface HeroSectionProps {
title?: any;
+8 -5
View File
@@ -8,11 +8,14 @@ export function CursorAura() {
const auraRef = useRef<HTMLDivElement>(null);
useEffect(() => {
// Only enable on pointer-fine (mouse/trackpad) and when not reduced-motion
if (window.matchMedia('(pointer: coarse)').matches) return;
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
setMounted(true);
try {
if (typeof window === 'undefined') return;
if (window.matchMedia && window.matchMedia('(pointer: coarse)').matches) return;
if (window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
setMounted(true);
} catch {
return;
}
const onMouseMove = (e: MouseEvent) => {
posRef.current.targetX = e.clientX;