From 8f26f9a7fa5b02c9d9d34cc663818034525fd5ca Mon Sep 17 00:00:00 2001 From: chrox Date: Wed, 25 Dec 2024 17:25:44 +0100 Subject: [PATCH] Fix potential 304 Not Modified when pulling in sync --- .../readest-app/src/app/reader/hooks/useNotesSync.ts | 6 +++--- .../src/app/reader/hooks/useProgressSync.ts | 4 ++-- apps/readest-app/src/libs/sync.ts | 12 +++++++++--- apps/readest-app/src/pages/api/sync.ts | 6 +++++- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/apps/readest-app/src/app/reader/hooks/useNotesSync.ts b/apps/readest-app/src/app/reader/hooks/useNotesSync.ts index 94659302..e96ac67e 100644 --- a/apps/readest-app/src/app/reader/hooks/useNotesSync.ts +++ b/apps/readest-app/src/app/reader/hooks/useNotesSync.ts @@ -13,7 +13,7 @@ export const useNotesSync = (bookKey: string) => { const bookHash = bookKey.split('-')[0]!; useEffect(() => { - if (!config || !user) return; + if (!user) return; syncNotes([], bookHash, 'pull'); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); @@ -21,7 +21,7 @@ export const useNotesSync = (bookKey: string) => { const lastSyncTime = useRef(0); const syncTimeoutRef = useRef | null>(null); useEffect(() => { - if (!config || !user) return; + if (!config?.location || !user) return; const bookNotes = config.booknotes ?? []; const newNotes = bookNotes.filter( (note) => lastSyncedAtNotes < note.updatedAt || lastSyncedAtNotes < (note.deletedAt ?? 0), @@ -49,7 +49,7 @@ export const useNotesSync = (bookKey: string) => { }, [config]); useEffect(() => { - if (syncedNotes?.length && config) { + if (syncedNotes?.length && config?.location) { const newNotes = syncedNotes.filter((note) => note.bookHash === bookHash); if (!newNotes.length) return; const oldNotes = config.booknotes ?? []; diff --git a/apps/readest-app/src/app/reader/hooks/useProgressSync.ts b/apps/readest-app/src/app/reader/hooks/useProgressSync.ts index d87ab1cd..d79101bc 100644 --- a/apps/readest-app/src/app/reader/hooks/useProgressSync.ts +++ b/apps/readest-app/src/app/reader/hooks/useProgressSync.ts @@ -31,7 +31,7 @@ export const useProgressSync = ( }; useEffect(() => { - if (!config || !user) return; + if (!user) return; const bookHash = bookKey.split('-')[0]!; syncConfigs([], bookHash, 'pull'); return () => { @@ -43,7 +43,7 @@ export const useProgressSync = ( const lastProgressSyncTime = useRef(0); const syncTimeoutRef = useRef | null>(null); useEffect(() => { - if (!config || !user) return; + if (!config?.location || !user) return; const now = Date.now(); const timeSinceLastSync = now - lastProgressSyncTime.current; if (configSynced.current && timeSinceLastSync > SYNC_PROGRESS_INTERVAL_SEC * 1000) { diff --git a/apps/readest-app/src/libs/sync.ts b/apps/readest-app/src/libs/sync.ts index 5d2e5507..7ca2edd9 100644 --- a/apps/readest-app/src/libs/sync.ts +++ b/apps/readest-app/src/libs/sync.ts @@ -1,8 +1,14 @@ import { supabase } from '@/utils/supabase'; import { Book, BookConfig, BookNote, BookDataRecord } from '@/types/book'; import { READEST_WEB_BASE_URL } from '@/services/constants'; +import { isWebAppPlatform } from '@/services/environment'; -const BASE_URL = process.env['NODE_ENV'] === 'production' ? READEST_WEB_BASE_URL : ''; +// Develop Sync API only in development mode and web platform +// with command `pnpm dev-web` +const SYNC_API_ENDPOINT = + process.env['NODE_ENV'] === 'development' && isWebAppPlatform() + ? '/api/sync' + : `${READEST_WEB_BASE_URL}/api/sync`; export type SyncType = 'books' | 'configs' | 'notes'; export type SyncOp = 'push' | 'pull' | 'both'; @@ -32,7 +38,7 @@ export class SyncClient { const token = await this.getAccessToken(); if (!token) throw new Error('Not authenticated'); - const url = `${BASE_URL}/api/sync?since=${encodeURIComponent(since)}&type=${type ?? ''}&book=${book ?? ''}`; + const url = `${SYNC_API_ENDPOINT}?since=${encodeURIComponent(since)}&type=${type ?? ''}&book=${book ?? ''}`; const res = await fetch(url, { headers: { Authorization: `Bearer ${token}`, @@ -55,7 +61,7 @@ export class SyncClient { const token = await this.getAccessToken(); if (!token) throw new Error('Not authenticated'); - const res = await fetch(`${BASE_URL}/api/sync`, { + const res = await fetch(SYNC_API_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/apps/readest-app/src/pages/api/sync.ts b/apps/readest-app/src/pages/api/sync.ts index 3a6d9f8e..37c58f3e 100644 --- a/apps/readest-app/src/pages/api/sync.ts +++ b/apps/readest-app/src/pages/api/sync.ts @@ -101,7 +101,11 @@ export async function GET(req: NextRequest) { return NextResponse.json({ error: errorMsg }, { status: 500 }); } - return NextResponse.json(results, { status: 200 }); + const response = NextResponse.json(results, { status: 200 }); + response.headers.set('Cache-Control', 'no-store'); + response.headers.set('Pragma', 'no-cache'); + response.headers.delete('ETag'); + return response; } catch (error: unknown) { console.error(error); const errorMessage = (error as PostgrestError).message || 'Unknown error';