In a large pan over medium heat, heat olive oil or butter. Add onions, salt, and optional sugar. Cook, stirring frequently, for about 20–30 minutes until golden brown and caramelized. Stir in thyme if using.
For fresh spinach: Add to the onions during the last 2–3 minutes and cook until wilted. For frozen spinach: Add it after onions are caramelized, stirring to combine.
Preheat oven to 350°F (175°C).
In a bowl, whisk eggs, milk, mozzarella, optional nutmeg, salt, and pepper.
Spread the onion-spinach mixture in a greased pie or baking dish. Pour the egg mixture over the top.
Bake for 30–35 minutes until set and golden on top. A knife inserted in the center should come out clean.
Let cool slightly before slicing. Enjoy warm or at room temperature.
