feat: enhance deployment and media handling

- Updated deployment configurations to disable Incremental Static Regeneration (ISR) and cache on demand pages for better performance control.
- Extended .gitignore to include deployment-specific directories, preventing unnecessary files from being tracked.
- Improved video handling in content by adding support for additional video types and enhancing video element attributes for better playback control.
- Refactored post retrieval logic to ensure channel information is correctly merged and SEO data is accurately assigned.
- Enhanced static file handling to include error handling and domain whitelisting for security and reliability.
This commit is contained in:
ccbikai
2024-08-05 18:57:28 +08:00
parent 4339f22851
commit 2df9b66ed9
8 changed files with 70 additions and 18 deletions

4
.gitignore vendored
View File

@@ -22,3 +22,7 @@ pnpm-debug.log*
# jetbrains setting folder # jetbrains setting folder
.idea/ .idea/
.vercel/
.netlify/
.vercel

26
api/static/index.js Normal file
View File

@@ -0,0 +1,26 @@
import { GET } from '../../src/pages/static/[...url]'
export const config = {
runtime: 'edge',
}
export default function handler(request) {
const url = request.url?.split('/static/')?.[1]
if (!url) {
return new Response('Not Found', { status: 404 })
}
const target = new URL(url)
target.searchParams.delete('path')
return GET({
request,
params: {
url: target.origin + target.pathname,
},
url: {
search: target.search,
},
})
}

View File

