From 8b6398907c71378abf8f6ccc1fb4a9bd95d72735 Mon Sep 17 00:00:00 2001 From: David LJ Date: Fri, 3 May 2024 14:01:41 +0200 Subject: [PATCH] fix: do not load simple icon if not an SVG (#433) --- .../simple-icon/simple-icon-loader.spec.ts | 22 +++++++++++++++++++ .../common/simple-icon/simple-icon-loader.ts | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/app/common/simple-icon/simple-icon-loader.spec.ts b/src/app/common/simple-icon/simple-icon-loader.spec.ts index 03d84097..8b5200e2 100644 --- a/src/app/common/simple-icon/simple-icon-loader.spec.ts +++ b/src/app/common/simple-icon/simple-icon-loader.spec.ts @@ -83,6 +83,28 @@ describe('SimpleIconLoader', () => { }) }) + // Can happen in PWA scenarios, where if static asset is not found, you get main index.html + describe('when response content is not svg', () => { + const flushWithTextHtmlContentType = (testRequest: TestRequest) => + testRequest.flush('', { + headers: { 'content-type': 'text/html; charset=utf-8' }, + }) + + it('should complete without emitting', (done) => { + let actualSvg: string | undefined + sut(ICON_SLUG).subscribe({ + next: (svg) => (actualSvg = svg), + complete: () => { + expect(actualSvg).toBeUndefined() + done() + }, + }) + + const testRequest = expectTestRequestToIcon(ICON_SLUG) + flushWithTextHtmlContentType(testRequest) + }) + }) + describe('when an error occurs', () => { const flushWithError = (testRequest: TestRequest) => testRequest.flush('', { diff --git a/src/app/common/simple-icon/simple-icon-loader.ts b/src/app/common/simple-icon/simple-icon-loader.ts index 422329f5..4b55ad68 100644 --- a/src/app/common/simple-icon/simple-icon-loader.ts +++ b/src/app/common/simple-icon/simple-icon-loader.ts @@ -1,7 +1,7 @@ import { inject, InjectionToken } from '@angular/core' import { isDevMode } from '@/common/is-dev-mode' import { HttpClient } from '@angular/common/http' -import { catchError, EMPTY, Observable, of, tap } from 'rxjs' +import { catchError, EMPTY, filter, Observable, of, tap } from 'rxjs' import { ASSETS_DIR } from '@/common/assets-dir' import { SIMPLE_ICONS_DIR } from '@/common/simple-icon/simple-icon.component' import { PLATFORM_SERVICE } from '@/common/platform.service' @@ -39,6 +39,7 @@ export const SIMPLE_ICON_LOADER = new InjectionToken( }, ) .pipe( + filter((svg) => svg.trimStart().startsWith(' cache.set(slug, svg)), catchError(() => EMPTY), )