Skip to content

feat(blog): store banners as static json asset #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions apps/blog/scripts/build-routes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const BASE_URL = process.env.AL_BASE_URL;
const SSG_ROUTES_FILE_PATH = 'apps/blog/routes.txt';
const SITEMAP_FILE_PATH = 'apps/blog/src/sitemap.xml';
const ROOT_PATHS_FILE_PREFIX = 'apps/blog/src/assets/root-paths';
const BANNERS_FILE_PREFIX = 'apps/blog/src/assets/banners';

const SUPPORTED_LANGUAGES = ['pl', 'en'];
const DEFAULT_LANGUAGE = 'en';
Expand Down Expand Up @@ -100,6 +101,37 @@ async function fetchAuthorRoutes(lang, skip = 0, take = 50) {
}
}

/**
* Fetches banners.
* @returns {Promise<void>}
*/
async function fetchBannersAndWriteToFile() {
const url = `${API_BASE_URL}/banners`;
try {
const data = await fetch(url).then((resp) => resp.json());

const stream = createWriteStream(`${BANNERS_FILE_PREFIX}.json`, {
encoding: 'utf-8',
});

stream.on('error', (error) => {
console.error('Error writing paths to file:', error);
});

try {
stream.write(JSON.stringify({ banners: data }));
} catch (error) {
console.error('Error during write operation:', error);
throw error;
} finally {
stream.end();
}
} catch (error) {
console.error('Failed to fetch banners');
throw error;
}
}

/**
* Appends static paths to the routes array for a given language.
* @param {"pl" | "en"} lang
Expand Down Expand Up @@ -182,6 +214,7 @@ async function main() {
await Promise.all([
...SUPPORTED_LANGUAGES.map((lang) => fetchArticleRoutes(lang)),
...SUPPORTED_LANGUAGES.map((lang) => fetchAuthorRoutes(lang)),
fetchBannersAndWriteToFile(),
]);

SUPPORTED_LANGUAGES.forEach((lang) => {
Expand Down
6 changes: 6 additions & 0 deletions apps/blog/src/assets/banners.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"banners": {
"slideDisplayTimeMs": 9000,
"slides": []
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { map } from 'rxjs';

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

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

getBannerSlider() {
return this._http.get<Slider>(`${this._apiBaseUrl}/banners`);
return this._http
.get<{ banners: Slider }>(`./assets/banners.json`)
.pipe(map((resp) => resp.banners));
}
}