📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Refresh blog posts when adding a new one
Blue Mouse3e357456bb0a
diff --git a/backend/.env.example b/backend/.env.exampleindex f6f55a852..3658a7483 100644--- a/backend/.env.example+++ b/backend/.env.example@@ -151,6 +151,8 @@ RANKPINE_BLOG_AUTHOR='Andrii Romasiun'RANKPINE_BLOG_TWITTER_HANDLE=andrii_romRANKPINE_BLOG_COMMITTER_NAME='Swetrix Content Bot'+# Delay before the live checkout pulls the new blog commit from GitHub.+RANKPINE_BLOG_REFRESH_DELAY_MS=30000# Managed reverse proxy ("Bypass adblockers") edge auth# Shared secret the proxy.swetrix.org edge box sends in `X-Edge-Api-Key` todiff --git a/backend/apps/cloud/src/blog/blog.service.ts b/backend/apps/cloud/src/blog/blog.service.tsindex 0e42fe961..6426ff9bf 100644--- a/backend/apps/cloud/src/blog/blog.service.ts+++ b/backend/apps/cloud/src/blog/blog.service.ts@@ -201,6 +201,10 @@ const getArticlesMetaData = async () => {export class BlogService {constructor(private readonly logger: AppLoggerService) {}+ async clearSitemapCache(): Promise<void> {+ await redis.del(REDIS_SITEMAP_KEY)+ }+async getArticleBySlug(slug: string, category?: string) {if (!slug) {return nulldiff --git a/backend/apps/cloud/src/webhook/rankpine-blog.service.spec.ts b/backend/apps/cloud/src/webhook/rankpine-blog.service.spec.tsindex 5ed812259..602bc9380 100644--- a/backend/apps/cloud/src/webhook/rankpine-blog.service.spec.ts+++ b/backend/apps/cloud/src/webhook/rankpine-blog.service.spec.ts@@ -2,6 +2,7 @@ import crypto from 'crypto'import { UnauthorizedException } from '@nestjs/common'import { AppLoggerService } from '../logger/logger.service'+import { BlogService } from '../blog/blog.service'import { RankPineBlogService } from './rankpine-blog.service'const SECRET = 'rankpine-test-secret'@@ -17,6 +18,7 @@ const ENV_KEYS = ['RANKPINE_BLOG_TWITTER_HANDLE','RANKPINE_BLOG_COMMITTER_NAME','RANKPINE_BLOG_COMMITTER_EMAIL',+ 'RANKPINE_BLOG_REFRESH_DELAY_MS',] as constconst ORIGINAL_ENV = Object.fromEntries(ENV_KEYS.map((key) => [key, process.env[key]]),@@ -38,7 +40,10 @@ describe('RankPineBlogService', () => {error: jest.fn(),log: jest.fn(),} as unknown as AppLoggerService- const service = new RankPineBlogService(logger)+ const blogService = {+ clearSitemapCache: jest.fn(),+ } as unknown as BlogService+ const service = new RankPineBlogService(logger, blogService)beforeEach(() => {process.env.RANKPINE_BLOG_WEBHOOK_SECRET = SECRET@@ -52,6 +57,7 @@ describe('RankPineBlogService', () => {process.env.RANKPINE_BLOG_TWITTER_HANDLE = 'andrii_rom'process.env.RANKPINE_BLOG_COMMITTER_NAME = 'Swetrix Content Bot'process.env.RANKPINE_BLOG_COMMITTER_EMAIL = '[email protected]'+ process.env.RANKPINE_BLOG_REFRESH_DELAY_MS = '30000'jest.clearAllMocks()})@@ -91,6 +97,15 @@ describe('RankPineBlogService', () => {})it('creates one idempotent bot-authored post and strips the duplicate H1', async () => {+ jest.useFakeTimers()+ const refreshSpy = jest+ .spyOn(+ service as unknown as {+ refreshLocalBlogPosts: () => Promise<void>+ },+ 'refreshLocalBlogPosts',+ )+ .mockResolvedValue()const files = new Map<string, string>([['posts/2026-08-01-older-automated-post.md',@@ -221,7 +236,13 @@ describe('RankPineBlogService', () => {'',].join('\n'),)+ jest.advanceTimersByTime(29_999)+ expect(refreshSpy).not.toHaveBeenCalled()+ jest.advanceTimersByTime(1)+ expect(refreshSpy).toHaveBeenCalledTimes(1)+ refreshSpy.mockRestore()fetchMock.mockRestore()+ jest.useRealTimers()})})diff --git a/backend/apps/cloud/src/webhook/rankpine-blog.service.ts b/backend/apps/cloud/src/webhook/rankpine-blog.service.tsindex 739808ba0..3d13b6997 100644--- a/backend/apps/cloud/src/webhook/rankpine-blog.service.ts+++ b/backend/apps/cloud/src/webhook/rankpine-blog.service.ts@@ -1,4 +1,7 @@import crypto from 'crypto'+import { execFile } from 'child_process'+import path from 'path'+import { promisify } from 'util'import {BadGatewayException,BadRequestException,@@ -9,6 +12,7 @@ import {import { z } from 'zod'import { AppLoggerService } from '../logger/logger.service'+import { BlogService } from '../blog/blog.service'const GITHUB_API_URL = 'https://api.github.com'const GITHUB_API_VERSION = '2026-03-10'@@ -21,6 +25,9 @@ const DEFAULT_AUTHOR = 'Andrii Romasiun'const DEFAULT_TWITTER_HANDLE = 'andrii_rom'const DEFAULT_COMMITTER_NAME = 'Swetrix Content Bot'const DEFAULT_COMMITTER_EMAIL = '[email protected]'+const DEFAULT_REFRESH_DELAY_MS = 30_000+const MAX_REFRESH_DELAY_MS = 300_000+const execFileAsync = promisify(execFile)const verificationSchema = z.object({event: z.literal('verification'),@@ -60,6 +67,7 @@ interface PublisherConfig {name: stringemail: string}+ refreshDelayMs: number}interface GitHubContent {@@ -191,6 +199,27 @@ function validDate(value: string | null | undefined): Date | null {return Number.isNaN(parsed.getTime()) ? null : parsed}+function refreshDelay(value: string | undefined): number {+ const delay = value === undefined ? DEFAULT_REFRESH_DELAY_MS : Number(value)++ if (!Number.isInteger(delay) || delay < 0 || delay > MAX_REFRESH_DELAY_MS) {+ throw new Error(+ `RANKPINE_BLOG_REFRESH_DELAY_MS must be an integer between 0 and ${MAX_REFRESH_DELAY_MS}`,+ )+ }++ return delay+}++function backendRoot(): string {+ return path.resolve(+ __dirname,+ ['development', 'test'].includes(process.env.NODE_ENV || '')+ ? '../../../..'+ : '../..',+ )+}+function articleMarkdown(article: z.infer<typeof publishedArticleSchema>['article'],publishedDate: Date,@@ -227,7 +256,12 @@ function articleMarkdown(@Injectable()export class RankPineBlogService {- constructor(private readonly logger: AppLoggerService) {}+ private refreshTimer: NodeJS.Timeout | null = null++ constructor(+ private readonly logger: AppLoggerService,+ private readonly blogService: BlogService,+ ) {}private getConfig(): PublisherConfig {const secret = process.env.RANKPINE_BLOG_WEBHOOK_SECRET?.trim()@@ -264,6 +298,53 @@ export class RankPineBlogService {email:process.env.RANKPINE_BLOG_COMMITTER_EMAIL || DEFAULT_COMMITTER_EMAIL,},+ refreshDelayMs: refreshDelay(process.env.RANKPINE_BLOG_REFRESH_DELAY_MS),+ }+ }++ private scheduleLocalRefresh(delayMs: number): void {+ if (this.refreshTimer) {+ clearTimeout(this.refreshTimer)+ }++ this.refreshTimer = setTimeout(() => {+ this.refreshTimer = null+ void this.refreshLocalBlogPosts()+ }, delayMs)+ this.refreshTimer.unref()+ }++ private async refreshLocalBlogPosts(): Promise<void> {+ const cwd = backendRoot()++ try {+ const { stdout, stderr } = await execFileAsync(+ 'npm',+ ['run', 'refresh-submodules'],+ {+ cwd,+ timeout: 120_000,+ maxBuffer: 1_000_000,+ },+ )+ await this.blogService.clearSitemapCache()+ this.logger.log(+ {+ stdout: stdout.trim() || undefined,+ stderr: stderr.trim() || undefined,+ },+ 'RankPine blog submodule refresh',+ true,+ )+ } catch (error) {+ this.logger.error(+ {+ reason: error instanceof Error ? error.message : String(error),+ cwd,+ },+ 'RankPine blog submodule refresh',+ true,+ )}}@@ -475,6 +556,7 @@ export class RankPineBlogService {'POST /webhook/rankpine',true,)+ this.scheduleLocalRefresh(config.refreshDelayMs)return {ok: true,diff --git a/backend/apps/cloud/src/webhook/webhook.module.ts b/backend/apps/cloud/src/webhook/webhook.module.tsindex dca1ccde3..cf2d5879b 100644--- a/backend/apps/cloud/src/webhook/webhook.module.ts+++ b/backend/apps/cloud/src/webhook/webhook.module.ts@@ -7,6 +7,7 @@ import { AppLoggerModule } from '../logger/logger.module'import { MailerModule } from '../mailer/mailer.module'import { WebhookService } from './webhook.service'import { RevenueModule } from '../revenue/revenue.module'+import { BlogModule } from '../blog/blog.module'import { RankPineBlogService } from './rankpine-blog.service'@Module({@@ -16,6 +17,7 @@ import { RankPineBlogService } from './rankpine-blog.service'AppLoggerModule,MailerModule,RevenueModule,+ BlogModule,],providers: [WebhookService, RankPineBlogService],exports: [WebhookService],