Preheat your oven to 400°F (200°C). Line a baking sheet with parchment paper.
Snap off the tough ends of the asparagus spears. In a bowl, toss the asparagus with olive oil, minced shallot (if using), and minced garlic (if using). Season lightly with salt and freshly ground black pepper.
Gently unfold the thawed puff pastry sheet on the prepared baking sheet. If it's a large rectangle, you can leave it as is. If it's folded, gently unfold it.
If desired, create a slight border around the edge of the puff pastry (about 1 inch) by scoring a line with a knife, being careful not to cut all the way through. This will help the edges puff up nicely. You can also gently crimp the edges with a fork for a decorative look.
Sprinkle about 3/4 of the shredded Gruyere cheese evenly over the base of the puff pastry, staying within your scored border (if you made one). If using Parmesan cheese, sprinkle it over the Gruyere.
Arrange the oiled asparagus spears in a single layer over the cheese, ensuring they are relatively even.
Sprinkle the remaining Gruyere cheese over the asparagus.
Brush the edges of the puff pastry (the border area) with the lightly beaten egg wash. This will help them turn golden brown and crispy.
Carefully transfer the baking sheet to the preheated oven and bake for 25-30 minutes, or until the puff pastry is golden brown and puffed up, and the asparagus is tender-crisp. The cheese should be melted and bubbly.
Let the tart cool on the baking sheet for a few minutes before slicing and serving. It's delicious warm or at room temperature.