Physical distancing and handwashing can be important infection prevention measures during an infectious disease outbreak such as the COVID-19 pandemic. To stimulate these behaviours, knowledge of psychosocial determinants as well as contextual factors is vital. We present longitudinal, within-person analyses of the impact of contextual and psychosocial factors on handwashing and distancing behaviour. We used individual-level data (186,490 participants completing 971,899 surveys) from the Corona Behavioural Unit COVID-19 Cohort, a dynamic cohort study conducted during 26 months of the COVID-19 pandemic in the Netherlands. Fixed-effects models were employed to estimate within-person associations between psychosocial factors and behaviour, combined with main and moderating effects of contextual factors. Pandemic severity was associated with more handwashing and distancing behaviour, while the duration of the pandemic had little effect. Within-person changes in response efficacy were most relevant for changes in both handwashing and distancing behaviour, while self-efficacy, descriptive norms and perceived severity of infecting others affected behaviour indirectly. These effects were stable over time. Associations were larger in cross-sectional models, indicating that such models tend to overestimate effects. Our study highlights the importance of longitudinal data and within-person models to detect possible causal associations. The results suggest that during an outbreak, government and public health professionals should clearly communicate the severity of the pandemic (e.g., hospitalization rates) and the effectiveness of recommended prevention measures in reducing that risk; and seek to improve people's capabilities and opportunities to adhere to guidelines, for example, by modifying the environment.