️(service-worker) manage document versioning

- Cache correctly the document version to work
in offline mode.
- Restoring a version is possible even if offline.
- Better sync triggering.
This commit is contained in:
Anthony LC
2024-08-06 14:50:02 +02:00
committed by Anthony LC
parent fb494c8c71
commit 2b824c0862
4 changed files with 30 additions and 8 deletions

View File

@@ -65,7 +65,7 @@ const VersionListState = ({
{error && ( {error && (
<Box <Box
$justify="center" $justify="center"
$margin={{ vertical: 'big', horizontal: 'auto' }} $margin={{ vertical: 'small', horizontal: 'small' }}
> >
<TextErrors <TextErrors
causes={error.cause} causes={error.cause}

View File

@@ -15,7 +15,13 @@ interface OptionsMutate {
type: 'update' | 'delete' | 'create'; type: 'update' | 'delete' | 'create';
} }
type Options = (OptionsReadonly | OptionsMutate) & { syncManager: SyncManager }; interface OptionsSync {
type: 'synch';
}
type Options = (OptionsReadonly | OptionsMutate | OptionsSync) & {
syncManager: SyncManager;
};
export class ApiPlugin implements WorkboxPlugin { export class ApiPlugin implements WorkboxPlugin {
private readonly options: Options; private readonly options: Options;

View File

@@ -28,7 +28,8 @@ export const isApiUrl = (href: string) => {
*/ */
registerRoute( registerRoute(
({ url }) => ({ url }) =>
isApiUrl(url.href) && url.href.match(/.*\/documents\/\?(page|ordering)=.*/), isApiUrl(url.href) &&
url.href.match(/.*\/documents\/\?(page|ordering)=.*/g),
new NetworkOnly({ new NetworkOnly({
plugins: [ plugins: [
new ApiPlugin({ new ApiPlugin({
@@ -42,7 +43,8 @@ registerRoute(
); );
registerRoute( registerRoute(
({ url }) => isApiUrl(url.href) && url.href.match(/.*\/documents\/.*\//), ({ url }) =>
isApiUrl(url.href) && url.href.match(/.*\/documents\/([a-z0-9\-]+)\/$/g),
new NetworkOnly({ new NetworkOnly({
plugins: [ plugins: [
new ApiPlugin({ new ApiPlugin({
@@ -56,7 +58,8 @@ registerRoute(
); );
registerRoute( registerRoute(
({ url }) => isApiUrl(url.href) && url.href.match(/.*\/documents\/.*\//), ({ url }) =>
isApiUrl(url.href) && url.href.match(/.*\/documents\/([a-z0-9\-]+)\/$/g),
new NetworkOnly({ new NetworkOnly({
plugins: [ plugins: [
new ApiPlugin({ new ApiPlugin({
@@ -69,7 +72,7 @@ registerRoute(
); );
registerRoute( registerRoute(
({ url }) => isApiUrl(url.href) && url.href.match(/.*\/documents\//), ({ url }) => isApiUrl(url.href) && url.href.match(/.*\/documents\/$/g),
new NetworkOnly({ new NetworkOnly({
plugins: [ plugins: [
new ApiPlugin({ new ApiPlugin({
@@ -82,7 +85,8 @@ registerRoute(
); );
registerRoute( registerRoute(
({ url }) => isApiUrl(url.href) && url.href.match(/.*\/documents\/.*\//), ({ url }) =>
isApiUrl(url.href) && url.href.match(/.*\/documents\/([a-z0-9\-]+)\/$/g),
new NetworkOnly({ new NetworkOnly({
plugins: [ plugins: [
new ApiPlugin({ new ApiPlugin({
@@ -103,6 +107,10 @@ registerRoute(
new ExpirationPlugin({ new ExpirationPlugin({
maxAgeSeconds: 24 * 60 * 60 * DAYS_EXP, maxAgeSeconds: 24 * 60 * 60 * DAYS_EXP,
}), }),
new ApiPlugin({
type: 'synch',
syncManager,
}),
], ],
}), }),
'GET', 'GET',

View File

@@ -87,6 +87,7 @@ self.addEventListener('activate', function (event) {
const FALLBACK = { const FALLBACK = {
offline: '/offline/', offline: '/offline/',
docs: '/docs/[id]/', docs: '/docs/[id]/',
versions: '/docs/[id]/versions/[versionId]/',
images: '/assets/img-not-found.svg', images: '/assets/img-not-found.svg',
}; };
const precacheResources = [ const precacheResources = [
@@ -99,6 +100,7 @@ const precacheResources = [
FALLBACK.offline, FALLBACK.offline,
FALLBACK.images, FALLBACK.images,
FALLBACK.docs, FALLBACK.docs,
FALLBACK.versions,
]; ];
const precacheStrategy = getStrategy({ const precacheStrategy = getStrategy({
@@ -117,8 +119,14 @@ setCatchHandler(async ({ request, url, event }) => {
return ApiPlugin.getApiCatchHandler(); return ApiPlugin.getApiCatchHandler();
case request.destination === 'document': case request.destination === 'document':
if (url.pathname.match(/^\/docs\/.*\//)) { if (url.pathname.match(/^\/docs\/([a-z0-9\-]+)\/$/g)) {
return precacheStrategy.handle({ event, request: FALLBACK.docs }); return precacheStrategy.handle({ event, request: FALLBACK.docs });
} else if (
url.pathname.match(
/^\/docs\/([a-z0-9\-]+)\/versions\/([a-z0-9\-]+)\/$/g,
)
) {
return precacheStrategy.handle({ event, request: FALLBACK.versions });
} }
return precacheStrategy.handle({ event, request: FALLBACK.offline }); return precacheStrategy.handle({ event, request: FALLBACK.offline });