Tag requests sent while the user is idle

What does this MR do and why?

This is the frontend half of stopping idle background polls from extending the user's session.

Any authenticated request refreshes the Rails session. A tab that only polls (MR widget, pipelines list) therefore never idle-times-out, even if nobody is using it.

The backend half is !258104 (merged) (draft). Classifying requests on the server by read vs write alone is too wide: in Vue single-page views, deliberate actions (route changes, filtering, paging, search) are also reads, so an active user could be signed out. This MR gives the server a signal about whether the user is actually present.

What changes:

  • New ~/lib/utils/user_activity.js records the time of the last user input: pointerdown, pointermove, keydown, wheel, scroll.
    • Listeners are passive and capture-phase on window, so non-bubbling scroll on elements counts too.
    • Page load counts as activity.
    • isUserIdle() is true after 60 seconds without input.
  • The existing axios request interceptor in axios_utils.js (the one that counts pending requests) adds X-GitLab-Background-Request: 1 to same-origin requests while the user is idle. It reuses that interceptor because a separate one would add an asynchronous step to every request and shift request timing app-wide (it broke a Vue 3 spec that way). Cross-origin requests are not tagged, because a custom header there needs the other server to allow it in CORS.
  • New Apollo link ~/lib/apollo/background_request_link.js adds the same header to GraphQL requests. It sits in the link chain in lib/graphql.js, before the batch/HTTP links.

Effect on its own: none. The backend does not read the header yet.

When !258104 (merged) lands and its skip_session_extension_for_background_requests feature flag is on, the server skips the session write only if the header is present AND the request is a read. Writes and navigations always extend the session.

Notes:

  • Requests that bypass axios and Apollo (direct fetch, EventSource) are never tagged and keep today's behavior.
  • There is no frontend feature flag. The header is inert until the backend flag is on, and turning that flag off undoes the effect.

References

Screenshots or screen recordings

Not applicable: no visible UI change.

How to set up and validate locally

No feature flag is needed for this MR.

  1. Check out this branch.
  2. Open a page that polls, for example a merge request page (MR widget polling) or a project's Pipelines page.
  3. Open the browser DevTools Network tab and leave the tab without any input (mouse, keyboard, scroll, touch) for over a minute.
  4. Confirm the polling requests (JSON and graphql) now carry the request header X-GitLab-Background-Request: 1.
  5. Move the mouse, click, or type. Confirm the next requests no longer carry that header.
  6. Confirm cross-origin requests never carry the header.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Eduardo Sanz García

Merge request reports

Loading
Loading