build middleware only once