Skip to content

Commit

Permalink
fix(page): use qualified url for service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-heimbuch committed Jul 15, 2024
1 parent 3803877 commit 1ece22b
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 66 deletions.
6 changes: 1 addition & 5 deletions apps/page/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export default defineConfig({
// adapter: node({
// mode: 'standalone'
// }),
adapter: cloudflare({
platformProxy: {
enabled: true
}
}),
adapter: cloudflare(),
integrations: [vue({ appEntrypoint: '/src/app' }), tailwind()]
});
37 changes: 0 additions & 37 deletions apps/page/src/features/LoadingOverlay.vue

This file was deleted.

2 changes: 0 additions & 2 deletions apps/page/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import PageFooter from '../features/PageFooter.vue';
import LoadingBar from '../features/LoadingBar.vue';
import Subscribe from '../features/subscribe/Subscribe.vue';
import Colors from '../features/Colors.vue';
import LoadingOverlay from '../features/LoadingOverlay.vue';
import { store } from '../logic';
import { getLanguage } from '../i18n';
Expand All @@ -32,7 +31,6 @@ const lang = getLanguage();
</head>
<body>
<Colors client:only="vue" />
<LoadingOverlay client:load />
<LoadingBar client:only="vue" />
<PageHeader client:idle transition:persist="header" />
<main transition:animate="fade">
Expand Down
20 changes: 5 additions & 15 deletions apps/page/src/lib/caching.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@

export const etag = async (input: any): Promise<string> => {
const entity = JSON.stringify(input);

const textAsBuffer = new TextEncoder().encode(entity);
const hashBuffer = await crypto.subtle.digest("SHA-1", textAsBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hash = hashArray
.map((item) => item.toString(16).padStart(2, "0"))
.join("");

const len = Buffer.byteLength(entity, 'utf8');

return len.toString(16) + '-' + Buffer.from(hash).toString('base64').substring(0, 27);
}
export const createHash = async (input: string): Promise<string> =>
Array.from(
new Uint8Array(await crypto.subtle.digest('SHA-1', new TextEncoder().encode(input))),
(byte) => byte.toString(16).padStart(2, '0')
).join('');
7 changes: 2 additions & 5 deletions apps/page/src/logic/sagas/data.sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import parseFeed from '../data/feed-parser';
import type { initializeAppPayload } from '../store/stores/runtime.store';
import type { Podcast } from '../../types/feed.types';
import { version } from '../../../package.json';
import { etag } from '../../lib/caching.js';
import { createHash } from '../../lib/caching';

function* fetchData(input: Action<initializeAppPayload>) {
const data: Podcast = yield parseFeed(input.payload);

const cacheKey: string | null = data.etag ? yield etag({
feed: data.etag,
version
}) : null
const cacheKey: string | null = data.etag ? yield createHash(`${data.etag}${version}`) : null;

yield put(actions.lifecycle.dataFetched({ data, cacheKey }));
}
Expand Down
7 changes: 5 additions & 2 deletions apps/page/src/logic/sagas/serviceworker.sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ export default ({

const feed: string = yield select(selectFeed);
const cacheKey: string = yield select(selectCacheKey);
const baseUrl = new URL(document.location.href);

const serviceWorkerUrl = `${baseUrl.origin}${serviceWorker}?feed=${feed}&cacheKey=${cacheKey}`;

try {
const registration: ServiceWorkerRegistration = yield navigator.serviceWorker.register(
`${serviceWorker}?feed=${feed}&cacheKey=${cacheKey}`,
serviceWorkerUrl,
{
scope: '/'
}
Expand All @@ -35,7 +38,7 @@ export default ({

return function* () {
if(import.meta.env.MODE === 'production') {
yield registerServiceWorker();
yield registerServiceWorker();
}
};
};

0 comments on commit 1ece22b

Please sign in to comment.