forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ perf: refactor pwa implement to have better performance (lobehub#4124
) * test remove serwist * 尝试优化 pwa install 实现 * Update next.config.mjs * fix lint * delay the service worker register * only enabled on prod * when isShowPWAGuide update, trigger guide too
- Loading branch information
Showing
13 changed files
with
135 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,5 +61,9 @@ bun.lockb | |
sitemap*.xml | ||
robots.txt | ||
|
||
# Serwist | ||
public/sw* | ||
public/swe-worker* | ||
|
||
*.patch | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { defaultCache } from '@serwist/next/worker'; | ||
import type { PrecacheEntry, SerwistGlobalConfig } from 'serwist'; | ||
import { Serwist } from 'serwist'; | ||
|
||
// This declares the value of `injectionPoint` to TypeScript. | ||
// `injectionPoint` is the string that will be replaced by the | ||
// actual precache manifest. By default, this string is set to | ||
// `"self.__SW_MANIFEST"`. | ||
declare global { | ||
interface WorkerGlobalScope extends SerwistGlobalConfig { | ||
__SW_MANIFEST: (PrecacheEntry | string)[] | undefined; | ||
} | ||
} | ||
|
||
// eslint-disable-next-line no-undef | ||
declare const self: ServiceWorkerGlobalScope; | ||
|
||
const serwist = new Serwist({ | ||
clientsClaim: true, | ||
navigationPreload: true, | ||
precacheEntries: self.__SW_MANIFEST, | ||
runtimeCaching: defaultCache, | ||
skipWaiting: true, | ||
}); | ||
|
||
serwist.addEventListeners(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use client'; | ||
|
||
import dynamic from 'next/dynamic'; | ||
import { memo, useEffect, useLayoutEffect } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { BRANDING_NAME } from '@/const/branding'; | ||
import { PWA_INSTALL_ID } from '@/const/layoutTokens'; | ||
import { usePWAInstall } from '@/hooks/usePWAInstall'; | ||
import { useGlobalStore } from '@/store/global'; | ||
import { systemStatusSelectors } from '@/store/global/selectors'; | ||
import { useUserStore } from '@/store/user'; | ||
|
||
// @ts-ignore | ||
const PWA: any = dynamic(() => import('@khmyznikov/pwa-install/dist/pwa-install.react.js'), { | ||
ssr: false, | ||
}); | ||
|
||
const PWAInstall = memo(() => { | ||
const { t } = useTranslation('metadata'); | ||
|
||
const { install, canInstall } = usePWAInstall(); | ||
|
||
const isShowPWAGuide = useUserStore((s) => s.isShowPWAGuide); | ||
const [hidePWAInstaller, updateSystemStatus] = useGlobalStore((s) => [ | ||
systemStatusSelectors.hidePWAInstaller(s), | ||
s.updateSystemStatus, | ||
]); | ||
|
||
// we need to make the pwa installer hidden by default | ||
useLayoutEffect(() => { | ||
sessionStorage.setItem('pwa-hide-install', 'true'); | ||
}, []); | ||
|
||
const pwaInstall = | ||
// eslint-disable-next-line unicorn/prefer-query-selector | ||
typeof window === 'undefined' ? undefined : document.getElementById(PWA_INSTALL_ID); | ||
|
||
// add an event listener to control the user close installer action | ||
useEffect(() => { | ||
if (!pwaInstall) return; | ||
|
||
const handler = (e: Event) => { | ||
const event = e as CustomEvent; | ||
|
||
// it means user hide installer | ||
if (event.detail.message === 'dismissed') { | ||
updateSystemStatus({ hidePWAInstaller: true }); | ||
} | ||
}; | ||
|
||
pwaInstall.addEventListener('pwa-user-choice-result-event', handler); | ||
return () => { | ||
pwaInstall.removeEventListener('pwa-user-choice-result-event', handler); | ||
}; | ||
}, [pwaInstall]); | ||
|
||
// trigger the PWA guide on demand | ||
useEffect(() => { | ||
if (!canInstall || hidePWAInstaller) return; | ||
|
||
// trigger the pwa installer and register the service worker | ||
if (isShowPWAGuide) { | ||
install(); | ||
if ('serviceWorker' in navigator && window.serwist !== undefined) { | ||
window.serwist.register(); | ||
} | ||
} | ||
}, [canInstall, hidePWAInstaller, isShowPWAGuide]); | ||
|
||
return ( | ||
<PWA | ||
description={t('chat.description', { appName: BRANDING_NAME })} | ||
id={PWA_INSTALL_ID} | ||
manifest-url={'/manifest.webmanifest'} | ||
/> | ||
); | ||
}); | ||
|
||
export default PWAInstall; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,24 @@ | ||
'use client'; | ||
|
||
import dynamic from 'next/dynamic'; | ||
import { memo, useEffect, useLayoutEffect } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { memo } from 'react'; | ||
|
||
import { BRANDING_NAME } from '@/const/branding'; | ||
import { PWA_INSTALL_ID } from '@/const/layoutTokens'; | ||
import { usePWAInstall } from '@/hooks/usePWAInstall'; | ||
import { usePlatform } from '@/hooks/usePlatform'; | ||
import { useGlobalStore } from '@/store/global'; | ||
import { systemStatusSelectors } from '@/store/global/selectors'; | ||
import { useUserStore } from '@/store/user'; | ||
|
||
// @ts-ignore | ||
const PWA: any = dynamic(() => import('@khmyznikov/pwa-install/dist/pwa-install.react.js'), { | ||
const Install: any = dynamic(() => import('./Install'), { | ||
ssr: false, | ||
}); | ||
|
||
const PWAInstall = memo(() => { | ||
const { t } = useTranslation('metadata'); | ||
const { isPWA } = usePlatform(); | ||
|
||
const { install, canInstall } = usePWAInstall(); | ||
|
||
const isShowPWAGuide = useUserStore((s) => s.isShowPWAGuide); | ||
const [hidePWAInstaller, updateSystemStatus] = useGlobalStore((s) => [ | ||
systemStatusSelectors.hidePWAInstaller(s), | ||
s.updateSystemStatus, | ||
]); | ||
|
||
// we need to make the pwa installer hidden by default | ||
useLayoutEffect(() => { | ||
sessionStorage.setItem('pwa-hide-install', 'true'); | ||
}, []); | ||
|
||
const pwaInstall = | ||
// eslint-disable-next-line unicorn/prefer-query-selector | ||
typeof window === 'undefined' ? undefined : document.getElementById(PWA_INSTALL_ID); | ||
|
||
// add an event listener to control the user close installer action | ||
useEffect(() => { | ||
if (!pwaInstall) return; | ||
|
||
const handler = (e: Event) => { | ||
const event = e as CustomEvent; | ||
|
||
// it means user hide installer | ||
if (event.detail.message === 'dismissed') { | ||
updateSystemStatus({ hidePWAInstaller: true }); | ||
} | ||
}; | ||
|
||
pwaInstall.addEventListener('pwa-user-choice-result-event', handler); | ||
return () => { | ||
pwaInstall.removeEventListener('pwa-user-choice-result-event', handler); | ||
}; | ||
}, [pwaInstall]); | ||
|
||
// trigger the PWA guide on demand | ||
useEffect(() => { | ||
if (!canInstall || hidePWAInstaller) return; | ||
|
||
if (isShowPWAGuide) { | ||
install(); | ||
} | ||
}, [canInstall, hidePWAInstaller, isShowPWAGuide]); | ||
if (isPWA || !isShowPWAGuide) return null; | ||
|
||
if (isPWA) return null; | ||
return ( | ||
<PWA | ||
description={t('chat.description', { appName: BRANDING_NAME })} | ||
id={PWA_INSTALL_ID} | ||
manifest-url={'/manifest.webmanifest'} | ||
/> | ||
); | ||
// only when the user is suitable for the pwa install and not install the pwa | ||
// then show the installation guide | ||
return <Install />; | ||
}); | ||
|
||
export default PWAInstall; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters