Skip to content

Commit

Permalink
SW fix + Promises unification
Browse files Browse the repository at this point in the history
  • Loading branch information
StopNGo committed Apr 19, 2023
1 parent a58f209 commit 320b9b1
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-proto",
"version": "1.4.1",
"version": "1.4.2",
"description": "React TypeScript Boilerplate",
"author": "Max L Stop&Go",
"license": "ISC",
Expand Down
6 changes: 4 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FC, ReactElement, useEffect } from 'react'
import { useRoutes } from 'react-router-dom'
import cn from 'classnames'

import {
Expand All @@ -7,13 +8,14 @@ import {
ErrorBoundary,
Offline
} from 'components'
import Router from 'router/Router'
import { routes } from 'router/Router'

import { useAppSelector, useAppDispatch } from 'store/store'
import { switchToDark } from 'store/theme/themeSlice'
import { THEME_NAMES } from 'constants/commonConstants'

const App: FC = (): ReactElement => {
const content = useRoutes(routes)
const currentTheme = useAppSelector((state) => state.theme.theme)
const dispatch = useAppDispatch()

Expand All @@ -37,7 +39,7 @@ const App: FC = (): ReactElement => {
<LanguageSelector />
<ThemeSwitcher />
<Offline />
<Router />
{content}
</div>
</ErrorBoundary>
)
Expand Down
3 changes: 2 additions & 1 deletion src/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
script-src 'self';
script-src-attr 'none';
style-src 'self' https: 'unsafe-inline';
upgrade-insecure-requests
upgrade-insecure-requests;
worker-src 'self';
"
/>
<title></title>
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/i18nApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export async function fetchTranslations (
.then(
(data) => resolve(data),
() => {}
)
).catch(er => console.log(er))
})
}
6 changes: 2 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ if (
}

startServiceWorkerPromise().then(
() => {},
() => {}
)
).catch(er => console.log(er))
}

const indexJSX = (
Expand All @@ -63,7 +62,6 @@ if (NO_SSR) {
loadableReady(() => {
hydrateRoot(container, indexJSX)
}).then(
() => {},
() => {}
)
).catch(er => console.log(er))
}
42 changes: 30 additions & 12 deletions src/router/Router.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import { FC } from 'react'
import { Route, Routes } from 'react-router-dom'
import { RouteObject } from 'react-router-dom'

import { ROUTE_CONSTANTS } from 'constants/routeConstants'
import { About, Fetch, Home, NotFound } from 'pages'

const Router: FC = () => (
<Routes>
<Route path='*' element={<NotFound />} />
<Route path={ROUTE_CONSTANTS.HOME} element={<Home />} />
<Route path={ROUTE_CONSTANTS.FETCH} element={<Fetch />} />
<Route path={ROUTE_CONSTANTS.ABOUT} element={<About />} />
<Route path={ROUTE_CONSTANTS.NOT_FOUND} element={<NotFound />} />
</Routes>
)
const routes: RouteObject[] = [
{
path: '*',
element: <NotFound />
},
{
path: ROUTE_CONSTANTS.HOME,
element: <Home />
},
{
path: ROUTE_CONSTANTS.FETCH,
element: <Fetch />
},
{
path: ROUTE_CONSTANTS.ABOUT,
element: <About />
},
{
path: ROUTE_CONSTANTS.NOT_FOUND,
element: <NotFound />
},
{
path: 'sw.js',
loader: async () => {
return await fetch('sw.js')
}
}
]

export default Router
export { routes }
5 changes: 2 additions & 3 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,16 @@ const runServer = (hotReload?: () => RequestHandler[]): void => {
}

if (IS_DEV) {
;(async () => {
(async () => {
const { hotReload, devMiddlewareInstance } = await import(
'./middlewares/hotReload'
)
devMiddlewareInstance.waitUntilValid(() => {
runServer(hotReload)
})
})().then(
() => {},
() => {}
)
).catch(er => console.log(er))
} else {
runServer()
}
10 changes: 8 additions & 2 deletions src/serviceWorker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const registerSW = async (): Promise<void> => {
try {
const registration = await navigator.serviceWorker.register('/sw.js')
const registration = await navigator.serviceWorker.register('./sw.js')
console.log(
'ServiceWorker registration successful with scope: ',
registration.scope
Expand All @@ -12,6 +12,12 @@ const registerSW = async (): Promise<void> => {

export const startServiceWorker = (): void => {
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => registerSW)
window.addEventListener('load', () => {
(async () => {
await registerSW()
})().then(
() => {}
).catch(er => console.log(er))
})
}
}
5 changes: 4 additions & 1 deletion webpack/client.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ const plugins: WebpackPluginInstance[] = [
]),
new CopyPlugin({
patterns: [
{ from: `${SRC_DIR}/i18n/translations`, to: 'lang' }
{ from: `${SRC_DIR}/i18n/translations`, to: 'lang' },
...(process.env.NO_SSR === 'true'
? [{ from: `${SRC_DIR}/sw.js`, to: 'sw.js' }]
: [])
]
})
]
Expand Down

0 comments on commit 320b9b1

Please sign in to comment.