Skip to content

Commit 80b4460

Browse files
committed
fix: reduce rss cache time, resolves #516
Signed-off-by: Innei <[email protected]>
1 parent c4bdd1b commit 80b4460

File tree

1 file changed

+69
-35
lines changed

1 file changed

+69
-35
lines changed

src/app/feed/route.tsx

+69-35
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@ import { CDN_HOST } from '~/app.static.config'
88
import { AlertsRule as __AlertsRule } from '~/components/ui/markdown/parsers/alert'
99
import { ContainerRule as __ContainerRule } from '~/components/ui/markdown/parsers/container'
1010
import { InsertRule } from '~/components/ui/markdown/parsers/ins'
11-
import {
12-
KateXBlockRule as __KateXBlockRule,
13-
KateXRule as __KateXRule,
14-
} from '~/components/ui/markdown/parsers/katex'
1511
import { MarkRule } from '~/components/ui/markdown/parsers/mark'
1612
import { MentionRule } from '~/components/ui/markdown/parsers/mention'
1713
import { SpoilerRule } from '~/components/ui/markdown/parsers/spoiler'
14+
import { get } from '~/lib/lodash'
1815
import { apiClient } from '~/lib/request'
1916

20-
import { fetchAggregationData } from '../(app)/api'
21-
2217
export const dynamic = 'force-dynamic'
23-
export const revalidate = 86400 // 1 day
18+
export const revalidate = 60
2419

2520
interface RSSProps {
2621
title: string
@@ -42,18 +37,39 @@ export async function GET() {
4237
const ReactDOM = (await import('react-dom/server')).default
4338

4439
const [{ author, data, url }, agg] = await Promise.all([
45-
fetch(apiClient.aggregate.proxy.feed.toString(true), {
46-
next: {
47-
revalidate: 86400,
48-
},
49-
}).then((res) => res.json() as Promise<RSSProps>),
50-
51-
fetchAggregationData(),
40+
apiClient.aggregate.proxy.feed.get<RSSProps>(),
41+
apiClient.aggregate.getAggregateData<AppThemeConfig>('shiro'),
5242
])
5343

5444
const { title, description } = agg.seo
5545

5646
const now = new Date()
47+
const custom_elements = get(
48+
agg.$raw.theme as AppConfig,
49+
'config.module.rss.custom_elements',
50+
)
51+
52+
const followChallengeIndex = custom_elements
53+
? custom_elements.findIndex((item: any) => item.follow_challenge)
54+
: -1
55+
if (-~followChallengeIndex) {
56+
const map = {} as Record<string, string>
57+
58+
const { follow_challenge } = custom_elements[followChallengeIndex]
59+
for (const item of follow_challenge) {
60+
Object.assign(map, item)
61+
}
62+
63+
custom_elements[followChallengeIndex].follow_challenge = [
64+
{
65+
feedId: map.feed_id,
66+
},
67+
{
68+
userId: map.user_id,
69+
},
70+
]
71+
}
72+
5773
const feed = new RSS({
5874
title,
5975
description,
@@ -63,6 +79,8 @@ export async function GET() {
6379
image_url: `${url}${agg?.theme?.config?.site?.favicon}`,
6480
generator: 'Shiro (https://github.com/Innei/Shiro)',
6581
pubDate: now.toUTCString(),
82+
83+
custom_elements,
6684
})
6785

6886
data.forEach((item) => {
@@ -138,8 +156,8 @@ export async function GET() {
138156
mark: MarkRule,
139157
ins: InsertRule,
140158

141-
kateX: KateXRule,
142-
kateXBlock: KateXBlockRule,
159+
// kateX: KateXRule,
160+
// kateXBlock: KateXBlockRule,
143161
container: ContainerRule,
144162
alerts: AlertsRule,
145163
},
@@ -171,38 +189,54 @@ export async function GET() {
171189
})
172190
})
173191

174-
return new Response(feed.xml(), {
192+
return new Response(`${feed.xml()}`, {
175193
headers: {
176194
'Content-Type': 'application/xml',
177-
'Cache-Control': 'max-age=60, s-maxage=86400',
178-
'CDN-Cache-Control': 'max-age=86400',
179-
'Cloudflare-CDN-Cache-Control': 'max-age=86400',
180-
'Vercel-CDN-Cache-Control': 'max-age=86400',
195+
'Cache-Control': `max-age=60, s-maxage=${revalidate}`,
196+
'CDN-Cache-Control': `max-age=${revalidate}`,
197+
'Cloudflare-CDN-Cache-Control': `max-age=${revalidate}`,
198+
'Vercel-CDN-Cache-Control': `max-age=${revalidate}`,
181199
},
182200
})
183201
}
184202

185203
const NotSupportRender = () => {
186-
throw new Error('Not support render in RSS')
204+
// throw new Error('Not support render in RSS')
205+
return (
206+
<p
207+
style={{
208+
padding: '6px 12px',
209+
borderLeft: '2px solid #33A6B8',
210+
background: '#33A6B850',
211+
fontStyle: 'italic',
212+
fontWeight: 500,
213+
}}
214+
>
215+
Not support render this content in RSS render
216+
</p>
217+
)
187218
}
188219

189-
const KateXRule: MarkdownToJSX.Rule = {
190-
...__KateXRule,
191-
react(node, _, state?) {
192-
return <NotSupportRender key={state?.key} />
193-
},
194-
}
195-
const KateXBlockRule: MarkdownToJSX.Rule = {
196-
...__KateXBlockRule,
197-
react(node, _, state?) {
198-
return <NotSupportRender key={state?.key} />
199-
},
200-
}
220+
// const KateXRule: MarkdownToJSX.Rule = {
221+
// ...__KateXRule,
222+
// react(node, _, state?) {
223+
// return node
224+
// },
225+
// }
226+
// const KateXBlockRule: MarkdownToJSX.Rule = {
227+
// ...__KateXBlockRule,
228+
// react(node, _, state?) {
229+
// return <NotSupportRender key={state?.key} />
230+
// },
231+
// }
201232

202233
const AlertsRule: MarkdownToJSX.Rule = {
203234
...__AlertsRule,
204235
react(node, output, state) {
205-
return <NotSupportRender key={state?.key} />
236+
const { body } = node.parsed
237+
const bodyClean = body.replaceAll(/^> */gm, '').trim()
238+
239+
return <blockquote key={state.key}>{compiler(bodyClean)}</blockquote>
206240
},
207241
}
208242

0 commit comments

Comments
 (0)