Skip to content

Commit

Permalink
fix: use minio regex from commons in client service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
jamil314 committed Dec 27, 2024
1 parent f02f06c commit 7bf94e1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
8 changes: 2 additions & 6 deletions packages/client/src/src-sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>) {
const regex = /\/[^\/?]+\.(jpg|png|jpeg|svg)(?=\?|$)/i

async function recursiveTransform(obj: any) {
if (typeof obj !== 'object' || obj === null) {
return obj
Expand All @@ -63,7 +62,7 @@ async function replaceMinioUrlWithBase64(template: Record<string, any>) {

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)
Expand Down
5 changes: 5 additions & 0 deletions packages/commons/src/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
*
* 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
}
const strSplit = str.split(':')
return strSplit.length > 0 && strSplit[0] === 'data'
}

export const isMinioUrl = (url: string) => MINIO_REGEX.test(url)

0 comments on commit 7bf94e1

Please sign in to comment.