Heat a large, heavy saucepan over low-medium heat and add the butter, onions, salt and pepper. Cook slowly, stirring regularly, for about 45-60 minutes until the onions are well caramelised (see recipe notes).
Add the bay leaves, thyme, white wine and port or sherry, then increase the heat and bring to a simmer. Simmer until the wine stops smelling alcoholic, then add the stock and gelatine. If you are using home-made stock, you can leave out the gelatine (see recipe notes). Reduce the heat and simmer, covered, for 20 minutes. Taste the soup and adjust the seasoning.
Heat the oven to 160C fan-forced (180C conventional). While the soup is simmering, lightly butter the baguette slices, placing them on a rack over a baking tray. Toast until lightly browned, then remove from the oven and set aside.
Fill ovenproof serving bowls with the soup, leaving a few centimetres clear at the top of each bowl. Add 2 slices of toasted baguette to each bowl, season with a little salt and cover with grated cheese. Place the bowls under the oven grill and grill until the cheese is browned and bubbling (see recipe notes). Serve scattered with a few extra thyme leaves.
