Skip to content

Commit

Permalink
feat: enhance RSS handling and improve middleware logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ccbikai committed Nov 7, 2024
1 parent a1c79d4 commit e12a0d0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/components/header.astro
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import telegram from '../assets/telegram.svg'
import mastodon from '../assets/mastodon.svg'
import bluesky from '../assets/bluesky.svg'
const { SITE_URL } = Astro.locals
const { SITE_URL, RSS_URL } = Astro.locals
const { channel } = Astro.props
const PODCASRT = getEnv(import.meta.env, Astro, 'PODCASRT')
Expand Down Expand Up @@ -39,7 +39,7 @@ const staticProxy = getEnv(import.meta.env, Astro, 'STATIC_PROXY') ?? '/static/'
</a>
</div>
<div class="header-icons">
<a href={`${SITE_URL}rss.xml`} target="_blank" rel="alternate" type="application/rss+xml" title="RSS Feed">
<a href={RSS_URL} target="_blank" rel="alternate" type="application/rss+xml" title="RSS Feed">
<img {...rss} alt="RSS" class="social-icon" width="1em" />
</a>

Expand Down
1 change: 1 addition & 0 deletions src/components/item.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
import '../assets/item.css'
import 'prismjs/themes/prism.css'
import dayjs from '../lib/dayjs'
import { getEnv } from '../lib/env'
Expand Down
4 changes: 3 additions & 1 deletion src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/// <reference types="astro/client" />
declare namespace App {
interface Locals {
SITE_URL: string,
SITE_URL: string
RSS_URL: string
RSS_PREFIX: string
}
}
5 changes: 2 additions & 3 deletions src/layouts/base.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import '../assets/normalize.css'
import '../assets/style.css'
import '../assets/global.css'
import '../assets/item.css'
import { SEO } from 'astro-seo'
import { getEnv } from '../lib/env'
import backToTopIcon from '../assets/back-to-top.svg'
const { SITE_URL } = Astro.locals
const { SITE_URL, RSS_URL, RSS_PREFIX } = Astro.locals
const { channel } = Astro.props
const locale = getEnv(import.meta.env, Astro, 'LOCALE')
Expand Down Expand Up @@ -67,7 +66,7 @@ const navs = (getEnv(import.meta.env, Astro, 'NAVS') || '')
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#f4f1ec" />
<link rel="alternate" type="application/rss+xml" title={channel?.title} href={origin + '/rss.xml'} />
<link rel="alternate" type="application/rss+xml" title={`${RSS_PREFIX}${channel?.title}`} href={RSS_URL} />
<style is:inline>
@view-transition {
navigation: auto; /* enabled */
Expand Down
16 changes: 15 additions & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
export async function onRequest(context, next) {
context.locals.SITE_URL = `${import.meta.env.SITE ?? ''}${import.meta.env.BASE_URL}`
context.locals.RSS_URL = `${context.locals.SITE_URL}rss.xml`
context.locals.RSS_PREFIX = ''

if (context.url.pathname.startsWith('/search') && context.params.q?.startsWith('#')) {
const tag = context.params.q.replace('#', '')
context.locals.RSS_URL = `${context.locals.SITE_URL}rss.xml?tag=${tag}`
context.locals.RSS_PREFIX = `${tag} | `
}

const response = await next()

if (!response.bodyUsed) {
response.headers.set('Speculation-Rules', '"/rules/prefetch.json"')
if (response.headers.get('Content-type') === 'text/html') {
response.headers.set('Speculation-Rules', '"/rules/prefetch.json"')
}

if (!response.headers.has('Cache-Control')) {
response.headers.set('Cache-Control', 'public, max-age=300, s-maxage=300')
}
}
return response
};
10 changes: 7 additions & 3 deletions src/pages/rss.json.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { getChannelInfo } from '../lib/telegram'

export async function GET(Astro) {
const request = Astro.request
const { SITE_URL } = Astro.locals
const channel = await getChannelInfo(Astro)
const tag = Astro.url.searchParams.get('tag')
const channel = await getChannelInfo(Astro, {
q: tag ? `#${tag}` : '',
})
const posts = channel.posts || []

const request = Astro.request
const url = new URL(request.url)
url.pathname = SITE_URL
url.search = ''

return Response.json({
version: 'https://jsonfeed.org/version/1.1',
title: channel.title,
title: `${tag ? `${tag} | ` : ''}${channel.title}`,
description: channel.description,
home_page_url: url.toString(),
items: posts.map(item => ({
Expand Down
11 changes: 8 additions & 3 deletions src/pages/rss.xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ import sanitizeHtml from 'sanitize-html'
import { getChannelInfo } from '../lib/telegram'

export async function GET(Astro) {
const request = Astro.request
const { SITE_URL } = Astro.locals
const channel = await getChannelInfo(Astro)
const tag = Astro.url.searchParams.get('tag')
const channel = await getChannelInfo(Astro, {
q: tag ? `#${tag}` : '',
})
const posts = channel.posts || []

const request = Astro.request
const url = new URL(request.url)
url.pathname = SITE_URL
url.search = ''

return rss({
title: channel.title,
title: `${tag ? `${tag} | ` : ''}${channel.title}`,
description: channel.description,
site: url.origin,
trailingSlash: false,
items: posts.map(item => ({
link: `posts/${item.id}`,
title: item.title,
Expand Down

0 comments on commit e12a0d0

Please sign in to comment.