Skip to content

Commit 8ae05c0

Browse files
authored
feat(blog): store banners as static json asset (#415)
1 parent f26f924 commit 8ae05c0

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

apps/blog/scripts/build-routes.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const BASE_URL = process.env.AL_BASE_URL;
77
const SSG_ROUTES_FILE_PATH = 'apps/blog/routes.txt';
88
const SITEMAP_FILE_PATH = 'apps/blog/src/sitemap.xml';
99
const ROOT_PATHS_FILE_PREFIX = 'apps/blog/src/assets/root-paths';
10+
const BANNERS_FILE_PREFIX = 'apps/blog/src/assets/banners';
1011

1112
const SUPPORTED_LANGUAGES = ['pl', 'en'];
1213
const DEFAULT_LANGUAGE = 'en';
@@ -100,6 +101,37 @@ async function fetchAuthorRoutes(lang, skip = 0, take = 50) {
100101
}
101102
}
102103

104+
/**
105+
* Fetches banners.
106+
* @returns {Promise<void>}
107+
*/
108+
async function fetchBannersAndWriteToFile() {
109+
const url = `${API_BASE_URL}/banners`;
110+
try {
111+
const data = await fetch(url).then((resp) => resp.json());
112+
113+
const stream = createWriteStream(`${BANNERS_FILE_PREFIX}.json`, {
114+
encoding: 'utf-8',
115+
});
116+
117+
stream.on('error', (error) => {
118+
console.error('Error writing paths to file:', error);
119+
});
120+
121+
try {
122+
stream.write(JSON.stringify({ banners: data }));
123+
} catch (error) {
124+
console.error('Error during write operation:', error);
125+
throw error;
126+
} finally {
127+
stream.end();
128+
}
129+
} catch (error) {
130+
console.error('Failed to fetch banners');
131+
throw error;
132+
}
133+
}
134+
103135
/**
104136
* Appends static paths to the routes array for a given language.
105137
* @param {"pl" | "en"} lang
@@ -182,6 +214,7 @@ async function main() {
182214
await Promise.all([
183215
...SUPPORTED_LANGUAGES.map((lang) => fetchArticleRoutes(lang)),
184216
...SUPPORTED_LANGUAGES.map((lang) => fetchAuthorRoutes(lang)),
217+
fetchBannersAndWriteToFile(),
185218
]);
186219

187220
SUPPORTED_LANGUAGES.forEach((lang) => {

apps/blog/src/assets/banners.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"banners": {
3+
"slideDisplayTimeMs": 9000,
4+
"slides": []
5+
}
6+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { HttpClient } from '@angular/common/http';
22
import { inject, Injectable } from '@angular/core';
3+
import { map } from 'rxjs';
34

45
import { Slider } from '@angular-love/blog/contracts/banners';
5-
import { ConfigService } from '@angular-love/shared/config';
66

77
@Injectable({ providedIn: 'root' })
88
export class AdBannerService {
9-
private readonly _apiBaseUrl = inject(ConfigService).get('apiBaseUrl');
109
private readonly _http = inject(HttpClient);
1110

1211
getBannerSlider() {
13-
return this._http.get<Slider>(`${this._apiBaseUrl}/banners`);
12+
return this._http
13+
.get<{ banners: Slider }>(`./assets/banners.json`)
14+
.pipe(map((resp) => resp.banners));
1415
}
1516
}

0 commit comments

Comments
 (0)