Preheat the oven to 400°F (200°C) and line a baking sheet with parchment paper.
Unroll the puff pastry onto the prepared baking sheet. Score a border approximately 1 inch from the edge without cutting completely through the pastry. Refrigerate while preparing the filling.
Melt the butter in a large skillet over medium heat.
Add the sliced shallots or onion and dried thyme. Season lightly with salt and black pepper.
Cook until the onions are softened and beginning to turn golden.
Add the minced garlic and cook briefly until fragrant. Transfer the mixture out of the skillet.
Add the olive oil and sliced mushrooms to the skillet.
Cook until the mushrooms release their moisture and begin to brown.
Return the onion mixture to the skillet. Add the balsamic vinegar and season with salt and black pepper.
Cook briefly until most excess moisture has evaporated. Remove from the heat and allow to cool slightly.
Spread the mushroom and onion filling over the center of the puff pastry, keeping the border clear.
Sprinkle the Gruyere evenly over the filling.
Brush the exposed pastry border with beaten egg.
Bake for 20–25 minutes, or until the pastry is puffed and deep golden and the cheese is melted.
Let rest for 5–10 minutes before slicing and serving.