51 lines
1.5 KiB
Text
51 lines
1.5 KiB
Text
---
|
|
import { createClient } from 'redis'
|
|
import { type ArticleData, extract } from '@extractus/article-extractor'
|
|
import SimpleAddressBar from '../components/SimpleAddressBar.astro'
|
|
import Post from '../components/Post.astro'
|
|
import App from '../layouts/App.astro'
|
|
import Library from '../components/Library.astro'
|
|
import Footer from '../components/Footer.astro'
|
|
|
|
const client = createClient()
|
|
// TODO: put on a log file
|
|
client.on('error', (err) => console.error('Redis Client Error', err))
|
|
await client.connect()
|
|
export const prerender = false
|
|
|
|
let url = Astro.url.searchParams.get('url')
|
|
let article: ArticleData | null = { url: '/' }
|
|
|
|
while (url?.startsWith(Astro.url.origin)) {
|
|
url = new URL(url).searchParams.get('url')
|
|
}
|
|
|
|
if (url) {
|
|
// try cache
|
|
article = (await client.json.get('cozy:url:' + url)) as ArticleData
|
|
if (article !== null) {
|
|
console.log('>>> Using cached content', article.url)
|
|
} else {
|
|
try {
|
|
article = await extract(url)
|
|
console.log('>>> Using fetched content', article?.url)
|
|
if (article !== null) {
|
|
// cache via redis
|
|
await client.json.set('cozy:url:' + url, '$', article)
|
|
console.log('>>> Added to cache', article.url)
|
|
}
|
|
} catch {
|
|
article = null
|
|
}
|
|
}
|
|
}
|
|
---
|
|
|
|
<App article={article}>
|
|
<SimpleAddressBar url={url} />
|
|
<div slot="post" id="router-outlet">
|
|
<Post article={article} />
|
|
</div>
|
|
<Library slot="library" skipSave={article === null} />
|
|
<Footer slot="footer" />
|
|
</App>
|