01 - Preheat the oven to 350°F. Grease a 9x13 inch baking dish with butter.
02 - Melt butter in a saucepan over medium heat. Add onion if using, and sauté until softened, about 3 minutes. Stir in garlic and cook for 1 minute until fragrant.
03 - Add heavy cream, whole milk, salt, black pepper, and nutmeg to the saucepan. Warm gently until steaming but not boiling, then remove from heat.
04 - Arrange half of the sliced potatoes evenly in the prepared baking dish, slightly overlapping. Pour half of the cream sauce over the potatoes and sprinkle half of the shredded cheddar and Gruyère cheeses on top.
05 - Layer the remaining sliced potatoes evenly over the first layer. Pour the remaining cream sauce followed by the rest of the shredded cheeses.
06 - Cover the dish tightly with aluminum foil and bake in the preheated oven for 45 minutes.
07 - Remove the foil and continue baking for an additional 15 to 20 minutes until the top is golden brown, bubbly, and potatoes are tender when pierced with a knife.
08 - Allow to rest for 10 minutes before serving. Optionally, garnish with chopped fresh chives or parsley.