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

Added canonical link to html head #1102

Merged
merged 4 commits into from
May 3, 2024
Merged

Conversation

jfkonecn
Copy link
Contributor

@jfkonecn jfkonecn commented Apr 4, 2024

fixes oncokb/oncokb-pipeline#321

@jfkonecn jfkonecn requested a review from zhx828 April 4, 2024 15:10
@jfkonecn jfkonecn added the bug label Apr 4, 2024
@jfkonecn jfkonecn self-assigned this Apr 4, 2024
@@ -166,13 +167,19 @@ const AppRouts = (props: {
isUserAuthenticated={props.authenticationStore.isUserAuthenticated}
appStore={props.appStore}
path={PAGE_ROUTE.GENE}
canonicalProps={{
__canonicalTypeName: 'location',
Copy link
Member

Choose a reason for hiding this comment

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

Why we use __ format here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to denote it's only need to help typescript with type hinting.
It's a convention I stole from a graphql library I used for CTDataHub.

Copy link
Member

Choose a reason for hiding this comment

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

Could you help me with "help typescript with type hinting"? We don't use the convention anywhere in the project, seems a bit odd to me to add only for this prop here. But want to know whether we should adopt across the board.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Basically I'm trying to know what object I'm looking at. If I use the "typeof" or "instanceof" keywords it will only tell me that it's an object. I could make a class with instanceof, but that seems like overkill so what I'm doing instead is telling typescript that there can only be a handful of hardcoded strings which are valid in the "canonicalTypeName" field. Since each string is unique to each object type, performing an equality check tells typescript exactly which type you're looking at.

Here's an example

Copy link
Contributor Author

@jfkonecn jfkonecn Apr 29, 2024

Choose a reason for hiding this comment

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

Maybe I could use the "in" operator instead

Comment on lines 30 to 32
: canonicalProps.__canonicalTypeName === 'function'
? canonicalProps.getCanonicalEndpoint(routeProps, removedPropsUrl)
: canonicalProps.__canonicalTypeName === 'location-without-url-params'
Copy link
Member

Choose a reason for hiding this comment

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

How are these two used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They aren't used anywhere in the PR.
I added them because I was going to used them, but refactored them away.

function allows you to hand a callback to the component to allow you to build the canonical url based on the information provided by react router.

location-without-url-params strips the url params from the url so for example this url
/gene/:hugoSymbol/:alteration
becomes
/gene

I can remove it if you think it'll make things too confusing.

Copy link
Member

Choose a reason for hiding this comment

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

It's ok without removing. But can we please add the comment above in the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can do

Copy link
Contributor Author

Choose a reason for hiding this comment

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

use switch case


linkNode.setAttribute('id', 'canonical');
linkNode.setAttribute('rel', 'canonical');
linkNode.setAttribute('href', `${location.host}${canonicalEndpoint}`);
Copy link
Member

Choose a reason for hiding this comment

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

I see wikipedia does include the protocol in their canonical link tag. Is that optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm doing this to fix this issue

image

The "Learn More" link takes you here

Copy link
Member

Choose a reason for hiding this comment

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

I meant whether we should include http or https

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I need to get better a reading. 😄
Yeah we need it. Good catch!

document.head.appendChild(linkNode);

return () => {
document.head.removeChild(linkNode);
Copy link
Member

Choose a reason for hiding this comment

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

Do you think this is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because I didn't add a canonical link for every route so I don't want to confuse the google crawler by having an incorrect link

Copy link
Member

Choose a reason for hiding this comment

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

Oh right, nvm. I thought the page is reloaded on every redirect, but we are SPA. In that case, do we need to look for the canonical tag before appending? Or is the dom is start enough to avoid dup?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Avoid duplication and to remove the link tag all together if we didn't specify needing one.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry how is it doing what you said above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It always deletes the link on a route change, but only adds it back when you specify in the OncokbRoute component that you want a canonical link.

<>
{React.createElement(component!, props)}

<CanonicalLink routeProps={props} canonicalProps={canonicalProps} />
Copy link
Member

Choose a reason for hiding this comment

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

Why the canonical link is a component?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I can hook into react lifetime and also have access to routeProps from react-router

@jfkonecn jfkonecn requested a review from zhx828 April 5, 2024 13:46
@jfkonecn jfkonecn added fix Fix tag for release and removed bug labels Apr 11, 2024
Copy link
Member

@zhx828 zhx828 left a comment

Choose a reason for hiding this comment

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

Thanks! Don't forget squashing when merging.

@jfkonecn jfkonecn merged commit ebb5e2c into oncokb:master May 3, 2024
6 checks passed
@jfkonecn jfkonecn deleted the google-search-fix branch May 3, 2024 17:43
jfkonecn added a commit to jfkonecn/oncokb-public that referenced this pull request May 13, 2024
jfkonecn added a commit to jfkonecn/oncokb-public that referenced this pull request Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Fix tag for release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants