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
@@ -6,11 +6,13 @@ import Footer from "../components/Footer";
|
||||
import { NetworkBanner } from "../components/NetworkBanner";
|
||||
import { Toaster, toast } from "sonner";
|
||||
import { usePathname, useRouter } from 'next/navigation';
|
||||
import dynamic from 'next/dynamic';
|
||||
|
||||
const CartDrawer = dynamic(() => import("../components/CartDrawer"), { ssr: false });
|
||||
const LoginModal = dynamic(() => import("../components/LoginModal"), { ssr: false });
|
||||
const AuthModal = dynamic(() => import("../components/AuthModal"), { ssr: false });
|
||||
const B2BPortal = dynamic(() => import("../components/B2BPortal"), { ssr: false });
|
||||
|
||||
import CartDrawer from "../components/CartDrawer";
|
||||
import LoginModal from "../components/LoginModal";
|
||||
import AuthModal from "../components/AuthModal";
|
||||
import B2BPortal from "../components/B2BPortal";
|
||||
import { useUserStore } from "../lib/store/userStore";
|
||||
import { useSettingsStore } from "../lib/store/settingsStore";
|
||||
import { useUIStore } from "../lib/store/uiStore";
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { revalidateTag, revalidatePath } from 'next/cache';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
return handleRevalidate(request);
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
return handleRevalidate(request);
|
||||
}
|
||||
|
||||
async function handleRevalidate(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
let secret = searchParams.get('secret') || request.headers.get('x-revalidate-secret');
|
||||
let tag = searchParams.get('tag');
|
||||
let path = searchParams.get('path');
|
||||
|
||||
// Check auth header if bearer token provided
|
||||
const authHeader = request.headers.get('authorization');
|
||||
if (authHeader && authHeader.startsWith('Bearer ')) {
|
||||
secret = authHeader.substring(7);
|
||||
}
|
||||
|
||||
if (request.method === 'POST') {
|
||||
try {
|
||||
const body = await request.json().catch(() => ({}));
|
||||
if (body && typeof body === 'object') {
|
||||
if (body.secret) secret = body.secret;
|
||||
if (body.tag) tag = body.tag;
|
||||
if (body.path) path = body.path;
|
||||
}
|
||||
} catch {
|
||||
// Ignore body read error
|
||||
}
|
||||
}
|
||||
|
||||
const expectedSecret = process.env.REVALIDATION_SECRET || 'canina_revalidation_secret_key_2026';
|
||||
if (!secret || secret !== expectedSecret) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Invalid revalidation secret token' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!tag && !path) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Missing tag or path parameter for cache revalidation' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const revalidated: { tag?: string; path?: string } = {};
|
||||
|
||||
if (tag) {
|
||||
revalidateTag(tag, 'default');
|
||||
revalidated.tag = tag;
|
||||
}
|
||||
|
||||
if (path) {
|
||||
revalidatePath(path);
|
||||
revalidated.path = path;
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
revalidated: true,
|
||||
...revalidated,
|
||||
now: Date.now(),
|
||||
});
|
||||
}
|
||||
@@ -24,7 +24,12 @@ async function getBlog(rawSlug: string) {
|
||||
const slug = decodeURIComponent(rawSlug);
|
||||
const apiUrl = process.env.INTERNAL_API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4001/api';
|
||||
const apiBase = apiUrl.endsWith('/api') ? apiUrl : `${apiUrl.replace(/\/$/, '')}/api`;
|
||||
const res = await fetch(`${apiBase}/blogs/${encodeURIComponent(slug)}`, { next: { revalidate: 60 } });
|
||||
const res = await fetch(`${apiBase}/blogs/${encodeURIComponent(slug)}`, {
|
||||
next: {
|
||||
tags: ['blogs', `blog-${slug}`],
|
||||
revalidate: 3600,
|
||||
},
|
||||
});
|
||||
if (!res.ok) {
|
||||
console.error(`[Blog] Failed to fetch blog ${slug}: ${res.status}`);
|
||||
return null;
|
||||
@@ -37,7 +42,7 @@ async function getBlog(rawSlug: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export const revalidate = 60;
|
||||
export const revalidate = 3600;
|
||||
|
||||
export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }): Promise<Metadata> {
|
||||
const { slug } = await params;
|
||||
@@ -301,9 +306,10 @@ export default async function BlogPostPage({ params }: { params: Promise<{ slug:
|
||||
src={blog.imageUrl}
|
||||
alt={blog.imageAlt || blog.title}
|
||||
fill
|
||||
priority
|
||||
sizes="(max-width: 1024px) 100vw, 800px"
|
||||
className="w-full h-full object-cover"
|
||||
unoptimized
|
||||
priority
|
||||
/>
|
||||
{blog.imageCaption && (
|
||||
<div className="absolute bottom-0 inset-x-0 bg-black/60 backdrop-blur-sm text-white text-xs font-medium py-2 px-4 text-center">
|
||||
|
||||
@@ -10,11 +10,15 @@ export async function generateMetadata(): Promise<Metadata> {
|
||||
});
|
||||
}
|
||||
|
||||
export const revalidate = 3600;
|
||||
|
||||
async function getCategories() {
|
||||
const apiUrl = process.env.INTERNAL_API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4001/api';
|
||||
const apiBase = apiUrl.endsWith('/api') ? apiUrl : `${apiUrl.replace(/\/$/, '')}/api`;
|
||||
try {
|
||||
const res = await fetch(`${apiBase}/blogs/categories`, { next: { revalidate: 60 } });
|
||||
const res = await fetch(`${apiBase}/blogs/categories`, {
|
||||
next: { tags: ['blogs', 'blog-categories'], revalidate: 3600 },
|
||||
});
|
||||
if (!res.ok) return [];
|
||||
const json = await res.json();
|
||||
return json.data || json || [];
|
||||
@@ -28,7 +32,9 @@ async function getBlogs() {
|
||||
const apiUrl = process.env.INTERNAL_API_URL || process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4001/api';
|
||||
const apiBase = apiUrl.endsWith('/api') ? apiUrl : `${apiUrl.replace(/\/$/, '')}/api`;
|
||||
try {
|
||||
const res = await fetch(`${apiBase}/blogs?limit=50`, { next: { revalidate: 60 } });
|
||||
const res = await fetch(`${apiBase}/blogs?limit=50`, {
|
||||
next: { tags: ['blogs'], revalidate: 3600 },
|
||||
});
|
||||
if (!res.ok) {
|
||||
console.error(`[Blog] Failed to fetch blogs: ${res.status}`);
|
||||
return [];
|
||||
|
||||
@@ -70,13 +70,13 @@
|
||||
@import "tailwindcss";
|
||||
|
||||
@theme {
|
||||
--font-sans: "Vazirmatn";
|
||||
--font-vazir: "Vazirmatn";
|
||||
--font-shabnam: "Shabnam";
|
||||
--font-sahel: "Sahel";
|
||||
--font-lalezar: "Lalezar", cursive;
|
||||
--font-sans: var(--font-vazir), "Vazirmatn", system-ui, sans-serif;
|
||||
--font-vazir: var(--font-vazir), "Vazirmatn", sans-serif;
|
||||
--font-shabnam: "Shabnam", sans-serif;
|
||||
--font-sahel: "Sahel", sans-serif;
|
||||
--font-lalezar: var(--font-lalezar), "Lalezar", cursive;
|
||||
--font-nastaliq: "IranNastaliq", serif;
|
||||
--font-iranian-sans: "IranianSans";
|
||||
--font-iranian-sans: "IranianSans", sans-serif;
|
||||
|
||||
/* Brand Colors */
|
||||
--color-canina-blue: #1E4053;
|
||||
@@ -99,11 +99,11 @@
|
||||
|
||||
@layer base {
|
||||
:root {
|
||||
font-family: "Vazirmatn" !important;
|
||||
font-family: var(--font-vazir), "Vazirmatn", sans-serif !important;
|
||||
}
|
||||
|
||||
html, body {
|
||||
font-family: "Vazirmatn" !important;
|
||||
font-family: var(--font-vazir), "Vazirmatn", sans-serif !important;
|
||||
@apply bg-medical-gray-50 text-medical-gray-900 antialiased max-w-[100vw] overflow-x-hidden;
|
||||
font-feature-settings: "ss01", "ss02", "cv01", "cv11";
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { Metadata } from 'next';
|
||||
import './globals.css';
|
||||
import ClientLayout from './ClientLayout';
|
||||
import { getSeoConfig } from '../lib/seo';
|
||||
import { vazirmatn, lalezar } from '../lib/fonts';
|
||||
|
||||
export async function generateMetadata(): Promise<Metadata> {
|
||||
const config = await getSeoConfig();
|
||||
@@ -118,7 +119,7 @@ export default function RootLayout({
|
||||
const safeJsonLd = JSON.stringify(jsonLd).replace(/</g, '\\u003c');
|
||||
|
||||
return (
|
||||
<html lang="fa" dir="rtl" suppressHydrationWarning>
|
||||
<html lang="fa" dir="rtl" className={`${vazirmatn.variable} ${lalezar.variable}`} suppressHydrationWarning>
|
||||
<body className="min-h-screen bg-medical-gray-50 font-sans" suppressHydrationWarning>
|
||||
<script
|
||||
type="application/ld+json"
|
||||
|
||||
@@ -4,6 +4,8 @@ import type { Metadata } from 'next';
|
||||
import { Suspense } from 'react';
|
||||
import { getSeoConfig, formatPageTitle } from "../../../lib/seo";
|
||||
|
||||
export const revalidate = 3600;
|
||||
|
||||
export async function generateMetadata(
|
||||
{ params }: { params: Promise<{ slug: string }> }
|
||||
): Promise<Metadata> {
|
||||
@@ -117,8 +119,9 @@ export default async function ShopProductPage({ params }: { params: Promise<{ sl
|
||||
...(product.images || [])
|
||||
])).filter(Boolean) as string[];
|
||||
|
||||
// 1 year valid price offer
|
||||
const priceValidUntil = new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
|
||||
// 1 year valid price offer based on product timestamp
|
||||
const baseTime = product.updatedAt ? new Date(product.updatedAt).getTime() : 1771939200000;
|
||||
const priceValidUntil = new Date(baseTime + 365 * 24 * 60 * 60 * 1000).toISOString().split('T')[0];
|
||||
|
||||
// Process approved reviews and ratings
|
||||
const approvedReviews = (product.reviews || []).filter(r => r.status === 'APPROVED' || !r.status);
|
||||
|
||||
Reference in new issue
Block a user