Learn how to handle deferred views in Obsidian for better performance
As of Obsidian v1.7.2, tabs in the background are deferred by default to improve performance. This guide explains what deferred views are and how to work with them in your plugins.
Deferred views are a performance optimization introduced in Obsidian v1.7.2. When a tab is in the background (not currently visible), Obsidian doesn’t fully load the view’s content. Instead, it shows a placeholder until the user switches to that tab.
This optimization significantly reduces memory usage and improves app responsiveness when many tabs are open.
The recommended way to ensure a view is loaded is to use Workspace.revealLeaf():
const leaf = this.app.workspace.getLeavesOfType('markdown')[0];if (leaf) { // Reveal the leaf (brings it to foreground and loads if deferred) await this.app.workspace.revealLeaf(leaf); // Now the leaf is active and fully loaded console.log('Leaf revealed and loaded');}
revealLeaf() handles everything: it brings the leaf to the foreground, loads it if deferred, and ensures it’s ready to use.
import { WorkspaceLeaf } from 'obsidian';const MY_VIEW_TYPE = 'my-custom-view';// Find leaves of your view typeconst leaves = this.app.workspace.getLeavesOfType(MY_VIEW_TYPE);if (leaves.length > 0) { const leaf = leaves[0]; // Ensure the view is loaded before using it await this.app.workspace.revealLeaf(leaf); // Now safe to interact with the view const view = leaf.view; // Use view...}
async openFile(file: TFile) { const leaf = this.app.workspace.getLeaf('tab'); // Open the file await leaf.openFile(file); // Ensure the view is loaded await this.app.workspace.revealLeaf(leaf); // Now the view is ready}
When you need to interact with a leaf, always use revealLeaf():
3
// Goodawait this.app.workspace.revealLeaf(leaf);const view = leaf.view;view.doSomething();// Avoidconst view = leaf.view; // Might be DeferredView!view.doSomething(); // May fail
4
Check View Type Before Casting
5
Always verify the view type before casting:
6
import { MarkdownView } from 'obsidian';const leaf = this.app.workspace.activeLeaf;if (leaf && !leaf.isDeferred) { const view = leaf.view; if (view instanceof MarkdownView) { // Safe to use as MarkdownView const editor = view.editor; }}
7
Handle Both States
8
Design your plugin to handle both deferred and loaded views:
9
function processLeaf(leaf: WorkspaceLeaf) { if (leaf.isDeferred) { console.log('Leaf is deferred, will process when loaded'); return; } // Process the loaded view const view = leaf.view; // ...}
10
Use Workspace Events
11
Listen for layout changes to know when views are loaded:
12
this.registerEvent( this.app.workspace.on('layout-change', () => { // Views may have been loaded or deferred this.updateViewList(); }));