Skip to content

Commit 3f53fdb

Browse files
authored
refactor: add storage for rss news (#542)
1 parent 1849bff commit 3f53fdb

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/api/news.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ const NEWS_CONFIG = {
1515
proxyUrl: 'https://proxy-for-blogs-rss-in-dashboard.greptime.workers.dev',
1616
}
1717
const CACHE_KEY = 'greptime_news_cache'
18+
const UID_KEY = 'greptime_anon_id'
1819
const TTL_MS = 10 * 60 * 1000
1920

21+
const getAnonId = () => {
22+
let id = localStorage.getItem(UID_KEY)
23+
if (!id) {
24+
id = crypto.randomUUID()
25+
localStorage.setItem(UID_KEY, id)
26+
}
27+
return id
28+
}
29+
2030
const setCachedNews = (items: NewsItem[]) => {
2131
const data = {
2232
items,
@@ -91,14 +101,16 @@ const parseXmlRss = (xmlString: string): NewsItem[] => {
91101
export const fetchGreptimeNews = async (): Promise<NewsResponse> => {
92102
try {
93103
const cached = getCachedNews()
104+
const anonId = getAnonId()
94105
if (cached) {
95106
return cached
96107
}
97108

98109
const response = await fetch(NEWS_CONFIG.proxyUrl, {
99110
method: 'GET',
100111
headers: {
101-
Accept: 'application/xml, text/xml, */*',
112+
'Accept': 'application/xml, text/xml, */*',
113+
'X-Oss-Uid': anonId,
102114
},
103115
})
104116

0 commit comments

Comments
 (0)