Skip to content

Commit

Permalink
Merge pull request #34 from AlekEagle/development
Browse files Browse the repository at this point in the history
Improved connectivity handling

CU-86dv3rbbb[complete]
  • Loading branch information
AlekEagle authored Nov 5, 2024
2 parents 97b380a + bb3e6c8 commit 0422bb9
Show file tree
Hide file tree
Showing 26 changed files with 130 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cumulonimbus-frontend",
"private": true,
"version": "4.6.0",
"version": "4.6.1",
"type": "module",
"license": "GNU-GPL-3.0",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
// External Modules
import { ref, onMounted, watch, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
const user = userStore(),
toast = toastStore(),
Expand Down
4 changes: 2 additions & 2 deletions src/components/DomainModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
// External Modules
import { ref, onMounted, watch } from 'vue';
import { useNetwork } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
const emit = defineEmits<{
(event: 'submit', data: { domain: string; subdomain?: string }): void;
Expand Down Expand Up @@ -101,7 +101,7 @@
domain = ref<string>(),
subdomain = ref<string>(),
allowsSubdomains = ref(false),
{ isOnline: online } = useNetwork();
online = useOnline();
function onSubdomainInput(event: Event) {
const input = event.target as HTMLInputElement;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Online.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// No Store Modules to import here.
// External Modules
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
const props = defineProps({
noMsg: {
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createApp } from 'vue';
import App from './App.vue';
import { ConnectivityCheckPlugin } from './utils/ConnectivityCheck';
import { createPinia } from 'pinia';
import { router } from './router';
import packageJson from '../package.json';
Expand All @@ -13,5 +14,6 @@ app.config.globalProperties.$dependencies = packageJson.dependencies;
app.config.globalProperties.$devDependencies = packageJson.devDependencies;

app.use(router);
app.use(ConnectivityCheckPlugin);
app.use(createPinia());
app.mount('html body');
58 changes: 56 additions & 2 deletions src/sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,60 @@ async function threadedPrecache(urls: string[], threads: number = 4) {
debugLog('ServiceWorkerThreadedPrecache', 'Precaching complete.');
}

// ---- Refined Connectivity Check ----

const checkInterval = 60e3,
checkTarget = 'https://connectivitycheck.alekeagle.com',
checkTargetResponseStatus = 204;

// The variable we'll use to track if we're online or not. We'll initialize with the navigator's online status.
let isOnline = navigator.onLine,
isOnlineInterval: ReturnType<typeof setInterval> | null = null;

async function makeConnectionCheck() {
try {
// Make a call to a known online resource to check if we're online.
const res = await fetch(checkTarget, { method: 'HEAD' });
// If the response status matches the expected status, we're online.
if (isOnline !== (res.status === checkTargetResponseStatus)) {
isOnline = res.status === checkTargetResponseStatus;
debugLog(
'ServiceWorkerConnectivityCheck',
'Connection status changed:',
isOnline,
);
}
} catch (err) {
// If the fetch fails, we're probably offline.
if (isOnline) {
isOnline = false;
debugLog(
'ServiceWorkerConnectivityCheck',
'Connection status changed:',
isOnline,
);
}
}
}

async function runConnectionCheck() {
// Clear the interval if it exists.
if (isOnlineInterval) {
clearInterval(isOnlineInterval);
isOnlineInterval = null;
}
// Make a connection check immediately.
await makeConnectionCheck();
// Set the interval back up to check every x seconds.
isOnlineInterval = setInterval(makeConnectionCheck, checkInterval);
}

self.addEventListener('online', runConnectionCheck);
self.addEventListener('offline', runConnectionCheck);

// Start the connection check interval.
runConnectionCheck();

// ---- Service worker lifecycle ----

self.addEventListener('install', async (event) => {
Expand Down Expand Up @@ -149,7 +203,7 @@ router.addRoute(
'Cache hit',
`URL: ${options.url}`,
);
if (navigator.onLine) {
if (isOnline) {
debugLog(
'ServiceWorkerOfflineCacheManager',
'Revalidating cache',
Expand All @@ -167,7 +221,7 @@ router.addRoute(
'Cache miss',
`URL: ${options.url}`,
);
if (!navigator.onLine) {
if (!isOnline) {
debugLog(
'ServiceWorkerOfflineCacheManager',
'Offline, serving default route',
Expand Down
46 changes: 46 additions & 0 deletions src/utils/ConnectivityCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { inject, Plugin, ref } from 'vue';

const checkInterval = 60e3,
checkTarget = 'https://connectivitycheck.alekeagle.com',
checkTargetResponseStatus = 204;

export const ConnectivityCheckPlugin: Plugin = {
install(app) {
let isOnlineInterval: ReturnType<typeof setInterval> | null = null;
// Initialize the isOnline ref with the current navigator status
const isOnline = ref(navigator.onLine);

app.provide('isOnline', isOnline);

async function makeConnectionCheck() {
try {
const response = await fetch(checkTarget, { method: 'HEAD' });
isOnline.value = response.status === checkTargetResponseStatus;
} catch (error) {
isOnline.value = false;
}
}

async function runConnectionCheck() {
// Clear the interval if it exists.
if (isOnlineInterval) {
clearInterval(isOnlineInterval);
isOnlineInterval = null;
}
// Make a connection check immediately.
await makeConnectionCheck();
// Set the interval back up to check every x seconds.
isOnlineInterval = setInterval(makeConnectionCheck, checkInterval);
}

window.addEventListener('online', runConnectionCheck);
window.addEventListener('offline', runConnectionCheck);

// Start the connection check interval.
runConnectionCheck();
},
};

export const useOnline = () => {
return inject('isOnline') as ReturnType<typeof ref>;
};
2 changes: 1 addition & 1 deletion src/utils/loadWhenOnline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { watch } from 'vue';

// This function takes a callback and conditions for when to call the callback.
Expand Down
2 changes: 1 addition & 1 deletion src/views/AccountSwitcher.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
// External Modules
import { ref, onBeforeMount, computed } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter } from 'vue-router';
const user = userStore(),
Expand Down
4 changes: 2 additions & 2 deletions src/views/Auth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
// External Modules
import { ref, onMounted } from 'vue';
import { useNetwork } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter, useRoute } from 'vue-router';
const user = userStore(),
Expand All @@ -128,7 +128,7 @@
toast = toastStore(),
loginForm = ref<InstanceType<typeof Form>>(),
registerForm = ref<InstanceType<typeof Form>>(),
{ isOnline: online } = useNetwork();
online = useOnline();
async function toggleState() {
switch (action.value) {
Expand Down
2 changes: 1 addition & 1 deletion src/views/staff-space/Domains.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
// External Modules
import { ref, onMounted } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter } from 'vue-router';
const online = useOnline(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/staff-space/Files.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
// External Modules
import { ref, onMounted } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter } from 'vue-router';
import loadWhenOnline from '@/utils/loadWhenOnline';
Expand Down
2 changes: 1 addition & 1 deletion src/views/staff-space/KillSwitches.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
// External Modules
import { ref, onMounted } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import LoadingMessage from '@/components/LoadingMessage.vue';
const killSwitches = killSwitchesStore(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/staff-space/SetupGuide.vue
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
// External Modules
import { ref, watch, onMounted } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter } from 'vue-router';
const online = useOnline(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/staff-space/SetupGuides.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
// External Modules
import { ref, watch, onMounted } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter } from 'vue-router';
import loadWhenOnline from '@/utils/loadWhenOnline';
Expand Down
2 changes: 1 addition & 1 deletion src/views/staff-space/User.vue
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@
// External Modules
import { ref, computed, onMounted } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter } from 'vue-router';
const user = userStore(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/staff-space/UserSecondFactors.vue
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
// External Modules
import { ref, onMounted } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter } from 'vue-router';
const secondFactors = secondFactorsStore(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/staff-space/UserSessions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
// External Modules
import { ref, onMounted, computed } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter } from 'vue-router';
const router = useRouter(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/staff-space/Users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
// External Modules
import { ref, onMounted } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import loadWhenOnline from '@/utils/loadWhenOnline';
const users = usersStore(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/user-space/Account.vue
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@
// External Modules
import { ref } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter } from 'vue-router';
const deleteAccountModal = ref<InstanceType<typeof FormModal>>(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/user-space/AccountSecondFactors.vue
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
// External Modules
import { ref, onMounted, computed } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import QRCode from 'qrcode';
import { startRegistration } from '@simplewebauthn/browser';
Expand Down
2 changes: 1 addition & 1 deletion src/views/user-space/AccountSessions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@
// External Modules
import { ref, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useClipboard } from '@vueuse/core';
const confirmDeleteModal = ref<InstanceType<typeof ConfirmModal>>(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/user-space/Files.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
// External Modules
import { ref, onMounted } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
const files = filesStore(),
page = ref(0),
Expand Down
2 changes: 1 addition & 1 deletion src/views/user-space/SetupGuides.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
// External Modules
import { ref, onMounted } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
const instructions = instructionsStore(),
toast = toastStore(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/user-space/Verify.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// External Modules
import { onMounted, ref } from 'vue';
import { useOnline } from '@vueuse/core';
import { useOnline } from '@/utils/ConnectivityCheck';
import { useRouter } from 'vue-router';
const user = userStore(),
Expand Down
2 changes: 2 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//TODO: Add a way to extend the configuration without having to copy the whole file.

import { defineConfig } from 'vite';
import { VitePWA } from 'vite-plugin-pwa';
import vue from '@vitejs/plugin-vue';
Expand Down

0 comments on commit 0422bb9

Please sign in to comment.