Skip to content

Commit

Permalink
Add the 'Periodic Sync' page
Browse files Browse the repository at this point in the history
  • Loading branch information
theninthsky committed Dec 4, 2022
1 parent a20a0a0 commit e741cf3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
5 changes: 4 additions & 1 deletion public/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ self.addEventListener('fetch', event => {
})

self.addEventListener('periodicsync', event => {
if (event.tag === 'revalidate-assets') event.waitUntil(preCache())
if (event.tag === 'revalidate-assets') {
event.waitUntil(preCache())
new BroadcastChannel('main').postMessage({ syncTime: new Date().toISOString() })
}
})
3 changes: 2 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ const LoremIpsum = lazy(() => import(/* webpackChunkName: "lorem-ipsum" */ 'page
const Pokemon = lazy(() => import(/* webpackChunkName: "pokemon" */ 'pages/Pokemon'))
const PokemonInfo = lazy(() => import(/* webpackChunkName: "pokemon-info" */ 'pages/PokemonInfo'))
const WebVitals = lazy(() => import(/* webpackChunkName: "core-web-vitals" */ 'pages/WebVitals'))
const PeriodicSync = lazy(() => import(/* webpackChunkName: "periodic-sync" */ 'pages/PeriodicSync'))

const pages = [Home, LoremIpsum, Pokemon, PokemonInfo, WebVitals]
const pages = [Home, LoremIpsum, Pokemon, PokemonInfo, WebVitals, PeriodicSync]
const routes = pagesManifest.map(({ path }, ind) => {
const Element = pages[ind]

Expand Down
2 changes: 2 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ createRoot(document.getElementById('root')).render(
</Suspense>
</BrowserRouter>
)

new BroadcastChannel('main').onmessage = ({ data }) => localStorage.setItem('syncTime', data.syncTime)
6 changes: 6 additions & 0 deletions src/pages-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,11 @@
"path": "/web-vitals",
"title": "Web Vitals",
"description": "This page specifies the values of core web vitals."
},
{
"chunk": "periodic-sync",
"path": "/periodic-sync",
"title": "Periodic Sync",
"description": "This page specifies the time of the last periodic background sync."
}
]
48 changes: 48 additions & 0 deletions src/pages/PeriodicSync.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Meta } from 'frontend-essentials'
import { css, cx } from '@emotion/css'

import pagesManifest from 'pages-manifest.json'
import Title from 'components/common/Title'
import Info from 'components/common/Info'

const { title, description } = pagesManifest.find(({ chunk }) => chunk === 'periodic-sync')

const WebVitals = () => {
const { syncTime } = localStorage

return (
<div>
<Meta title={`${title} | Client-side Rendering`} description={description} />

<Title>{title}</Title>

<Info className={style.info}>{description}</Info>

{window.matchMedia('(display-mode: standalone)').matches ? (
<span className={style.syncTime}>
Last sync time:{' '}
{syncTime
? `${new Date(syncTime).toLocaleDateString('en-GB')} ${new Date(syncTime).toLocaleTimeString()}`
: 'Never'}
</span>
) : (
<span className={cx(style.syncTime, style.alert)}>App not installed.</span>
)}
</div>
)
}

const style = {
info: css`
margin-top: 20px;
`,
syncTime: css`
display: block;
margin-top: 20px;
`,
alert: css`
color: red;
`
}

export default WebVitals

0 comments on commit e741cf3

Please sign in to comment.