From 7bf94e1be7bf3a3200e0cb3575625a4b2ae76777 Mon Sep 17 00:00:00 2001 From: jamil314 Date: Mon, 23 Dec 2024 16:00:35 +0600 Subject: [PATCH] fix: use minio regex from commons in client service worker --- packages/client/src/src-sw.ts | 8 ++------ .../src/views/PrintCertificate/usePrintableCertificate.ts | 5 ++--- packages/commons/src/documents.ts | 5 +++++ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/client/src/src-sw.ts b/packages/client/src/src-sw.ts index e30e80f4dec..1de4ae86d5c 100644 --- a/packages/client/src/src-sw.ts +++ b/packages/client/src/src-sw.ts @@ -17,6 +17,7 @@ import { import { registerRoute, NavigationRoute } from 'workbox-routing' import { NetworkFirst, CacheFirst } from 'workbox-strategies' import { clientsClaim } from 'workbox-core' +import { MINIO_REGEX } from '@opencrvs/commons/client' declare let self: ServiceWorkerGlobalScope @@ -79,12 +80,7 @@ registerRoute(/http(.+)conditionals\.js$/, new NetworkFirst()) registerRoute(/http(.+)config$/, new NetworkFirst()) // This caches the minio urls -registerRoute( - import.meta.env.DEV - ? /http(.+)localhost:3535\/ocrvs\/.+/ - : /http(.+)minio\.(.+)\/.*ocrvs.*\/.+/, - new CacheFirst() -) +registerRoute(MINIO_REGEX, new CacheFirst()) /* * Alternate for navigateFallback & navigateFallbackBlacklist diff --git a/packages/client/src/views/PrintCertificate/usePrintableCertificate.ts b/packages/client/src/views/PrintCertificate/usePrintableCertificate.ts index c7215819bd5..6ab9328719c 100644 --- a/packages/client/src/views/PrintCertificate/usePrintableCertificate.ts +++ b/packages/client/src/views/PrintCertificate/usePrintableCertificate.ts @@ -50,10 +50,9 @@ import { import { useNavigate } from 'react-router-dom' import { ICertificateData } from '@client/utils/referenceApi' import { fetchImageAsBase64 } from '@client/utils/imageUtils' +import { isMinioUrl } from '@opencrvs/commons' async function replaceMinioUrlWithBase64(template: Record) { - const regex = /\/[^\/?]+\.(jpg|png|jpeg|svg)(?=\?|$)/i - async function recursiveTransform(obj: any) { if (typeof obj !== 'object' || obj === null) { return obj @@ -63,7 +62,7 @@ async function replaceMinioUrlWithBase64(template: Record) { for (const key in obj) { const value = obj[key] - if (typeof value === 'string' && regex.test(value)) { + if (typeof value === 'string' && isMinioUrl(value)) { transformedObject[key] = await fetchImageAsBase64(value) } else if (typeof value === 'object') { transformedObject[key] = await recursiveTransform(value) diff --git a/packages/commons/src/documents.ts b/packages/commons/src/documents.ts index 4277f0fa5a7..1e906b56ad2 100644 --- a/packages/commons/src/documents.ts +++ b/packages/commons/src/documents.ts @@ -8,6 +8,9 @@ * * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. */ + +export const MINIO_REGEX = /\/[^\/?]+\.(jpg|png|jpeg|svg)(?=\?|$)/i + export function isBase64FileString(str: string) { if (str === '' || str.trim() === '') { return false @@ -15,3 +18,5 @@ export function isBase64FileString(str: string) { const strSplit = str.split(':') return strSplit.length > 0 && strSplit[0] === 'data' } + +export const isMinioUrl = (url: string) => MINIO_REGEX.test(url)