Heat the olive oil in a large skillet or Dutch oven over medium heat. Add the sliced onions and 1 tsp of the salt, then stir in the 2 tbsp water. Cover and cook for 10 minutes to sweat the onions down.
Uncover, reduce the heat to medium-low, and cook for 45-60 minutes, stirring every 5-10 minutes. Whenever the pan looks dry or the fond on the bottom starts to darken, add a splash of water and scrape it up. The onions are done when they are jammy and deep mahogany brown.
Add the minced garlic, rosemary, and thyme and cook for 1 minute until fragrant.
Pour in the beef broth and Worcestershire sauce. Simmer, stirring, for 5-8 minutes until the liquid has nearly all cooked off and the onions are glazed, not soupy. Stir in the remaining ½ tsp salt. Set aside.
Melt the 4 oz (113 g) butter in a saucepan over medium heat. Whisk in the 2.3 oz (½ cup / 65 g) flour and cook, whisking constantly, for 2 minutes — it should smell nutty but stay pale.
Add the milk about 1 cup at a time, whisking smooth after each addition before adding more.
Bring to a gentle simmer and cook, whisking, for 4-5 minutes until the sauce thickens enough to coat the back of a spoon. Stir in the salt, pepper, and rosemary. Remove from the heat.
Fold the caramelized onions and most of the pancetta into the white sauce.
Heat the oven to 375°F (190°C). Butter a 9x13 in (23x33 cm) baking dish.
Spread a thin layer of the onion sauce in the bottom of the dish. Shingle one third of the potato slices over it in an even, overlapping layer.
Spoon over one third of the remaining sauce and scatter with a handful of gruyère. Repeat for two more layers, finishing with sauce.
Cover tightly with foil and bake for 45 minutes, until a knife slides through the center with no resistance.
Uncover, top with the remaining gruyère and the reserved pancetta, and bake for another 25-30 minutes until bubbling and browned on top. Broil for 2-3 minutes if you want more color.
Rest for 20 minutes before serving so the sauce sets up enough to cut clean portions.
Butter and flour are weighed. A scooped cup of flour can run 20% heavy, which turns the béchamel pasty and gluey. Weigh both for a sauce that pours.
Don't rush the onions. The whole character of the dish comes from the fond. If they are browning in under 30 minutes the heat is too high and you are frying, not caramelizing. Add water and scrape every time the pan goes dry.
Salt, pepper, herbs, and Worcestershire stay as volume measures — they all land under about 5 g, which is below the resolution of most home scales.
Slice the potatoes evenly. A mandoline at ⅛ in (3 mm) is the easiest way to get layers that cook at the same rate. Do not soak the slices after slicing — the surface starch helps thicken and bind the layers.
Make ahead. Assemble fully, cover, and refrigerate up to 24 hours. Add 15 minutes to the covered bake time from cold.
Swaps. Bacon works in place of pancetta; cut back on added salt since it is usually saltier. Comté or a sharp Swiss substitute for the gruyère. Use vegetable broth and skip the pancetta for a vegetarian version.