Preheat oven to 375°F and butter a 1.5 to 2-quart casserole dish.
In a medium saucepan over medium heat, melt the butter and add diced onion. Cook until soft, about 5 minutes.
Stir in flour and cook for another minute, then slowly whisk in the heated milk until smooth.
Bring mixture to a simmer, stirring constantly until thickened.
Stir in salt, pepper, and both cheeses until melted and smooth.
Layer half of the sliced potatoes in the prepared baking dish. Pour half the cheese sauce over the potatoes. Repeat with remaining potatoes and sauce.
Cover with foil and bake for 45 minutes. Remove foil, sprinkle with extra cheese if desired, and bake uncovered for another 15–20 minutes until golden and bubbly.
Let cool slightly before serving.