Skip to content
Merged
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
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"vue-material-design-icons": "^5.4.0",
"vue-router": "^5.5.0",
"vue-virtual-scroller": "^3.0.5",
"workbox-cacheable-response": "^7.4.1",
"workbox-core": "^7.4.1",
"workbox-expiration": "^7.4.1",
"workbox-precaching": "^7.4.1",
Expand Down
15 changes: 12 additions & 3 deletions src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

import type { RouteMatchCallbackOptions } from 'workbox-core'

import { CacheableResponsePlugin } from 'workbox-cacheable-response'
import { clientsClaim } from 'workbox-core'
import { ExpirationPlugin } from 'workbox-expiration'
import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching'
import { registerRoute } from 'workbox-routing'
import { NetworkFirst } from 'workbox-strategies'
import { isCacheableNavigation, navigationCacheKey } from './util/serviceWorkerNavigation.ts'

declare let self: ServiceWorkerGlobalScope

Expand All @@ -33,25 +35,31 @@ precacheAndRoute(itemsToPrecache)
// clean old assets
cleanupOutdatedCaches()

// Fall back to the cache if the server does not respond in time
const NETWORK_TIMEOUT_SECONDS = 5

/**
* Route match callback function for `collectives-vue-router`
* Matches navigation requests within the vue router scope
*
* @param matchOptions options
* @param matchOptions.url URL to match
* @param matchOptions.request request to match
*/
function matchNavigateCb({ url, request }: RouteMatchCallbackOptions) {
return request.mode === 'navigate' && url.pathname.match(/(?:\/index.php)?\/apps\/collectives/)
return isCacheableNavigation(url, request.mode)
}

registerRoute(matchNavigateCb, new NetworkFirst({
cacheName: 'collectives-vue-router',
networkTimeoutSeconds: NETWORK_TIMEOUT_SECONDS,
plugins: [
// Don't cache redirects (e.g. to the login page) or error pages in place of the app
new CacheableResponsePlugin({ statuses: [200] }),
new ExpirationPlugin({
maxAgeSeconds: 3600 * 24 * 7, // one week
}),
{
cacheKeyWillBeUsed: async () => '/apps/collectives',
cacheKeyWillBeUsed: async ({ request }) => navigationCacheKey(new URL(request.url)),
},
],
}))
Expand All @@ -73,6 +81,7 @@ function matchAssetsCb({ url }: RouteMatchCallbackOptions) {
// Cache for assets from other apps
registerRoute(matchAssetsCb, new NetworkFirst({
cacheName: 'collectives-assets',
networkTimeoutSeconds: NETWORK_TIMEOUT_SECONDS,
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 3600 * 24 * 7, // one week
Expand Down
56 changes: 56 additions & 0 deletions src/tests/util/serviceWorkerNavigation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { describe, expect, it } from 'vitest'
import { isCacheableNavigation, navigationCacheKey } from '../../util/serviceWorkerNavigation.ts'

const url = (path: string) => new URL(`https://cloud.example${path}`)

describe('isCacheableNavigation', () => {
it.each([
['app', '/index.php/apps/collectives/'],
['app with pretty URLs', '/apps/collectives/'],
['page', '/index.php/apps/collectives/Garden-12/Roses-34'],
['public share', '/index.php/apps/collectives/p/abc123/'],
['page in public share', '/index.php/apps/collectives/p/abc123/Garden/Roses'],
])('caches navigation to %s', (_, path) => {
expect(isCacheableNavigation(url(path), 'navigate')).toBe(true)
})

it.each([
['public share password form', '/index.php/apps/collectives/p/abc123/authenticate/showshare'],
['public share password form with pretty URLs', '/apps/collectives/p/abc123/authenticate/showshare'],
['other app', '/index.php/apps/files/'],
])('does not cache navigation to %s', (_, path) => {
expect(isCacheableNavigation(url(path), 'navigate')).toBe(false)
})

it('ignores requests that are not navigations', () => {
expect(isCacheableNavigation(url('/index.php/apps/collectives/'), 'cors')).toBe(false)
})
})

describe('navigationCacheKey', () => {
it.each([
['/index.php/apps/collectives/'],
['/apps/collectives'],
['/index.php/apps/collectives/Garden-12/Roses-34'],
])('uses one key for all app routes (%s)', (path) => {
expect(navigationCacheKey(url(path))).toBe('/apps/collectives')
})

it.each([
['/index.php/apps/collectives/p/abc123'],
['/index.php/apps/collectives/p/abc123/'],
['/apps/collectives/p/abc123/Garden/Roses'],
])('uses one key per public share (%s)', (path) => {
expect(navigationCacheKey(url(path))).toBe('/apps/collectives/p/abc123')
})

it('separates different public shares', () => {
expect(navigationCacheKey(url('/index.php/apps/collectives/p/abc123/')))
.not.toBe(navigationCacheKey(url('/index.php/apps/collectives/p/def456/')))
})
})
38 changes: 38 additions & 0 deletions src/util/serviceWorkerNavigation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

const appPath = /(?:\/index.php)?\/apps\/collectives/
const sharePath = /\/apps\/collectives\/p\/([^/]+)/
const shareAuthenticatePath = /\/apps\/collectives\/p\/[^/]+\/authenticate(\/|$)/

/**
* Whether the service worker caches the page for a navigation request
*
* Matches navigation requests within the app,
* except for the password form of public shares.
*
* @param url URL of the request
* @param mode mode of the request
*/
export function isCacheableNavigation(url: URL, mode: RequestMode): boolean {
return mode === 'navigate'
&& appPath.test(url.pathname)
&& !shareAuthenticatePath.test(url.pathname)
}

/**
* Cache key for a navigation request
*
* All routes of the app are served by the same page, so they share one cache entry.
* Each public share gets its own entry, so it never replaces the app or another share.
*
* @param url URL of the request
*/
export function navigationCacheKey(url: URL): string {
const shareToken = url.pathname.match(sharePath)?.[1]
return shareToken
? `/apps/collectives/p/${shareToken}`
: '/apps/collectives'
}
Loading