feat(perf): implement comprehensive Core Web Vitals, ISR caching, and media optimizations
Deploy Canina / deploy (push) Successful in 1m48s
Deploy Canina / deploy (push) Successful in 1m48s
This commit is contained in:
1 parent
2a31a9f8e4
commit
fe6a900665
42 files changed
+12941
-10561
No files matched your search
@@ -0,0 +1,9 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { RevalidationService } from './revalidation.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [RevalidationService],
|
||||
exports: [RevalidationService],
|
||||
})
|
||||
export class RevalidationModule {}
|
||||
@@ -0,0 +1,123 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class RevalidationService {
|
||||
private readonly logger = new Logger(RevalidationService.name);
|
||||
|
||||
private get frontendUrl(): string {
|
||||
const raw =
|
||||
process.env.FRONTEND_URL ||
|
||||
process.env.NEXT_PUBLIC_SITE_URL ||
|
||||
'http://localhost:3000';
|
||||
return raw.replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
private get secret(): string {
|
||||
return (
|
||||
process.env.REVALIDATION_SECRET || 'canina_revalidation_secret_key_2026'
|
||||
);
|
||||
}
|
||||
|
||||
async revalidateTag(tag: string): Promise<boolean> {
|
||||
try {
|
||||
const url = `${this.frontendUrl}/api/revalidate`;
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-revalidate-secret': this.secret,
|
||||
},
|
||||
body: JSON.stringify({ tag, secret: this.secret }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
this.logger.warn(
|
||||
`Failed to revalidate tag [${tag}] on frontend: ${res.status} ${res.statusText}`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
this.logger.log(`Successfully revalidated cache tag [${tag}]`);
|
||||
return true;
|
||||
} catch (err: any) {
|
||||
this.logger.debug(
|
||||
`Revalidation tag request skipped or frontend offline: ${err?.message}`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async revalidatePath(path: string): Promise<boolean> {
|
||||
try {
|
||||
const url = `${this.frontendUrl}/api/revalidate`;
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-revalidate-secret': this.secret,
|
||||
},
|
||||
body: JSON.stringify({ path, secret: this.secret }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
this.logger.warn(
|
||||
`Failed to revalidate path [${path}] on frontend: ${res.status} ${res.statusText}`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
this.logger.log(`Successfully revalidated cache path [${path}]`);
|
||||
return true;
|
||||
} catch (err: any) {
|
||||
this.logger.debug(
|
||||
`Revalidation path request skipped or frontend offline: ${err?.message}`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async revalidateProduct(slug?: string, artNo?: string): Promise<void> {
|
||||
const tasks: Promise<boolean>[] = [
|
||||
this.revalidateTag('products'),
|
||||
this.revalidateTag('home-data'),
|
||||
this.revalidatePath('/shop'),
|
||||
this.revalidatePath('/'),
|
||||
];
|
||||
|
||||
if (slug) {
|
||||
tasks.push(this.revalidateTag(`product-${slug}`));
|
||||
tasks.push(this.revalidatePath(`/shop/${slug}`));
|
||||
}
|
||||
if (artNo && artNo !== slug) {
|
||||
tasks.push(this.revalidateTag(`product-${artNo}`));
|
||||
tasks.push(this.revalidatePath(`/shop/${artNo}`));
|
||||
}
|
||||
|
||||
await Promise.allSettled(tasks);
|
||||
}
|
||||
|
||||
async revalidateBlog(slug?: string): Promise<void> {
|
||||
const tasks: Promise<boolean>[] = [
|
||||
this.revalidateTag('blogs'),
|
||||
this.revalidateTag('home-data'),
|
||||
this.revalidatePath('/blog'),
|
||||
this.revalidatePath('/'),
|
||||
];
|
||||
|
||||
if (slug) {
|
||||
tasks.push(this.revalidateTag(`blog-${slug}`));
|
||||
tasks.push(this.revalidatePath(`/blog/${slug}`));
|
||||
}
|
||||
|
||||
await Promise.allSettled(tasks);
|
||||
}
|
||||
|
||||
async revalidateSettings(): Promise<void> {
|
||||
await Promise.allSettled([
|
||||
this.revalidateTag('settings'),
|
||||
this.revalidateTag('home-data'),
|
||||
this.revalidateTag('seo-config'),
|
||||
this.revalidatePath('/'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user