In the quest for the development of faster and more reliable technologies, the abilityto control the propagation, confinement, and emission of light has become crucial. Thedesign of guide mode resonators and perfect absorbers has proven to be of fundamentalimportance. In this project, we consider the shape optimization of a periodic dielectricslab aiming at efficient directional routing of light to reproduce similar features of a guidemode resonator. For this, the design objective is to maximize the routing efficiency of anincoming wave. That is, the goal is to promote wave propagation along the periodic slab.A Helmholtz problem with a piecewise constant and periodic refractive index mediummodels the wave propagation, and an accurate Robin-to-Robin map models an exteriordomain. We propose an optimal design strategy that consists of representing the dielectricinterface by a finite Fourier formula and using its coefficients as the design variables.Moreover, we use a high order finite element (FE) discretization combined with a bilinearTransfinite Interpolation formula. This setting admits explicit differentiation with respectto the design variables, from where an exact discrete adjoint method computes thesensitivities. We show in detail how the sensitivities are obtained in the quasi-periodicdiscrete setting. The design strategy employs gradient-based numerical optimization, whichconsists of a BFGS quasi-Newton method with backtracking line search. As a test caseexample, we present results for the optimization of a so-called single port perfect absorber.We test our strategy for a variety of incoming wave angles and different polarizations. Inall cases, we efficiently reach designs featuring high routing efficiencies that satisfy therequired criteria.