Preheat the oven and grease a baking dish generously with butter.
Melt butter in a skillet over medium heat, then whisk in flour and cook briefly until smooth and lightly golden.
Slowly pour in the milk followed by the vegetable broth, whisking continuously to create a smooth sauce.
Add garlic, thyme, salt, and pepper, then cook until the sauce slightly thickens and coats the back of a spoon. Remove from heat.
Arrange a layer of sliced potatoes in the bottom of the baking dish, followed by a layer of onions, a portion of the sauce, and a sprinkle of cheese.
Repeat the layering process with the remaining potatoes, onions, sauce, and cheese.
Cover the dish and bake until the potatoes begin to soften, then uncover and continue baking until the top is golden brown and the potatoes are fully tender.
Let the dish rest before serving so the layers set and the sauce thickens.