Skip to content

Commit

Permalink
fix: handle hydration missing menu and style
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Sep 12, 2024
1 parent 2562d4a commit bf687da
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ function main() {
styleEl.id = 'sentinel-css'
document.head.append(styleEl)

const injectionWeakMap = new WeakMap()
const injectionMap = new Map<HTMLElement, HTMLElement>()

sentinel.on('nav', (nav) => {
if (injectionWeakMap.has(nav)) return
injectionWeakMap.set(nav, true)
const injectNavMenu = (nav: HTMLElement) => {
if (injectionMap.has(nav)) return

const container = getMenuContainer()
injectionMap.set(nav, container)

const chatList = nav.querySelector(':scope > div.overflow-y-auto, :scope > div.overflow-y-hidden')
if (chatList) {
chatList.after(container)
Expand All @@ -31,7 +32,21 @@ function main() {
// fallback to the bottom of the nav
nav.append(container)
}
})
}

sentinel.on('nav', injectNavMenu)

setInterval(() => {
injectionMap.forEach((container, nav) => {
if (!nav.isConnected) {
container.remove()
injectionMap.delete(nav)
}
})

const navList = Array.from(document.querySelectorAll('nav')).filter(nav => !injectionMap.has(nav))
navList.forEach(injectNavMenu)
}, 300)

// Support for share page
if (isSharePage()) {
Expand Down
11 changes: 11 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ export default defineConfig({
['jszip', cdn.jsdelivr('JSZip', 'dist/jszip.min.js')],
['html2canvas', cdn.jsdelivr('html2canvas', 'dist/html2canvas.min.js')],
],
cssSideEffects() {
return (e) => {
const o = document.createElement('style')
o.textContent = e
document.head.append(o)
setInterval(() => {
if (o.isConnected) return
document.head.append(o)
}, 300)
}
},
},
server: {
open: true,
Expand Down

0 comments on commit bf687da

Please sign in to comment.