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

feat(blog): slug/pathname aliases for blog posts #8356

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

johnnyreilly
Copy link
Contributor

@johnnyreilly johnnyreilly commented Nov 20, 2022

Pre-flight checklist

Motivation

It would be great to have alias URLs for blog posts, so as well as /my-blog-post you could have /my-other-url-for-blog-post.

Ideally the canonical link remains pointing to the permalink. HOWEVER I'M NOT SURE HOW BEST TO IMPLEMENT THIS PART

Consider:

// Default canonical url inferred from current page location pathname
function useDefaultCanonicalUrl() {
const {
siteConfig: {url: siteUrl},
} = useDocusaurusContext();
const {pathname} = useLocation();
return siteUrl + useBaseUrl(pathname);
}

The canonical URL is driven just by the pathname of the page. There's not an obvious way to provide a canonical URL to this without serious code changes. Rather than do this unsupervised, I wanted to ask if there was an approach that might be good approach to take?

Test Plan

Please see the new tests / amended tests.

Test links

Deploy preview: https://deploy-preview-8356--docusaurus-2.netlify.app/

Related issues/PRs

#8311

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Nov 20, 2022
metadata.aliases.forEach((alias) => {
addRoute({
path: alias,
component: blogPostComponent,
Copy link
Collaborator

Choose a reason for hiding this comment

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

IIUC, this would create two pages with the same content? Isn't it simpler to set up redirects instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @Josh-Cena,

The context for this is in @slorber's comment here: #8311 (reply in thread)

---
slug: /2022/11/17/azure-ad-claims-static-web-apps-azure-functions
aliases: ["/"] # NEW FEATURE
---

# Azure AD Claims with Static Web Apps and Azure Functions

Text

The aliasing feature would create extra pages for another slug (that can be /)

It gives the ability to keep a shared canonical URL for both pages and avoid SEO content being flagged as duplicate.

Does this make sense?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes that makes sense to me, and we'll likely want to add something similar to individual docs for consistency

@netlify
Copy link

netlify bot commented Nov 20, 2022

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit ab1cf60
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/6584653b2298f500087302ce
😎 Deploy Preview https://deploy-preview-8356--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions
Copy link

github-actions bot commented Nov 20, 2022

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🟠 73 🟢 98 🟢 100 🟢 100 🟠 89 Report
/docs/installation 🟠 68 🟢 98 🟢 100 🟢 100 🟠 89 Report
/docs/category/getting-started 🟠 76 🟢 100 🟢 100 🟢 90 🟠 89 Report
/blog 🟠 75 🟢 100 🟢 100 🟢 90 🟠 89 Report
/blog/preparing-your-site-for-docusaurus-v3 🟠 61 🟢 97 🟢 100 🟢 100 🟠 89 Report
/blog/tags/release 🟠 76 🟢 100 🟢 100 🟠 80 🟠 89 Report
/blog/tags 🟠 78 🟢 100 🟢 100 🟢 90 🟠 89 Report

@slorber slorber added the pr: new feature This PR adds a new API or behavior. label Nov 24, 2022
@slorber slorber changed the title Aliases for blog posts feat(blog): slug/pathname aliases for blog posts Nov 24, 2022
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

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

Thanks, added some comments

We should rather have some dogfood cases to see it in action.


Note that we should also take care of the UI aspect.

For example, suppose the blog sidebar highlights the currently browsed blog post. In that case, it should probably keep being highlighted if we browse it from the permalink URL or any of the aliases. The theme code probably doesn't account for this yet. There are also things to consider like navbar items highlighting, although people rarely link to blog post URLs from the navbar and can use the regexp active fn.


An alternative implementation that is likely faster than rendering twice the same blog post could be to just copy the static file in the build output, inside the postBuild hook?

cp build/blog/folder/myBlogPost.html build/blog/another-folder/myBlogPostAlias.html

Pro: much faster if there are many aliases (unlikely? 🤔 )
Cons: worst DX, aliases not working in dev unless we implement something. A Hybrid solution could also make sense.

We can probably think about perf later, and current implementation is likely good enough.

{aliases: ['../blog']},
{aliases: ['../../blog']},
{aliases: ['/api/plugins/@docusaurus/plugin-debug']},
{aliases: ['@site/api/asset/image.png']},
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure we should accept such value 😅

This code is unlikely to yield a great pathname

normalizeUrl([baseUrl, routeBasePath, alias])

and the tests do not cover these edge cases.


The question to me is: do we want to support relative aliases?

Is this really useful? I mean, you create a relative alias, then update the slug, and then you end up with a different alias. That might be a source of bug/confusion?

If we do want relative aliases, there's a resolvePathname util that could probably be applied to compute the final non-relative pathname of the alias.

But I would suggest to keep it simple for now and only accept absolute pathname aliases in validation? If someone wants a relative alias someday and has great reason to add it, we'll do an update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The question to me is: do we want to support relative aliases?

Probably not - will amend

metadata.aliases.forEach((alias) => {
addRoute({
path: alias,
component: blogPostComponent,
Copy link
Collaborator

Choose a reason for hiding this comment

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

yes that makes sense to me, and we'll likely want to add something similar to individual docs for consistency

packages/docusaurus-plugin-content-blog/src/index.ts Outdated Show resolved Hide resolved
@johnnyreilly
Copy link
Contributor Author

Thanks for the comments - I'll take a look at them. But before doing I wanted to get your thoughts on the below: (copied from the motivation section up top). Given that this will likely change implementation somewhat, I wanted to tackle that before the code comments. Hope this makes sense!

Motivation

It would be great to have alias URLs for blog posts, so as well as /my-blog-post you could have /my-other-url-for-blog-post.

Ideally the canonical link remains pointing to the permalink. HOWEVER I'M NOT SURE HOW BEST TO IMPLEMENT THIS PART

Consider:

// Default canonical url inferred from current page location pathname
function useDefaultCanonicalUrl() {
const {
siteConfig: {url: siteUrl},
} = useDocusaurusContext();
const {pathname} = useLocation();
return siteUrl + useBaseUrl(pathname);
}

The canonical URL is driven just by the pathname of the page. There's not an obvious way to provide a canonical URL to this without serious code changes. Rather than do this unsupervised, I wanted to ask if there was an approach that might be good approach to take?

@slorber
Copy link
Collaborator

slorber commented Dec 8, 2022

I guess this logic should only be used for as a fallback for pages that do not define an explicit canonical URL (which is likely the case for user-created standalone pages)

If a content-plugin supports the aliasing feature then it probably needs to declare the canonical url deeper in the tree, to override this fallback value.

That probably makes sense to add it to the PageMetadata helper considering it sets 2 meta at once and could be reused in the future

CleanShot 2022-12-08 at 18 02 38@2x

It is ok to declare the same meta multiple times in the tree, Head is supposed to deduplicate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants