Render GlLink as an anchor when there is no $router

What does this MR do?

Fixes #3719, a regression from !6541 (merged). It also covers a case that MR left open.

Nuxt without a router

Live examples on the docs site mount as standalone new Vue instances with no router. Nuxt's $nuxt getter falls back to window.$nuxt, so these instances still have $nuxt. Since the check order changed in !6541 (merged), GlLink and BLink render nuxt-link for any to when $nuxt is set. Without a router, nuxt-link throws Cannot read properties of undefined (reading 'resolve'), and pages like /components/dropdown-disclosure and /components/breadcrumb render empty under yarn start.

This MR returns an anchor when there is no router, and checks $nuxt only to choose between nuxt-link and router-link.

A $router getter that throws

With Vue 2, VueRouter's $router getter throws on an instance created before VueRouter was installed (gitlab-org/gitlab#634928 (closed)). !6541 (merged) avoided the read for links without to, but a link with to still threw and rendered as <!---->.

Such an instance can never have a router: VueRouter connects an instance to a router only in its beforeCreate hook, and any app with a router is created after the install. So a new getRouter helper in vue_utils.js (and a local copy in the vendored router.js) catches the error and treats it as no router. Re-rendering after the install wouldn't help, because the result would still be an anchor.

Why not check '$router' in this

VueRouter 3 defines $router as a getter on Vue.prototype. After any Vue.use(VueRouter), every instance has the property, even one with no router of its own. So in doesn't tell you whether the instance has a router, and that version fails 4 of the existing link.spec.js tests under Vue 2.

How it was verified

  • New specs:
    • "$nuxt set, no $router" in link.spec.js and router.spec.js
    • "$router throws" with to in link.spec.js and router.spec.js
    • getRouter in vue_utils.spec.js
  • The new link.spec.js and router.spec.js cases fail on main and pass with this change.
  • All link, router, vue_utils, BLink and dropdown-item specs pass with VUE_VERSION=2 and VUE_VERSION=3.
  • Visiting http://localhost:2018/components/dropdown-disclosure after yarn start now works.

Screenshots or screen recordings

None. No visual change.

Integrations

  • GitLab: not needed, no API change. The fix reaches GitLab with the next @gitlab/ui bump.

Does this MR meet the acceptance criteria?

This checklist encourages the authors, reviewers, and maintainers of merge requests (MRs) to confirm changes were analyzed for conformity with the project's guidelines, security and accessibility.

Toggle the acceptance checklist

Conformity

  • The “What does this MR do?” section in the MR description is filled out, explaining the reasons for and scope of the proposed changes, per “Say why not just what”.
    • For example, if the MR is focused on usage guidelines, addressing accessibility challenges could be added in a separate MR.
  • Relevant label(s) are applied to the MR.
  • The MR is added to a milestone.
  • Added the ~"component:*" label(s) if applicable.
  • A changeset is added when this MR will do a patch, minor, or major update. More information can be found in doc/changesets.md.

Components

Documentation

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • Security reports checked/validated by a reviewer from the AppSec team

Accessibility

If this MR adds or modifies a component, take a few moments to review the following:

  • All actions and functionality can be done with a keyboard.
  • Links, buttons, and controls have a visible focus state.
  • All content is presented in text or with a text equivalent. For example, alt text for SVG, or aria-label for icons that have meaning or perform actions.
  • Changes in a component’s state are announced by a screen reader. For example, changing aria-expanded="false" to aria-expanded="true" when an accordion is expanded.
  • Color combinations have sufficient contrast.
Edited by Mark Florian

Merge request reports

Loading
Loading