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

View file

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