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

Canonical URL remains when navigating between pages #3648

Open
davwheat opened this issue Sep 20, 2022 · 3 comments
Open

Canonical URL remains when navigating between pages #3648

davwheat opened this issue Sep 20, 2022 · 3 comments
Assignees
Labels

Comments

@davwheat
Copy link
Member

Current Behavior

When navigating through the SPA, the canonical found in the page <head> remains.

E.g., if you visit the forum home page, then navigate to a discussion, the canonical is still the home page URL.

Steps to Reproduce

Self-explanatory

Expected Behavior

Canonical should be updated with a new URL

Screenshots

No response

Environment

  • Flarum version: 1.5.0
  • Website URL: [local]
  • PHP version: 8.1.x

Output of php flarum info

No response

Possible Solution

No response

Additional Context

No response

@davwheat davwheat added this to the 1.6 milestone Sep 20, 2022
@davwheat davwheat self-assigned this Sep 20, 2022
@davwheat
Copy link
Member Author

In a project I'm working on, I've implemented this hack:

const oldPushState = window.history.pushState;

window.history.pushState = function (...args) {
  oldPushState.call(this, ...args);

  document.head.querySelectorAll('link[rel="canonical"]').forEach((link) => {
    if (link.getAttribute('href') !== window.location.href) {
      link.setAttribute('href', window.location.href);
    }
  });

  document.head.querySelectorAll('link[rel="next"], link[rel="prev"]').forEach((link) => {
    link.remove();
  });
};

@clarkwinkelmann
Copy link
Member

I always assumed it was by design. That attribute is only for the initial page load / scrapers, just like if OpenGraph data is added to the page.

I'm not sure how we can refresh this kind of data without making an API request that involves the Content classes on each page change.

Setting the canonical on page change without retrieving the right canonical URL seems like it might create more issues than solutions? The URLs generated client-side by the SPA are not guaranteed to be canonical.

Removing the tag after the first URL change does seem to make the most sense if it's interfering with JS libraries. Libraries can then fall back to the current browser URL which isn't guaranteed canonical.

@davwheat
Copy link
Member Author

The reason I ran into this issue was that some analytics providers utilise the canonical, if present, instead of the actual URL, like Fathom.

I wonder if we should remove it (and rel=prev/next) on navigation, but perhaps an extension could be better placed for updating it (perhaps somehow with a "get canonical" API endpoint?).

Not sure about the best approach here.

@SychO9 SychO9 modified the milestones: 1.6, 1.7 Nov 15, 2022
@SychO9 SychO9 modified the milestones: 1.7, 1.8 Mar 6, 2023
@luceos luceos modified the milestones: 1.8, 1.x May 20, 2023
@SychO9 SychO9 removed this from the 1.x milestone May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants