mirror of
https://github.com/d0zingcat/BroadcastChannel.git
synced 2026-05-13 23:16:46 +00:00
feat: enhance RSS handling and improve middleware logic
This commit is contained in:
@@ -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')
|
||||
@@ -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>
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
---
|
||||
import '../assets/item.css'
|
||||
import 'prismjs/themes/prism.css'
|
||||
import dayjs from '../lib/dayjs'
|
||||
import { getEnv } from '../lib/env'
|
||||
|
||||
4
src/env.d.ts
vendored
4
src/env.d.ts
vendored
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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')
|
||||
@@ -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 */
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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 => ({
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user