Fix potential 304 Not Modified when pulling in sync

This commit is contained in:
chrox
2024-12-25 17:25:44 +01:00
parent a16ca22814
commit 8f26f9a7fa
4 changed files with 19 additions and 9 deletions
@@ -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<number>(0);
const syncTimeoutRef = useRef<ReturnType<typeof setTimeout> | 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 ?? [];
@@ -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<number>(0);
const syncTimeoutRef = useRef<ReturnType<typeof setTimeout> | 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) {
+9 -3
View File
@@ -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',
+5 -1
View File
@@ -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';