Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@reduxjs/toolkit": "^1.8.5",
"@sentry/react": "^7.12.1",
"@sentry/tracing": "^7.12.1",
"@testing-library/react": "^13.4.0",
"@types/react-table": "^7.7.12",
"bootstrap": "5.2.1",
"date-fns": "^2.29.3",
Expand All @@ -18,12 +19,16 @@
"i18next": "^22.4.10",
"i18next-browser-languagedetector": "^6.1.5",
"i18next-http-backend": "^1.4.1",
"idb": "^7.1.1",
"moment": "^2.29.4",
"msw": "^0.47.2",
"react": "^18.2.0",
"react-bootstrap": "^2.6.0",
"react-dom": "^18.2.0",
"react-hook-form": "7.35.0",
"react-i18next": "^12.1.5",
"react-modal-hook": "^3.0.2",
"react-moment": "^1.1.2",
"react-phone-input-2": "^2.15.1",
"react-redux": "^8.0.2",
"react-router-dom": "6.3.0",
Expand All @@ -38,9 +43,9 @@
"yup": "^0.32.9"
},
"scripts": {
"start": "vite",
"start": "esbuild --outfile=public/sw.js --bundle public/sw.ts && workbox injectManifest workbox-config.js && vite",
"analyze": "source-map-explorer 'build/static/js/*.js'",
"build": "tsc && vite build",
"build": "esbuild --outfile=public/sw.js --bundle public/sw.ts && workbox injectManifest workbox-config.js && tsc && vite build",
"predeploy:staging": "tsc && vite build --mode staging",
"predeploy:production": "tsc && vite build",
"deploy:staging": "aws s3 sync ./dist s3://<< replace-with-s3-bucket-name >> --profile shift3 --delete",
Expand Down Expand Up @@ -77,6 +82,7 @@
"@typescript-eslint/eslint-plugin": "^5.50.0",
"@typescript-eslint/parser": "^5.50.0",
"@vitejs/plugin-react": "^3.1.0",
"esbuild": "^0.17.8",
"eslint": "^8.23.1",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.5.0",
Expand All @@ -101,7 +107,12 @@
"typescript-plugin-styled-components": "^2.0.0",
"vite": "^4.1.1",
"vite-plugin-node-polyfills": "^0.7.0",
"vite-tsconfig-paths": "^4.0.5"
"vite-tsconfig-paths": "^4.0.5",
"workbox-background-sync": "^6.5.4",
"workbox-cli": "^6.5.4",
"workbox-precaching": "^6.5.4",
"workbox-routing": "^6.5.4",
"workbox-strategies": "^6.5.4"
},
"lint-staged": {
"*!(*spec).(ts|tsx)": "eslint --max-warnings 0 --fix"
Expand Down
7 changes: 7 additions & 0 deletions public/bootstrap.bundle.min.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions public/bootstrap.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"sizes": "512x512"
}
],
"start_url": ".",
"start_url": "/index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
Expand Down
69 changes: 69 additions & 0 deletions public/offline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="React Boilerplate" content="BWTC React Boilerplate App" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="stylesheet" href="%PUBLIC_URL%/bootstrap.min.css"/>
<script src="%PUBLIC_URL%/bootstrap.bundle.min.js"></script>
<title>React Boilerplate</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root">
<style>
body,
html {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}

.preload-container {
text-align: center;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex;
flex-direction: column;
height: 100%;
}

.preload-child {
margin: auto;
align-items: center;
display: flex;
flex-direction: column;
}

.loader {
border: 0.3em solid blue; /* 3/4 spinner */
border-top: 0.3em solid rgba(255, 255, 255, 0.2); /* 1/4 spinner */
border-radius: 50%;
width: 4em;
height: 4em;
animation: spin 1.1s linear infinite;
}

@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
<h5 class="text-center">We're sorry, this page hasn't been cached yet :/</h5>
<p class="text-center">But why don't you try one of our <a href="/">other pages</a>?</p>
</div>
</body>
</html>
229 changes: 229 additions & 0 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import { precacheAndRoute, matchPrecache } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { BackgroundSyncPlugin } from 'workbox-background-sync';
import { NetworkOnly } from 'workbox-strategies';
import { writeData } from '../src/common/db/utility';
import { useAuth } from '../src/features/auth/hooks';

declare const self: ServiceWorkerGlobalScope;

precacheAndRoute([{"revision":"b75ae000439862b6a97d2129c85680e8","url":"bootstrap.bundle.min.js"},{"revision":"3f30c2c47d7d23c7a994db0c862d45a5","url":"bootstrap.min.css"},{"revision":"7f98bafec3debd677ae13746caf0425d","url":"favicon.ico"},{"revision":"33dbdd0177549353eeeb785d02c294af","url":"logo192.png"},{"revision":"917515db74ea8d1aee6a246cfbcc0b45","url":"logo512.png"},{"revision":"2188852b35933f66039e6caeb7d99f71","url":"manifest.json"},{"revision":"4ef48feda6e0082530020001b73e8a80","url":"offline.html"},{"revision":"fa1ded1ed7c11438a9b0385b1e112850","url":"robots.txt"}]);

