Updating three.js scene on window re-size

I used the following code for updating everything(camera, renderer etc) when I resize my window so that nothing distorts. It seems to work perfectly while manually resizing the window, however, when I maximize my window by just clicking the maximize icon, the images distort and the update loop breaks. Anyone familiar with this problem? thanks!

//resize
window.addEventListener('resize', () => {
  sizes.width = window.innerWidth
  sizes.height = window.innerHeight
  //update camera
  camera.updateProjectionMatrix()
  camera.aspect = sizes.width / sizes.height
  renderer.setSize(sizes.width, sizes.height)
})

const loop = () => {
  renderer.render(scene, camera)
  window.requestAnimationFrame(loop)
}

loop();