Skip to content

Commit b2746e8

Browse files
authored
Merge pull request #6075 from cowprotocol/fix/cow-fi
fix(cow-fi): update articles page with caching strategy and type annotations
2 parents c5e299e + a117209 commit b2746e8

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

apps/cow-fi/app/(learn)/learn/[article]/page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import {
1717

1818
import type { Metadata } from 'next'
1919

20-
export const revalidate = 3600 // Revalidate at most once per hour
20+
// Next.js requires revalidate to be a literal number for static analysis
21+
// This value (3600 seconds = 1 hour) should match CMS_CACHE_TIME in services/cms/index.ts
22+
export const revalidate = 3600
2123

2224
// Maximum length for metadata descriptions. When content exceeds MAX_LENGTH,
2325
// we truncate to TRUNCATE_LENGTH (MAX_LENGTH - 3) to make room for "..." ellipsis

apps/cow-fi/app/(learn)/learn/articles/[[...pageIndex]]/page.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { ArticlesPageComponents } from '@/components/ArticlesPageComponents'
21
import { redirect } from 'next/navigation'
3-
import { Article, getArticles, getCategories } from '../../../../../services/cms'
2+
3+
import { Article, Category, getArticles, getCategories } from '../../../../../services/cms'
4+
5+
import { ArticlesPageComponents } from '@/components/ArticlesPageComponents'
46
import { ARTICLES_PER_PAGE } from '@/const/pagination'
57
import { calculateTotalPages } from '@/util/paginationUtils'
68

@@ -17,6 +19,8 @@ export type ArticlesResponse = {
1719
}
1820
}
1921

22+
// TODO: Add proper return type annotation
23+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
2024
export async function generateStaticParams() {
2125
const articlesResponse = await getArticles({ page: 0, pageSize: ARTICLES_PER_PAGE })
2226
const totalArticles = articlesResponse.meta?.pagination?.total || 0
@@ -25,6 +29,13 @@ export async function generateStaticParams() {
2529
return Array.from({ length: totalPages }, (_, i) => ({ pageIndex: [(i + 1).toString()] }))
2630
}
2731

32+
// Next.js requires revalidate to be a literal number for static analysis
33+
// This value (3600 seconds = 1 hour) should match CMS_CACHE_TIME in services/cms/index.ts
34+
export const revalidate = 3600
35+
36+
// TODO: Reduce function complexity by extracting logic
37+
// TODO: Add proper return type annotation
38+
// eslint-disable-next-line complexity, @typescript-eslint/explicit-function-return-type
2839
export default async function Page({ params }: Props) {
2940
const pageParam = (await params)?.pageIndex?.[0]
3041
const paramsAreSet = Boolean(pageParam)
@@ -52,6 +63,8 @@ export default async function Page({ params }: Props) {
5263
const allArticles = allArticlesResponse.data
5364

5465
const articles =
66+
// TODO: Reduce function complexity by extracting logic
67+
// eslint-disable-next-line complexity
5568
articlesResponse.data?.map((article: Article) => ({
5669
...article,
5770
id: article.id || 0,
@@ -69,7 +82,7 @@ export default async function Page({ params }: Props) {
6982

7083
const categoriesResponse = await getCategories()
7184
const allCategories =
72-
categoriesResponse?.map((category: any) => ({
85+
categoriesResponse?.map((category: Category) => ({
7386
name: category?.attributes?.name || '',
7487
slug: category?.attributes?.slug || '',
7588
})) || []

apps/cow-fi/next.config.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { WithNxOptions } from '@nx/next/plugins/with-nx'
33

44
const nextConfig: WithNxOptions = {
55
reactStrictMode: true,
6-
swcMinify: true,
76
nx: {
87
svgr: false,
98
},
@@ -87,10 +86,7 @@ const nextConfig: WithNxOptions = {
8786
},
8887
]
8988
},
90-
i18n: {
91-
locales: ['en'],
92-
defaultLocale: 'en',
93-
},
89+
9490
images: {
9591
domains: ['celebrated-gift-f83e5c9419.media.strapiapp.com'],
9692
remotePatterns: [
@@ -102,13 +98,22 @@ const nextConfig: WithNxOptions = {
10298
},
10399
async headers() {
104100
return [
105-
// Cache all pages for 60 seconds
101+
{
102+
source: '/learn/:path*',
103+
headers: [
104+
{
105+
key: 'Cache-Control',
106+
value: 'public, s-maxage=3600, stale-while-revalidate=86400', // 1h cache, 24h stale
107+
},
108+
],
109+
},
110+
// Cache all other pages for 1 hour
106111
{
107112
source: '/:path*',
108113
headers: [
109114
{
110115
key: 'Cache-Control',
111-
value: 'public, s-maxage=60, stale-while-revalidate=300',
116+
value: 'public, s-maxage=3600, stale-while-revalidate=86400', // 1h cache, 24h stale
112117
},
113118
],
114119
},

0 commit comments

Comments
 (0)