@@ -9,11 +9,12 @@ import sentry from '@sentry/astro'
const providers = { const providers = {
vercel: vercel({ vercel: vercel({
isr: false,
edgeMiddleware: false, edgeMiddleware: false,
}), }),
cloudflare_pages: cloudflare(), cloudflare_pages: cloudflare(),
netlify: netlify({ netlify: netlify({
cacheOnDemandPages: true, cacheOnDemandPages: false,
edgeMiddleware: false, edgeMiddleware: false,
}), }),
node: node({ node: node({

View File

@@ -90,7 +90,8 @@ const timeago = datetime.isBefore(dayjs().subtract(1, 'w'))
} }
} }
.content :global(.tgme_widget_message_video) { .content
:global(.tgme_widget_message_video, .tgme_widget_message_roundvideo) {
aspect-ratio: 1 / 1; aspect-ratio: 1 / 1;
} }

View File

@@ -28,8 +28,17 @@ function getImages($, item, { staticProxy, id, index, title }) {
function getVideo($, item, { staticProxy, index }) { function getVideo($, item, { staticProxy, index }) {
const video = $(item).find('.tgme_widget_message_video_wrap video') const video = $(item).find('.tgme_widget_message_video_wrap video')
video?.attr('src', staticProxy + video?.attr('src'))?.attr('controls', true)?.attr('preload', index > 15 ? 'auto' : 'metadata') video?.attr('src', staticProxy + video?.attr('src'))
return $.html(video) ?.attr('controls', true)
?.attr('preload', index > 15 ? 'auto' : 'metadata')
?.attr('playsinline', true).attr('webkit-playsinline', true)
const roundVideo = $(item).find('.tgme_widget_message_roundvideo_wrap video')
roundVideo?.attr('src', staticProxy + roundVideo?.attr('src'))
?.attr('controls', true)
?.attr('preload', index > 15 ? 'auto' : 'metadata')
?.attr('playsinline', true).attr('webkit-playsinline', true)
return $.html(video) + $.html(roundVideo)
} }
function getLinkPreview($, item, { staticProxy, index }) { function getLinkPreview($, item, { staticProxy, index }) {
@@ -52,10 +61,10 @@ function getPost($, item, { channel, staticProxy, index = 0 }) {
const title = content?.text()?.match(/[^。\n]*(?=[。\n]|http)/g)?.[0] ?? content?.text() ?? '' const title = content?.text()?.match(/[^。\n]*(?=[。\n]|http)/g)?.[0] ?? content?.text() ?? ''
const id = $(item).attr('data-post')?.replace(`${channel}/`, '') const id = $(item).attr('data-post')?.replace(`${channel}/`, '')
$(content).find('a').each((_index, a) => { $(content).find('a')?.each((_index, a) => {
$(a).attr('title', $(a).text()) $(a)?.attr('title', $(a)?.text())
}) })
$(content).find('.emoji').attr('style', '') $(content).find('.emoji')?.attr('style', '')
const tags = $(content).find('a[href^="?q="]')?.each((_index, a) => { const tags = $(content).find('a[href^="?q="]')?.each((_index, a) => {
$(a)?.attr('href', `/search/${encodeURIComponent($(a)?.text())}`) $(a)?.attr('href', `/search/${encodeURIComponent($(a)?.text())}`)
})?.map((_index, a) => $(a)?.text()?.replace('#', ''))?.get() })?.map((_index, a) => $(a)?.text()?.replace('#', ''))?.get()
@@ -68,16 +77,15 @@ function getPost($, item, { channel, staticProxy, index = 0 }) {
tags, tags,
text: content?.text(), text: content?.text(),
content: [ content: [
$.html($(item).find('.tgme_widget_message_reply')?.wrapInner('<small></small>')?.wrapInner('<blockquote></blockquote>')),
getImages($, item, { staticProxy, id, index, title }), getImages($, item, { staticProxy, id, index, title }),
getVideo($, item, { staticProxy, id, index, title }), getVideo($, item, { staticProxy, id, index, title }),
content?.html(), content?.html(),
// $(item).find('.tgme_widget_message_sticker_wrap')?.html(), // $(item).find('.tgme_widget_message_sticker_wrap')?.html(),
$(item).find('.tgme_widget_message_poll')?.html(), $(item).find('.tgme_widget_message_poll')?.html(),
$.html($(item).find('.tgme_widget_message_document_wrap')), $.html($(item).find('.tgme_widget_message_document_wrap')),
$.html($(item).find('.tgme_widget_message_roundvideo')?.attr('controls', true)),
$.html($(item).find('.tgme_widget_message_voice')?.attr('controls', true)), $.html($(item).find('.tgme_widget_message_voice')?.attr('controls', true)),
$.html($(item).find('.tgme_widget_message_location_wrap')), $.html($(item).find('.tgme_widget_message_location_wrap')),
$.html($(item).find('.tgme_widget_message_reply .tgme_widget_message_text')?.wrapInner('<blockquote></blockquote>')),
getLinkPreview($, item, { staticProxy, index }), getLinkPreview($, item, { staticProxy, index }),
].filter(Boolean).join('').replace(/(url\(["'])((https?:)?\/\/)/g, (match, p1, p2, _p3) => { ].filter(Boolean).join('').replace(/(url\(["'])((https?:)?\/\/)/g, (match, p1, p2, _p3) => {
if (p2 === '//') { if (p2 === '//') {

View File

@@ -6,15 +6,18 @@ import { getChannelInfo } from '../../lib/telegram'
const { SITE_URL } = Astro.locals const { SITE_URL } = Astro.locals
const channel = JSON.parse(JSON.stringify(await getChannelInfo(Astro))) const channelInfo = await getChannelInfo(Astro)
const post = await getChannelInfo(Astro, { const post = await getChannelInfo(Astro, {
type: 'post', type: 'post',
id: Astro.params.id, id: Astro.params.id,
}) })
channel.posts = [post] const channel = {
channel.seo = post ...(channelInfo || {}),
posts: [post],
seo: post,
}
const staticProxy = getEnv(import.meta.env, Astro, 'STATIC_PROXY') ?? '/static/' const staticProxy = getEnv(import.meta.env, Astro, 'STATIC_PROXY') ?? '/static/'

View File

@@ -8,11 +8,15 @@ const targetWhitelist = [
export const prerender = false export const prerender = false
export async function GET(Astro) { export async function GET({ request, params, url }) {
const { request, params, url } = Astro try {
const target = new URL(params.url + url.search) const target = new URL(params.url + url.search)
if (!targetWhitelist.some(host => target.host.endsWith(host))) { if (!targetWhitelist.some(domain => target.hostname.endsWith(domain))) {
return Astro.redirect(target.toString(), 302) return Response.redirect(target.toString(), 302)
}
return fetch(target.toString(), request)
}
catch (error) {
return new Response(error.message, { status: 500 })
} }
return fetch(target.toString(), request)
} }

5
vercel.json Normal file
View File

@@ -0,0 +1,5 @@
{
"rewrites": [
{ "source": "/static/:path*", "destination": "/api/static" }
]
}