Skip to content

Commit 545a81d

Browse files
feat(lib): lazy url config
1 parent 16c6206 commit 545a81d

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This library covers next aspects that developers should consider for their proje
1313
- SVG reusability
1414
- Optimized bundle size
1515
- SSR
16-
- Edge ready (only edge save APIs are used)
16+
- Edge ready (only edge safe APIs are used)
1717

1818
## Getting started
1919

@@ -103,7 +103,8 @@ During setup phase you can provide additional optional settings such as:
103103
svgLoadStrategy?: Type<SvgLoadStrategy>;
104104
```
105105

106-
`svgLoadStrategy` can be any injectable class that has `load` method that accepts url and returns observable string:
106+
`svgLoadStrategy` can be any injectable class that has `config` that excepts method that accepts url and returns observable string,
107+
and `load` which accepts the configured url as an observable and returns the svg as an observable string.
107108

108109
```typescript
109110
@Injectable()
@@ -196,6 +197,36 @@ And then just provide it in you server module.
196197
export class AppServerModule {}
197198
```
198199

200+
#### Providing a lazy configuration
201+
202+
If you need to provide a lazy configuration you can use the config method in the `SvgLoadStrategy`:
203+
204+
```typescript
205+
@Injectable()
206+
class LazyConfigSvgLoadStrategy extends SvgLoadStrategyImpl {
207+
dummyLazyConfig$ = timer(5_000).pipe(map(() => 'assets/svg-icons'))
208+
override config(url: string): Observable<string> {
209+
return this.dummyLazyConfig$.pipe(map((svgConfig) => `${svgConfig}/${url}`));
210+
}
211+
}
212+
```
213+
214+
And pass it to the provider function:
215+
216+
```typescript
217+
import { provideFastSVG } from '@push-based/ngx-fast-svg';
218+
219+
bootstrapApplication(AppComponent, {
220+
providers: [
221+
// ... other providers
222+
provideFastSVG({
223+
url: (name: string) => `${name}.svg`,
224+
svgLoadStrategy: LazyConfigSvgLoadStrategy,
225+
})
226+
]
227+
});
228+
```
229+
199230
## Features
200231

201232
### :sloth: Lazy loading for SVGs
@@ -273,4 +304,3 @@ To display (render) SVGs the browser takes time. We can reduce that time by addi
273304
---
274305

275306
made with ❤ by [push-based.io](https://www.push-based.io)
276-

packages/ngx-fast-lib/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This library covers next aspects that developers should consider for their proje
1313
- SVG reusability
1414
- Optimized bundle size
1515
- SSR
16+
- Edge ready (only edge safe APIs are used)
1617

1718
## Getting started
1819

@@ -166,7 +167,9 @@ You can provide your own SSR loading strategy that can look like this:
166167
```typescript
167168
@Injectable()
168169
export class SvgLoadStrategySsr implements SvgLoadStrategy {
169-
config = (url: string) => of(join(cwd(), 'path', 'to', 'svg', 'assets', url));
170+
config(url: string) {
171+
return of(join(cwd(), 'path', 'to', 'svg', 'assets', url));
172+
}
170173
load(iconPath$: Observable<string>) {
171174
return iconPath$.pipe(switchMap((iconPath) => from(readFile(iconPath, { encoding: 'utf8' }))));
172175
}
@@ -203,9 +206,9 @@ If you need to provide a lazy configuration you can use the config method in the
203206
```typescript
204207
@Injectable()
205208
class LazyConfigSvgLoadStrategy extends SvgLoadStrategyImpl {
206-
lazyConfigService = inject(SvgConfigService);
207-
override config(url: string) {
208-
return this.lazyConfigService.urlConfig(url);
209+
dummyLazyConfig$ = timer(5_000).pipe(map(() => 'assets/svg-icons'))
210+
override config(url: string): Observable<string> {
211+
return this.dummyLazyConfig$.pipe(map((svgConfig) => `${svgConfig}/${url}`));
209212
}
210213
}
211214
```
@@ -219,7 +222,7 @@ bootstrapApplication(AppComponent, {
219222
providers: [
220223
// ... other providers
221224
provideFastSVG({
222-
url: (name: string): Observable<string> => inject(ConfigService).svgUrl(name),
225+
url: (name: string) => `${name}.svg`,
223226
svgLoadStrategy: LazyConfigSvgLoadStrategy,
224227
})
225228
]

0 commit comments

Comments
 (0)