fix: pre-create user_preferences row in getUserPrefs to prevent loop
createRow inside a Server Action triggers Next.js router cache invalidation → layout reruns → components remount → loop. By creating the empty row from getUserPrefs (Server Component, not Server Action), saveUserPrefsAction always hits updateRow which doesn't cause cache invalidation.
This commit is contained in:
@@ -27,7 +27,26 @@ export async function getUserPrefs(): Promise<UserPrefs> {
|
|||||||
queries: [Query.equal("userId", user.$id), Query.limit(1)],
|
queries: [Query.equal("userId", user.$id), Query.limit(1)],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (result.rows.length === 0) return {};
|
if (result.rows.length === 0) {
|
||||||
|
// Pre-create an empty row so saveUserPrefsAction always calls updateRow
|
||||||
|
// (createRow inside a Server Action causes router cache invalidation → remount loop)
|
||||||
|
try {
|
||||||
|
await tablesDB.createRow(
|
||||||
|
DATABASE_ID,
|
||||||
|
TABLES.userPreferences,
|
||||||
|
ID.unique(),
|
||||||
|
{ userId: user.$id },
|
||||||
|
[
|
||||||
|
Permission.read(Role.user(user.$id)),
|
||||||
|
Permission.update(Role.user(user.$id)),
|
||||||
|
Permission.delete(Role.user(user.$id)),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
// race condition or already exists — fine
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
const row = result.rows[0] as Record<string, unknown>;
|
const row = result.rows[0] as Record<string, unknown>;
|
||||||
const str = (v: unknown) => (v && typeof v === "string" ? v : undefined);
|
const str = (v: unknown) => (v && typeof v === "string" ? v : undefined);
|
||||||
|
|||||||
Reference in New Issue
Block a user