Skip to content

Commit

Permalink
fix: do not load simple icon if not an SVG (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 authored May 3, 2024
1 parent 280e9f2 commit 8b63989
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/app/common/simple-icon/simple-icon-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<!DOCTYPE html>', {
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('', {
Expand Down
3 changes: 2 additions & 1 deletion src/app/common/simple-icon/simple-icon-loader.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -39,6 +39,7 @@ export const SIMPLE_ICON_LOADER = new InjectionToken<SimpleIconLoader>(
},
)
.pipe(
filter((svg) => svg.trimStart().startsWith('<svg')),
tap((svg) => cache.set(slug, svg)),
catchError(() => EMPTY),
)
Expand Down

0 comments on commit 8b63989

Please sign in to comment.