Closed deponovo closed 9 months ago
Hi,
I've tested your patch in material 9.5.2, but it doesn't seem to work correctly (produces errors in javascript window). Maybe we need to copy the return request(url, options)
part of the original source to the } else {
part. What I mean is:
this:
} else {
return request(url, options)
.pipe(
switchMap(res => res.text()),
map(body => JSON.parse(body) as T),
shareReplay(1)
)
}
instead of that:
} else {
return request(url, options)
.pipe(
switchMap(res => res.json()),
shareReplay(1)
)
}
It would be good to also check out @ttran2's patches. Maybe they are a better/cleaner way, but I didn't dive into it, yet.
I am not a javascript developer. I can try your suggestion, but I can't review the patches from ttran2.
The abomination of a language in which material is coded is called typescript. Looks like we need to mimic whatever map(body => JSON.parse(body) as T),
does...
This patch fixes it for me, please test and add this to your pull request:
--- src/templates/assets/javascripts/browser/request/index.ts 2023-12-11 16:21:03.000000000 +0100
+++ src/templates/assets/javascripts/browser/request/index.ts.encryptcontent 2023-12-16 19:57:45.611514178 +0100
@@ -24,6 +24,9 @@
Observable,
Subject,
map,
+ tap,
+ of,
+ throwError,
shareReplay,
switchMap
} from "rxjs"
@@ -117,12 +120,41 @@
export function requestJSON<T>(
url: URL | string, options?: Options
): Observable<T> {
- return request(url, options)
- .pipe(
- switchMap(res => res.text()),
- map(body => JSON.parse(body) as T),
- shareReplay(1)
- )
+ /* Hack to enable search Index decryption */
+ if (url.href.endsWith('/search_index.json')) {
+ const encryptcontentIndexKey = 'encryptcontent-index';
+ const encryptcontentIndex = sessionStorage.getItem(encryptcontentIndexKey);
+
+ if (encryptcontentIndex) {
+ // if already available in sessionStorage return Observable JSON data
+ try {
+ const parsedData = JSON.parse(encryptcontentIndex) as T;
+ return of(parsedData);
+ } catch (error) {
+ console.error('Error parsing encryptcontent-index:', error);
+ return throwError(error);
+ }
+ } else {
+ // if not yet available in sessionStorage request JSON data
+ return request(url, options).pipe(
+ switchMap(res => res.text()),
+ map(body => JSON.parse(body) as T),
+ tap((jsonData) => {
+ // and save them to sessionstorage
+ sessionStorage.setItem(encryptcontentIndexKey, JSON.stringify(jsonData));
+ }),
+ shareReplay(1)
+ );
+ }
+ } else {
+ return request(url, options)
+ .pipe(
+ switchMap(res => res.text()),
+ map(body => JSON.parse(body) as T),
+ shareReplay(1)
+ )
+ }
}
/**
Tested and working
Closes #56