From 5b2a2ed646f6dbf75d45eb0bfd0b492899215409 Mon Sep 17 00:00:00 2001 From: M Gilang Januar Date: Fri, 31 Dec 2021 09:46:52 +0700 Subject: [PATCH 1/5] [exp] pwa --- web/package.json | 2 +- web/src/index.tsx | 2 +- web/sw-config.js | 14 +++++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/web/package.json b/web/package.json index c1aa885cc..95af77746 100644 --- a/web/package.json +++ b/web/package.json @@ -37,7 +37,7 @@ }, "scripts": { "start": "craco start", - "build": "craco build", + "build": "craco build && sw-precache-cra --config sw-config.js", "test": "craco test", "eject": "craco eject" }, diff --git a/web/src/index.tsx b/web/src/index.tsx index 07ca7e849..2b95870e8 100644 --- a/web/src/index.tsx +++ b/web/src/index.tsx @@ -17,7 +17,7 @@ ReactDOM.render( // If you want your app to work offline and load faster, you can change // unregister() to register() below. Note this comes with some pitfalls. // Learn more about service workers: https://cra.link/PWA -serviceWorkerRegistration.unregister() +serviceWorkerRegistration.register() // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) diff --git a/web/sw-config.js b/web/sw-config.js index b42b19cb1..dbe2166c1 100644 --- a/web/sw-config.js +++ b/web/sw-config.js @@ -1,9 +1,21 @@ module.exports = { runtimeCaching: [ { - urlPattern: /^(.*\/api\/).*$/, + urlPattern: '/api/', handler: 'networkFirst', }, + { + urlPattern: '/view', + handler: 'networkFirst', + }, + // { + // urlPattern: '/dashboard', + // handler: 'networkFirst', + // }, + // { + // urlPattern: /^(.*\/api\/).*$|^(.*\/dashboard).*$|^(.*\/view).*$/, + // handler: 'networkFirst', + // }, // { // urlPattern: /^(?!.*\/api\/).*$/, // handler: 'fastest', From faaa6b0df70e5ffcd0d7670f101dd07648d60976 Mon Sep 17 00:00:00 2001 From: M Gilang Januar Date: Fri, 31 Dec 2021 10:06:48 +0700 Subject: [PATCH 2/5] comment notfound page --- web/src/App.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/web/src/App.tsx b/web/src/App.tsx index 699eee810..2c568899c 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -36,9 +36,9 @@ const Contact = lazy( () => import(/* webpackChunkName: 'ContactPage' */ './pages/Contact') ) const Faq = lazy(() => import(/* webpackChunkName: 'FaqPage' */ './pages/Faq')) -const NotFound = lazy( - () => import(/* webpackChunkName: 'NotFoundPage' */ './pages/errors/NotFound') -) +// const NotFound = lazy( +// () => import(/* webpackChunkName: 'NotFoundPage' */ './pages/errors/NotFound') +// ) function App(): React.ReactElement { const { pathname } = useLocation() @@ -70,7 +70,7 @@ function App(): React.ReactElement { {new URLSearchParams(window.location.search).get('source') === 'pwa' ? : } - + {/* */} } From ecfe2d8c1957cbc31aab0cb6e5d87a46bd8459eb Mon Sep 17 00:00:00 2001 From: M Gilang Januar Date: Fri, 31 Dec 2021 10:45:21 +0700 Subject: [PATCH 3/5] exclude pathname that contains /api --- web/src/App.tsx | 8 ++++---- web/src/service-worker.ts | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/web/src/App.tsx b/web/src/App.tsx index 2c568899c..699eee810 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -36,9 +36,9 @@ const Contact = lazy( () => import(/* webpackChunkName: 'ContactPage' */ './pages/Contact') ) const Faq = lazy(() => import(/* webpackChunkName: 'FaqPage' */ './pages/Faq')) -// const NotFound = lazy( -// () => import(/* webpackChunkName: 'NotFoundPage' */ './pages/errors/NotFound') -// ) +const NotFound = lazy( + () => import(/* webpackChunkName: 'NotFoundPage' */ './pages/errors/NotFound') +) function App(): React.ReactElement { const { pathname } = useLocation() @@ -70,7 +70,7 @@ function App(): React.ReactElement { {new URLSearchParams(window.location.search).get('source') === 'pwa' ? : } - {/* */} + } diff --git a/web/src/service-worker.ts b/web/src/service-worker.ts index fe560c43e..b45c382ea 100644 --- a/web/src/service-worker.ts +++ b/web/src/service-worker.ts @@ -47,6 +47,10 @@ registerRoute( return false } + if (url.pathname.match(/\/api\/.*/gi)) { + return false + } + // Return true to signal that we want to use the handler. return true }, From cc1e0149b92d36a06d38c85f6093cfa3be4a0241 Mon Sep 17 00:00:00 2001 From: M Gilang Januar Date: Fri, 31 Dec 2021 11:13:14 +0700 Subject: [PATCH 4/5] always not run by handler --- web/src/service-worker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/src/service-worker.ts b/web/src/service-worker.ts index b45c382ea..0bc8845a0 100644 --- a/web/src/service-worker.ts +++ b/web/src/service-worker.ts @@ -52,7 +52,7 @@ registerRoute( } // Return true to signal that we want to use the handler. - return true + return false }, createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html') ) From 0c9e5cf3f7cb8f0b296ee328a151722d8fa2eb21 Mon Sep 17 00:00:00 2001 From: M Gilang Januar Date: Fri, 31 Dec 2021 11:24:15 +0700 Subject: [PATCH 5/5] revert the previous commit --- web/src/service-worker.ts | 2 +- web/sw-config.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/web/src/service-worker.ts b/web/src/service-worker.ts index 0bc8845a0..b45c382ea 100644 --- a/web/src/service-worker.ts +++ b/web/src/service-worker.ts @@ -52,7 +52,7 @@ registerRoute( } // Return true to signal that we want to use the handler. - return false + return true }, createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html') ) diff --git a/web/sw-config.js b/web/sw-config.js index dbe2166c1..bd12ec3fb 100644 --- a/web/sw-config.js +++ b/web/sw-config.js @@ -1,4 +1,5 @@ module.exports = { + navigateFallbackWhitelist: [/^(?!.*\/api\/).*/], runtimeCaching: [ { urlPattern: '/api/',