Skip to content

Commit

Permalink
refactor: optimized performance
Browse files Browse the repository at this point in the history
  • Loading branch information
valcosmos committed Oct 26, 2024
1 parent d7dea08 commit 81cae57
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 653 deletions.
10 changes: 2 additions & 8 deletions app/blog/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ const layouts = {
PostBanner,
}

export async function generateMetadata(
props: {
params: Promise<{ slug: string[] }>
},
): Promise<Metadata | undefined> {
const params = await props.params
export async function generateMetadata({ params }: { params: { slug: string[] } }): Promise<Metadata | undefined> {
const slug = decodeURI(params.slug.join('/'))
const post = allBlogs.find(p => p.slug === slug)
const authorList = post?.authors || ['default']
Expand Down Expand Up @@ -79,8 +74,7 @@ export async function generateStaticParams() {
return allBlogs.map(p => ({ slug: p.slug.split('/').map(name => decodeURI(name)) }))
}

export default async function Page({ params: promiseParams }: { params: Promise<{ slug: string[] }> }) {
const params = await promiseParams
export default async function Page({ params }: { params: { slug: string[] } }) {
const slug = decodeURI(params.slug.join('/'))
// Filter out drafts in production
const sortedCoreContents = allCoreContent(sortPosts(allBlogs))
Expand Down
3 changes: 1 addition & 2 deletions app/blog/page/[page]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export async function generateStaticParams() {
return paths
}

export default async function Page({ params: promiseParams }: { params: Promise<{ page: string }> }) {
const params = await promiseParams
export default async function Page({ params }: { params: { page: string } }) {
const posts = allCoreContent(sortPosts(allBlogs))
const pageNumber = Number.parseInt(params.page as string)
const initialDisplayPosts = posts.slice(
Expand Down
6 changes: 2 additions & 4 deletions app/tags/[tag]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { slug } from 'github-slugger'
import { notFound } from 'next/navigation'
import { allCoreContent, sortPosts } from 'pliny/utils/contentlayer'

export async function generateMetadata(props: { params: Promise<{ tag: string }> }): Promise<Metadata> {
const params = await props.params
export async function generateMetadata({ params }: { params: { tag: string } }): Promise<Metadata> {
const tag = decodeURI(params.tag)
return genPageMetadata({
title: tag,
Expand All @@ -32,8 +31,7 @@ export async function generateStaticParams() {
return paths
}

export default async function TagPage({ params: promiseParams }: { params: Promise<{ tag: string }> }) {
const params = await promiseParams
export default async function TagPage({ params }: { params: { tag: string } }) {
const tag = decodeURI(params.tag)
// Capitalize first letter and convert space to dash
const title = tag[0].toUpperCase() + tag.split(' ').join('-').slice(1)
Expand Down
73 changes: 4 additions & 69 deletions components/BackgroundWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,83 +1,18 @@
'use client'

import { Sphere } from '@react-three/drei'
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
import { Canvas, useFrame } from '@react-three/fiber'
import { motion } from 'framer-motion'
import dynamic from 'next/dynamic'
import React, { useRef } from 'react'
import React from 'react'
import Cursor from './Cursor'
import { pointsInner, pointsOuter } from './utils'

const ScrollTopAndComment = dynamic(() => import('./ScrollTopAndComment'), { ssr: false })

const PointCircle = React.memo(() => {
const ref = useRef<any>(null)
const Ring = dynamic(() => import('./Ring/Ring'), { ssr: false })

useFrame(({ clock }) => {
if (ref.current?.rotation) {
ref.current.rotation.z = clock.getElapsedTime() * 0.05
}
})

return (
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
<group ref={ref}>
{pointsInner.map(point => (
<Point key={point.idx} position={point.position} color={point.color} />
))}
{pointsOuter.map(point => (
<Point key={point.idx} position={point.position} color={point.color} />
))}
{/* eslint-disable-next-line ts/ban-ts-comment */}
{/* @ts-expect-error */}
</group>
)
})

const Circle = React.memo(() => {
return (
<Canvas
camera={{
position: [10, -7.5, -5],
}}
style={{ height: '100vh' }}
className="!absolute inset-0 bg-white dark:bg-slate-900"
>
<PointCircle />
</Canvas>
)
})

function Point({ position, color }) {
return (
<Sphere position={position} args={[0.1, 10, 10]}>
{/* eslint-disable-next-line ts/ban-ts-comment */}
{/* @ts-expect-error */}
<meshStandardMaterial
emissive={color}
emissiveIntensity={0.5}
roughness={0.5}
color={color}
/>
</Sphere>
)
}
const ScrollTopAndComment = dynamic(() => import('./ScrollTopAndComment'), { ssr: false })

export default function BackgroundWrapper({ children }: { children: React.ReactNode }) {
return (
<div className="h-screen w-screen">
<Cursor />
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 3, ease: 'easeInOut' }}
className="!absolute inset-0"
>
<Circle />
</motion.div>
<Ring />
<main id="scroll-container" className="h-screen w-screen overflow-auto rounded-lg shadow-lg backdrop-blur-md">
{children}
</main>
Expand Down
40 changes: 40 additions & 0 deletions components/Ring/PointCircle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use client'

import { Sphere } from '@react-three/drei'
import { useFrame } from '@react-three/fiber'
import React, { useRef } from 'react'
import { pointsInner, pointsOuter } from '../utils'

function Point({ position, color }) {
return (
<Sphere position={position} args={[0.1, 10, 10]}>
<meshStandardMaterial
emissive={color}
emissiveIntensity={0.5}
roughness={0.5}
color={color}
/>
</Sphere>
)
}

export default function PointCircle() {
const ref = useRef<any>(null)

useFrame(({ clock }) => {
if (ref.current?.rotation) {
ref.current.rotation.z = clock.getElapsedTime() * 0.05
}
})

return (
<group ref={ref}>
{pointsInner.map(point => (
<Point key={point.idx} position={point.position} color={point.color} />
))}
{pointsOuter.map(point => (
<Point key={point.idx} position={point.position} color={point.color} />
))}
</group>
)
}
76 changes: 76 additions & 0 deletions components/Ring/Ring.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use client'

import { Canvas } from '@react-three/offscreen'
import { motion } from 'framer-motion'
import React, { lazy, useEffect, useState } from 'react'

const PointCircle = lazy(() => import('./PointCircle'))

// const worker = new Worker(new URL('./worker.tsx', import.meta.url), {
// type: 'module',
// })

// worker.onmessage = (event) => {
// console.error('🍏 Message received from worker: ', event.data)
// }

// worker.onerror = (event) => {
// if (event instanceof Event) {
// console.error('🍎 Error message received from worker: ', event)
// return event
// }

// console.error('🍎 Unexpected error: ', event)
// throw event
// }

export default function Ring() {
const [worker, setWorker] = useState<Worker>()

useEffect(() => {
const plusWorker = new Worker(new URL('./worker.tsx', import.meta.url), {
type: 'module',
})
if (typeof Worker !== 'undefined' && plusWorker) {
setWorker(plusWorker)
}

plusWorker.onmessage = (event) => {
console.error('🍏 Message received from worker: ', event.data)
}

plusWorker.onerror = (event) => {
if (event instanceof Event) {
console.error('🍎 Error message received from worker: ', event)
return event
}

console.error('🍎 Unexpected error: ', event)
throw event
}

return () => {
plusWorker.terminate()
}
}, [])

return (
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 3, ease: 'easeInOut' }}
className="!absolute inset-0"
>
<Canvas
worker={worker!}
shadows
fallback={<PointCircle />}
camera={{
position: [10, -7.5, -5],
}}
style={{ height: '100vh' }}
className="!absolute inset-0 bg-white dark:bg-slate-900"
/>
</motion.div>
)
}
6 changes: 6 additions & 0 deletions components/Ring/worker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use client'

import { render } from '@react-three/offscreen'
import PointCircle from './PointCircle'

render(<PointCircle />)
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = () => {
output,
basePath,
reactStrictMode: true,
// swcMinify: true,
swcMinify: true,
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
eslint: {
dirs: ['app', 'components', 'layouts', 'scripts'],
Expand Down
25 changes: 12 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"packageManager": "pnpm@9.12.2",
"scripts": {
"start": "next dev",
"dev": "cross-env INIT_CWD=$PWD next dev --turbopack",
"dev": "cross-env INIT_CWD=$PWD next dev",
"build": "cross-env INIT_CWD=$PWD next build && cross-env NODE_OPTIONS='--experimental-json-modules' node ./scripts/postbuild.mjs",
"serve": "next start",
"analyze": "cross-env ANALYZE=true next build",
Expand All @@ -14,31 +14,30 @@
"prepare": "simple-git-hooks"
},
"dependencies": {
"@emotion/is-prop-valid": "^1.3.1",
"@headlessui/react": "2.1.10",
"@next/bundle-analyzer": "15.0.1",
"@next/bundle-analyzer": "14.2.15",
"@react-three/drei": "^9.114.6",
"@react-three/fiber": "9.0.0-beta.1",
"@react-three/fiber": "^8.17.10",
"@react-three/offscreen": "^0.0.8",
"@tailwindcss/forms": "^0.5.9",
"@tailwindcss/typography": "^0.5.15",
"autoprefixer": "^10.4.20",
"body-scroll-lock": "^4.0.0-beta.0",
"contentlayer2": "0.5.1",
"esbuild": "0.24.0",
"framer-motion": "12.0.0-alpha.1",
"framer-motion": "^11.11.9",
"github-slugger": "^2.0.0",
"gray-matter": "^4.0.3",
"hast-util-from-html-isomorphic": "^2.0.0",
"image-size": "1.1.1",
"katex": "^0.16.11",
"next": "15.0.1",
"next": "14.2.15",
"next-contentlayer2": "0.5.1",
"next-themes": "^0.3.0",
"next-view-transitions": "^0.3.2",
"pliny": "0.3.2",
"postcss": "^8.4.47",
"react": "19.0.0-rc-cd22717c-20241013",
"react-dom": "19.0.0-rc-cd22717c-20241013",
"react": "18.3.1",
"react-dom": "18.3.1",
"reading-time": "1.5.0",
"rehype-autolink-headings": "^7.1.0",
"rehype-citation": "^2.2.0",
Expand All @@ -58,12 +57,12 @@
"@eslint-react/eslint-plugin": "^1.15.0",
"@svgr/webpack": "^8.1.0",
"@types/mdx": "^2.0.13",
"@types/react": "^18.3.12",
"@typescript-eslint/eslint-plugin": "^8.11.0",
"@typescript-eslint/parser": "^8.11.0",
"@types/react": "^18.3.11",
"@typescript-eslint/eslint-plugin": "^8.10.0",
"@typescript-eslint/parser": "^8.10.0",
"cross-env": "^7.0.3",
"eslint": "^9.13.0",
"eslint-config-next": "15.0.1",
"eslint-config-next": "14.2.15",
"eslint-plugin-format": "^0.1.2",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.13",
Expand Down
Loading

0 comments on commit 81cae57

Please sign in to comment.