Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adding legacy hash link support #2581

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

patricknelson
Copy link
Member

@patricknelson patricknelson commented Feb 20, 2025

Fixes legacy hash links and redirects to the correct component or guide page

  • This PR contains CSS changes
  • This PR does not contain CSS changes

Description

Ensures that links going to the legacy URLs such as the following are properly redirected. Example test cases:

  • #menu-button-radio redirects to component/menu-button/#menu-button-radio (retains deep link)
  • #menu-button redirects to component/menu-button/ (no trailing hash)
  • #menu redirects to components/menu/ (longer names above are greedily matched first to prevent partial mismatches)
  • Non-component edge cases:
    • #animation redirects to guide/animation/.
    • The old "Setup" navigation links to #about, #install, #bundling, #token-system and #themes are still valid for the original landing page.
    • The new "page grid" guide didn't exist (so the old #page-grid still redirects as one would expect to its corresponding component page).

For components, it redirects safely by ensuring that it matches longer component names first and it automatically matches based on actual URLs mentioned in the page navigation (to prevent blindly redirecting to arbitrary URLs).

Operates on the following conventions:

  • Component names are hyphenated lowercase strings
  • Hash URLs are hyphenated lowercase alphanumeric strings (e.g. allows numbers for deep links such as #pagination-overflow-horizontal-24-control or #toggle-button-group-x1)
  • "Deep" links share the prefix with the component name. This is also accounting for an edge case from the refactor in my last PR fix(docs): fix broken internal hash links #2573, which renamed tab-default and tab-fake to be consistently tabs- prefixed

Testing locally

git clone https://github.com/patricknelson/skin.git
cd skin
git checkout fix-legacy-links
npm i
npm run start

Links from above (for validation):

Checklist

This is an ac hoc cowboy PR (no issue was created for this) 🤠

  • I verify the build is in a non-broken state
  • I verify all changes are within scope of the linked issue

@patricknelson patricknelson marked this pull request as ready for review February 20, 2025 20:24
// Legacy link support: For the hash-based links dating before Jan 2025 when we transitioned to a multi-page site
// For example, redirects from /#menu-button-radio to /component/menu-button#menu-button-radio
document.addEventListener("DOMContentLoaded", function() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This waits for DOMContentLoaded so it can verify that the currently referenced #hash link doesn't already exist as a link to the current page (to prevent accidental redirects). Note that this code short circuits on component/guide pages anyway (see below); but this check is just to be safe. Of course, if we ran synchronously here, the DOM parser wouldn't have had time to load elements on the page so a query select would fail (thus the need to wait a little).

return;
}
} while ((lastIndex = partialHash.lastIndexOf("-")) !== -1);
Copy link
Member Author

@patricknelson patricknelson Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done instead of either blindly redirecting (very insecure) or packing the entire component link list (as an allow list). Instead, we'll just reference what's already on the page navigation using an educated guess (i.e. checking to see if a /component/HASH/ link is present).

It goes from longest to shortest to help match overlapping prefixes.

@patricknelson
Copy link
Member Author

Btw, lmk if I'm too verbose in my JS comments; I can cull those down a little bit if you want.

@ArtBlue
Copy link
Contributor

ArtBlue commented Feb 20, 2025

Thanks @patricknelson ! I'll take a closer look next week after our release. In my initial glance at it though, why not just update the old hashlinks in the html? Feels a bit of an over-engineering, but I'll take a closer look next week.

@patricknelson
Copy link
Member Author

patricknelson commented Feb 20, 2025

why not just update the old hashlinks in the html

This is to support external incoming links that have built up over the years (e.g. in Slack or saved in jira, documentation and etc) which are referencing the old /skin/#hash for components. Those links cannot be easily changed in bulk, so as an alternative one way to support changes/transitions such as these where the URLs to pages have occurred on our end is to redirect to the correct page if we know where it was intended to go.

Does that make sense @ArtBlue or did I misinterpret your question?

Edit: p.s. The internal linking issues were already addressed in #2573 (described in #2570), where just within the Skin documentation, cross linking between components was broken. This addresses old links living everywhere else. 🙂

@patricknelson
Copy link
Member Author

Also worth noting that, unfortunately, there's no way to fix all of those broken inbound links external to the site without client-side processing, since #hash links are only available in browser (plus this is a static site anyway, I believe). Anyway, this is good for me and I'm sure others who've got links to this doc site stored across their various documentation, Jira tickets and other places. This even includes links old enough to be pointing to http://ebay.github.io/skin/#hash, since that #hash is retained in the redirect.

I could probably do a better job at the top of the DOMContentLoaded callback to explain the purpose of this redirect code.

Copy link

changeset-bot bot commented Feb 20, 2025

⚠️ No Changeset found

Latest commit: 47ab108

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants