Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Jan 19, 2025
1 parent f7722ab commit c2fd4ef
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
23 changes: 18 additions & 5 deletions backend/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ route.GET('/latestHEADinfo/{contractID}', {
})

route.GET('/time', {}, function (request, h) {
return new Date().toISOString()
return h.response(new Date().toISOString())
.header('Cache-Control', 'no-store')
})

// TODO: if the browser deletes our cache then not everyone
Expand Down Expand Up @@ -713,7 +714,10 @@ route.GET('/assets/{subpath*}', {
.etag(basename)
.header('Cache-Control', 'public,max-age=31536000,immutable')
}
// Files like `main.js` or `main.css` should be revalidated before use. Se we use the default headers.
// Files like `main.js` or `main.css` should be revalidated before use.
// We set a short 'stale-while-revalidate' value instead of 'no-cache' to
// signal to the app that it's fine to use old versions when offline or over
// unreliable connections.
// This should also be suitable for serving unversioned fonts and images.
return h.file(subpath)
.header('Cache-Control', 'public,max-age=604800,stale-while-revalidate=86400')
Expand Down Expand Up @@ -805,7 +809,13 @@ route.GET('/zkpp/{name}/auth_hash', {
try {
const challenge = await getChallenge(req.params['name'], req.query['b'])

return challenge || Boom.notFound()
if (!challenge) {
return Boom.notFound()
}

return h.response(challenge)
.header('Cache-Control', 'no-store')
.header('Content-Type', 'text/plain')
} catch (e) {
e.ip = req.headers['x-real-ip'] || req.info.remoteAddress
console.error(e, 'Error at GET /zkpp/{name}/auth_hash: ' + e.message)
Expand All @@ -829,7 +839,9 @@ route.GET('/zkpp/{name}/contract_hash', {
const salt = await getContractSalt(req.params['name'], req.query['r'], req.query['s'], req.query['sig'], req.query['hc'])

if (salt) {
return salt
return h.response(salt)
.header('Cache-Control', 'no-store')
.header('Content-Type', 'text/plain')
}
} catch (e) {
e.ip = req.headers['x-real-ip'] || req.info.remoteAddress
Expand All @@ -855,7 +867,8 @@ route.POST('/zkpp/{name}/updatePasswordHash', {
const result = await updateContractSalt(req.params['name'], req.payload['r'], req.payload['s'], req.payload['sig'], req.payload['hc'], req.payload['Ea'])

if (result) {
return result
return h.response(result)
.header('Content-type', 'text/plain')
}
} catch (e) {
e.ip = req.headers['x-real-ip'] || req.info.remoteAddress
Expand Down
2 changes: 1 addition & 1 deletion frontend/controller/actions/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ export default (sbp('sbp/selectors/register', {
removeEventHandler()
removeLogoutHandler()
// The event handler recursively calls this same selector
// A different path should be taken, since te event handler
// A different path should be taken, since the event handler
// should be called after the key request has been answered
// and processed
sbp('gi.actions/group/join', params).catch((e) => {
Expand Down
1 change: 1 addition & 0 deletions frontend/controller/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ sbp('sbp/selectors/register', {
// have this function there. However, most examples perform this outside of the
// SW, and private testing showed that it's more reliable doing it here.
'service-worker/setup-push-subscription': async function (retryCount?: number) {
if (process.env.CI || window.Cypress) return
await sbp('okTurtles.eventQueue/queueEvent', 'service-worker/setup-push-subscription', async () => {
// Get the installed service-worker registration
const registration = await navigator.serviceWorker.ready
Expand Down
36 changes: 32 additions & 4 deletions frontend/controller/serviceworkers/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,18 @@ if (
return
}

if (
['/eventsAfter/', '/name/', '/latestHEADinfo/', '/file/', '/kv/', '/zkpp/'].some(prefix => url.pathname.startsWith(prefix)) ||
url.pathname === '/time'
) {
return
}

// If the route starts with `${routerBase}/`, use `${routerBase}/` as the
// URL, since the HTML content is presumed to be the same.
if (url.pathname.startsWith(`${routerBase}/`)) {
request = new Request(`${routerBase}/`)
console.error('@@@@XX rewriting request', url.pathname, `${url.origin}${routerBase}/`)
request = new Request(`${url.origin}${routerBase}/dashboard`)
}
} catch (e) {
return
Expand All @@ -93,16 +101,25 @@ if (
event.respondWith(
caches.open(CURRENT_CACHES.assets).then((cache) => {
return cache
.match(request)
.match(request, { ignoreSearch: true, ignoreVary: true })
.then((cachedResponse) => {
if (request instanceof URL) {
console.error('@@@@XX', 106, request)
}
if (cachedResponse) {
// If we're offline, return the cached response, if it exists
if (navigator.onLine === false) {
if (request instanceof URL) {
console.error('@@@@XX', 112, request, navigator.onLine)
}
return cachedResponse
}
}

return fetch(request.clone()).then(async (response) => {
return fetch(request.clone?.() || request).then(async (response) => {
if (request instanceof URL) {
console.error('@@@@XX', 120, request, response.status)
}
if (
// Save successful reponses
response.status >= 200 &&
Expand All @@ -119,12 +136,23 @@ if (
await cache.delete(request)
}

if (request instanceof URL) {
console.error('@@@@XX', 139, request, response.status)
}

return response
}).catch(e => {
if (request instanceof URL) {
console.error('@@@@XX', 145, request, e)
}

if (cachedResponse) {
console.warn('Error while fetching', request.url, e)
console.warn('Error while fetching', request, e)
// If there was a network error fetching, return the cached
// response, if it exists
if (request instanceof URL) {
console.error('@@@@XX', 153, request, e)
}
return cachedResponse
}

Expand Down
2 changes: 2 additions & 0 deletions frontend/views/pages/PendingApproval.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export default ({
// side-effects. In general, UI elements should not be changing
// non-ephemeral references.
await sbp('chelonia/contract/retain', this.ephemeral.groupIdWhenMounted, { ephemeral: true })
await sbp('okTurtles.eventQueue/queueEvent', 'event-handled', () => {})
await new Promise(resolve => setTimeout(resolve, 200))
this.ephemeral.contractFinishedSyncing = true
if (this.haveActiveGroupProfile) {
this.ephemeral.groupJoined = true
Expand Down

0 comments on commit c2fd4ef

Please sign in to comment.