feat: cozify cached articles

This commit is contained in:
Ayo 2023-10-01 09:36:21 +02:00
parent 99d3400c59
commit 45faa00c43
2 changed files with 11 additions and 5 deletions

View file

@ -16,6 +16,7 @@ export interface Props {
import type { Props } from './Library.astro';
import type { AppConfig } from '../pages/index.astro';
import { getPostCard, renderPost } from '../utils/library'
import { cozify } from '../utils/sanitizer';
const cache = await caches.open('cozy-reader');
let url= new URL(window.location.href);
// only cached unencoded url param
@ -57,10 +58,12 @@ export interface Props {
let responseText;
const fullResponse = await cache.match(url)
fullResponse?.text().then(data => {
fullResponse?.text().then(async data => {
responseText = data;
const { baseUrl } = deserialize<AppConfig>('app-config');
const cleanedResponse = await cozify(responseText, baseUrl)
const html = document.createElement('html');
html.innerHTML = responseText;
html.innerHTML = cleanedResponse;
const title = html.querySelector('meta[property="cozy:title"]')?.getAttribute('content');
if (title === 'Something is not right') {
cache.delete(url);
@ -74,7 +77,7 @@ export interface Props {
e.preventDefault();
localStorage.setItem('scrollPosition', window.scrollY.toString());
scrollTo(0,0);
renderPost(responseText, url, routerOutlet)
renderPost(cleanedResponse, url, routerOutlet)
}
const item = document.createElement('li');
item.appendChild(link);
@ -95,9 +98,11 @@ export interface Props {
}
const fullResponse = await cache.match(url)
fullResponse?.text().then(data => {
fullResponse?.text().then(async (data) => {
const responseText = data;
renderPost(responseText, url, routerOutlet, true);
const { baseUrl } = deserialize<AppConfig>('app-config');
const cleanedResponse = await cozify(responseText, baseUrl);
renderPost(cleanedResponse, url, routerOutlet, true);
if (isHome) {
const scrollPosition = localStorage.getItem('scrollPosition');
scrollTo(0, scrollPosition ? parseInt(scrollPosition) : 0);

View file

@ -9,6 +9,7 @@ import Footer from "../components/Footer.astro";
const appConfig = {
routerOutlet: 'router-outlet',
baseUrl: Astro.url.origin
};
export type AppConfig = typeof appConfig;