Skip to content

Commit

Permalink
using throttle for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
KishiTheMechanic committed Oct 31, 2024
1 parent 89d32e8 commit f5793b7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"iconoir-react": "https://esm.sh/[email protected]?alias=react:preact/compat,react-dom:preact/compat,@types/react:preact/compat&external=preact",
"clsx": "npm:[email protected]",
"@react-spring/web": "npm:@react-spring/[email protected]",
"class-variance-authority": "npm:[email protected]"
"class-variance-authority": "npm:[email protected]",
"lodash.throttle": "https://raw.githubusercontent.com/lodash/lodash/4.17.21-es/throttle.js"
},
"compilerOptions": {
"lib": ["dom", "dom.asynciterable", "deno.ns"],
Expand Down
17 changes: 16 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions hooks/utils/useShowHeader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect } from 'preact/hooks'
import { useSignal } from '@preact/signals'
import throttle from 'lodash.throttle'

type Props = {
defaultShowHeader?: boolean
Expand All @@ -21,11 +22,15 @@ export function useShowHeader({ defaultShowHeader = true }: Props = {}) {

lastScrollY.value = currentScrollY
}
const throttledHandleScroll = throttle(handleScroll, 100) as (
this: Window,
ev: Event,
) => void

globalThis.addEventListener('scroll', handleScroll)
globalThis.addEventListener('scroll', throttledHandleScroll)

return () => {
globalThis.removeEventListener('scroll', handleScroll)
globalThis.removeEventListener('scroll', throttledHandleScroll)
}
}, [])

Expand Down
1 change: 0 additions & 1 deletion routes/[locale]/(default)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const handler = define.handlers({
ctx.state.title = 'metadata.homeTitle'
ctx.state.description = 'metadata.description'
ctx.state.ogImage = new URL(asset('/ogp.jpg'), ctx.url).href

return page()
},
})
Expand Down

0 comments on commit f5793b7

Please sign in to comment.