// Urls that start with http://localhost:8000/agents/?
// registerRoute(/http:\/\/localhost:8000\/agents\/\?.*/,
// async (args) => {
// return fetch(args.event.request)
// .then((res) => {
// const clonedRes = res.clone();
// clonedRes.json().then((data) => {
// const dataArray = data.results;
// for (const item of dataArray) {
// writeData('agents', item);
// }
// });
// return res;
// });
// },
// 'GET'
// );

const precacheHandler = async ({request}) => {
// Fallback assets are precached when the service worker is installed, and are
// served in the event of an error below. Use `event`, `request`, and `url` to
// figure out how to respond, or use request.destination to match requests for
// specific resource types.
switch (request.destination) {
case 'document':
return matchPrecache('/offline.html')
.then((res) => {
return res || Response.error();
});

case 'style':
return matchPrecache('/bootstrap.min.css')
.then((res) => {
return res || Response.error();
});

case 'script':
return matchPrecache('/bootstrap.bundle.min.js')
.then((res) => {
return res || Response.error();
});

default:
// If we don't have a fallback, return an error response.
return Response.error();
}
}

registerRoute(function (routeData) { // TODO: Create additional register routes in order to dynamically cache the index.tsx, the vite client, and that react-refresh thing
console.log('routeData:', routeData);
console.log('routeData - get -', routeData.event.request.headers.get('accept'));
return (routeData.event.request.headers.get('accept').includes('text/html'));
}, async (args) => {
return caches.match(args.event.request)
.then(function (response) {
if (response) {
return response;
} else {
return fetch(args.event.request)
.then(function (res) {
return caches.open('dynamic')
.then(function (cache) {
cache.put(args.event.request.url, res.clone());
return res;
})
})
.catch(function (err) {
return precacheHandler(args.event.request);
});
}
})
});


registerRoute(function (routeData) { // TODO: Create additional register routes in order to dynamically cache the index.tsx, the vite client, and that react-refresh thing
console.log('routeData:', routeData);
console.log('routeData - get -', routeData.event.request.headers.get('accept'));
return (routeData.event.request.headers.get('accept').includes('*/*'));
}, async (args) => {

const { token } = useAuth();

return caches.match(args.event.request)
.then(function (response) {
if (response) {
return response;
} else {
const url = args.event.request.url;
if (url.includes('index.tsx')) {
return fetch(url)
// return fetch(url, {
// headers: { Authorization: `Token ${token}` },
// })
.then(function (res) {
return caches.open('dynamic')
.then(function (cache) {
cache.put(url, res.clone());
return res;
})
})
.catch(function (err) {
return precacheHandler(args.event.request);
});
} else if (url.includes('@react-refresh')) {
return fetch(url)
// return fetch(url, {
// headers: { Authorization: `Token ${token}` },
// })
.then(function (res) {
return caches.open('dynamic')
.then(function (cache) {
cache.put(url, res.clone());
return res;
})
})
.catch(function (err) {
return precacheHandler(args.event.request);
});
} else if (url.includes('@vite/client')) {
return fetch(url)
// return fetch(url, {
// headers: { Authorization: `Token ${token}` },
// })
.then(function (res) {
return caches.open('dynamic')
.then(function (cache) {
cache.put(url, res.clone());
return res;
})
})
.catch(function (err) {
return precacheHandler(args.event.request);
});
} else if (url.includes('event-token/')) {
return fetch(url)
// return fetch(url, {
// headers: { Authorization: `Token ${token}` },
// })
.then(function (res) {
return caches.open('dynamic')
.then(function (cache) {
cache.put(url, res.clone());
return res;
})
})
.catch(function (err) {
return precacheHandler(args.event.request);
});
} else if (url.includes('?read__isnull=true')) {
return fetch(url)
// return fetch(url, {
// headers: { Authorization: `Token ${token}` },
// })
.then(function (res) {
return caches.open('dynamic')
.then(function (cache) {
cache.put(url, res.clone());
return res;
})
})
.catch(function (err) {
return precacheHandler(args.event.request);
});
} else {
// return precacheHandler(args.event.request); // Getting an error. Look into this, but it looks like I'm going in the right direction.

const url = args.event.request.url;

return fetch(url).then(res => res);


const { token } = useAuth();

// return fetch(url, {
// headers: { Authorization: `Token ${token}` },
// }).then(res => res);

}
}
})
});

// registerRoute(function (routeData) {
// console.log('routeData:', routeData);
// console.log('routeData - get -', routeData.event.request.headers.get('accept'));
// return (routeData.event.request.headers.get('accept').includes('*/*'));
// }, async (args) => {
// return caches.match(args.event.request)
// .then(function (response) {
// if (response) {
// return response;
// } else {
// return fetch(args.event.request)
// .then(function (res) {
// return caches.open('dynamic')
// .then(function (cache) {
// cache.put(args.event.request.url, res.clone());
// return res;
// })
// })
// .catch(function (err) {
// return precacheHandler(args.event.request);
// });
// }
// })
// });

// const bgSyncPlugin = new BackgroundSyncPlugin('RequestQueue', {
// maxRetentionTime: 24 * 60, // Retry for max of 24 Hours (specified in minutes)
// });

// // Creating Agents
// registerRoute(/http:\/\/localhost:8000\/agents\/.*/,
// new NetworkOnly({
// plugins: [bgSyncPlugin],
// }),
// 'POST'
// );
Loading