Preheat the oven to 220°C/425°F/gas 7.
Peel the onions and halve across the middle.
Place the butter in a 26cm non-stick ovenproof frying pan on a medium heat.
Strip in the thyme leaves and add the bay, shake the pan around and get it bubbling.
Add the sugar, vinegar and 100ml of water.
Place the onion halves in the pan, cut side down.
Peel and halve the garlic cloves and place in the gaps.
Season generously with sea salt and black pepper.
Cover, turn the heat down to low and leave to steam for 10 minutes to soften the onions slightly.
Remove the lid and cook until the liquid starts to caramelise, gently shaking the pan occasionally to stop it from sticking.
Place the pastry over the onions, using a wooden spoon to push it right into the edges of the pan.
Bake for 35 minutes, or until golden brown and puffed up.
Using oven gloves to protect your hands, pop a large plate over the pan and confidently but very carefully turn out.
