Skip to content

Commit 3a8ec63

Browse files
committed
Process image when source updated instead of on create
1 parent 0f62977 commit 3a8ec63

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

functions/src/image/create-image-derivatives.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ import { processSource } from "./process";
33

44
export const createImageDerivatives = functions.firestore
55
.document("images/{imageId}")
6-
.onCreate(async (snap, context) => {
7-
const { source } = snap.data();
8-
const { imageId } = context.params;
6+
.onWrite(async (snap, context) => {
7+
const source = snap.after.data()?.source;
98
if (!source) {
109
console.log("No source field, ignoring write");
1110
return;
1211
}
1312

13+
if (source === snap.before.data()?.source) {
14+
console.log("Source field not updated, ignoring write");
15+
return;
16+
}
17+
18+
const { imageId } = context.params;
1419
const outputs = await processSource(source, imageId);
1520

1621
await Promise.all(
@@ -28,5 +33,5 @@ export const createImageDerivatives = functions.firestore
2833
};
2934
});
3035

31-
await snap.ref.update({ formats });
36+
await snap.after.ref.update({ formats });
3237
});

0 commit comments

Comments
 (0)