How to detect when Figma document changes in plugin development

I’m working on a Figma plugin and need to monitor when the document gets modified. I’ve been looking at the event handling system but can’t figure out the right approach.

Is there a way to set up a listener that fires whenever someone makes changes to the current file? I want my plugin to respond automatically when users edit elements, add new layers, or modify properties.

I tried exploring the available event callbacks but they seem pretty limited for what I need. Has anyone found a reliable method to track document updates in real time?

yep, event handling can be a pain! use figma.on('documentchange', callback) to catch updates. it’s super handy for your plugin! good luck with it!

Yeah, documentchange is your best bet, but heads up - it fires constantly depending on what users do. Found this out the hard way when my plugin ran heavy operations on every change and completely lagged the interface. You’ll want to add some debouncing to avoid performance issues, especially if you’re doing complex processing. I usually add a small delay before running the main logic so rapid changes don’t trigger multiple expensive operations. Just know the event won’t tell you what specifically changed - you’ll need your own comparison logic if you want to know exactly what was modified.

Yeah, documentchange is what you want, but it’s got some quirks. I found out the hard way that it doesn’t fire for viewport changes or selection updates - just actual document edits. Also heads up: it triggers before the change is fully done, so if you try reading the updated state right away, you’ll get stale data. I had to add a small timeout in my handler to let the DOM catch up. Oh, and it won’t fire for changes your own plugin makes, which prevents infinite loops but can mess with your head when you’re testing.