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(hashi-head): validate props.image, add twitter:description #623

Merged
merged 9 commits into from
Jun 22, 2022

Conversation

zchsh
Copy link
Contributor

@zchsh zchsh commented Jun 20, 2022

🎟️ Asana Task
πŸ” Preview Link


This PR adds validation to try to ensure that the image prop provided to @hashicorp/react-head is an absolute URL. This is to match the Open Graph protocol, which notes All valid URLs that utilize the https:// or https:// protocols.

PR Checklist πŸš€

Items in this checklist may not may not apply to your PR, but please consider each item carefully.

  • Add Asana and Preview links above.
  • Conduct thorough self-review.
  • Add or update tests as appropriate.
  • Conduct reasonable cross browser testing for both compatibility and responsive behavior (We have a Sauce Labs account for this, if you don't have access, just ask!).
  • Conduct reasonable accessibility review (use the WAS as a guide or an axe browser plugin until we establish more formal checks).
  • Identify (in the description above) and document (add Asana tasks on this board) any technical debt that you're aware of, but are not addressing as part of this PR.

@vercel
Copy link

vercel bot commented Jun 20, 2022

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Updated
react-components βœ… Ready (Inspect) Visit Preview Jun 22, 2022 at 9:41PM (UTC)

@changeset-bot
Copy link

changeset-bot bot commented Jun 20, 2022

πŸ¦‹ Changeset detected

Latest commit: 29892d3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hashicorp/react-head Minor

Not sure what this means? Click here to learn what changesets are.

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

@zchsh zchsh marked this pull request as ready for review June 21, 2022 22:33
key="description"
/>
<meta
// TODO: are we sure we need this? Seems like will fall back to OG
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BRKalow wanted to confirm - do we strictly need twitter:description? It seems like it should fall back to og:description (ref: https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/markup). Wondering whether we should omit it (or perhaps duplicate all such fallback tags with twitter: specific versions, for consistency?)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should be able to test this both with & without twitter:description using the Twitter card validator. Have you tried it out yet to help confirm the necessity of this tag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

βœ… Great call, and somewhat contrary to Twitter' docs, it does seem necessary (tested in hashicorp/dev-portal#658).

Copy link
Contributor

Choose a reason for hiding this comment

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

UGH that's so annoying 😭 but great tests! πŸ™ŒπŸ» Glad that was fairly straightforward to test.

* @param string The URL to test
* @returns true if the URL is absolute, false otherwise
*/
function isAbsoluteUrl(string: string): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pulled in from hashicorp/dev-portal (and elsewhere).

Copy link
Contributor

Choose a reason for hiding this comment

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

(Question πŸ™‹) Can some examples of what is and isn't an absolute URL be added to the block comment above this function? That will help make it clear at a glance how this function works. I end up having to look it up every time. πŸ˜…

(Future πŸ“Œ) Tests would be nice as well, but maybe this is a helper we should prioritize moving to @hashicorp/platform-util first? Sounds like it's very widely 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.

βœ… Great call! Added some JSDoc @example lines. Let me know if that seems good!

Also very on board with adding this to @hashicorp/platform-util at some point in the near future. Added a task to our platform board: 🎟️ [platform-util] add isAbsoluteUrl, it is widely used and implementation has been pretty static

Copy link
Contributor

Choose a reason for hiding this comment

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

These examples look great! Very clear and super helpful. Thank you for adding πŸ’–

*/
if (typeof props.image !== 'undefined' && !isAbsoluteUrl(props.image)) {
/**
* TODO: should we consider alternatives to throwing an error here?
Copy link
Contributor Author

@zchsh zchsh Jun 21, 2022

Choose a reason for hiding this comment

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

@BRKalow Forget to mention in review - wanted to get your take on the error approach here. I think the IS_DEV approach could make sense, but would love a second opinion on that (and also a lil curious about whether further tracking, eg in DataDog if available, sounds like something we might want to do... console.error seems like something that might not have significant visibility for us?).

Copy link
Contributor

Choose a reason for hiding this comment

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

If our goal is to fix all existing now-incorrect values for props.image, it seems reasonable to me to always throw an error regardless of the NODE_ENV. No production builds will be immediately broken unless the version for this package is upgraded after release right? We'll just start seeing failed Vercel builds in PRs?

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 think we'd definitely want to always throw an error if we did 100% static builds, but might not be as safe anymore since we don't static render all pages... for ISR pages, I think throwing an error could cause a client-side error, or possibly a 500 for the initial server-side render? Does that sound accurate?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, yeah. That makes sense. I wonder if we can test different cases to make sure? But that can definitely be done outside of this PR! We can always update this block in the future if needed. πŸ“Œ

Copy link
Contributor

@ashleemboyer ashleemboyer left a comment

Choose a reason for hiding this comment

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

This looks good to me! ✨

@zchsh zchsh changed the title feat(hashi-head): validate that props.image is absolute URL feat(hashi-head): validate props.image, add twitter:description Jun 22, 2022
@zchsh zchsh merged commit 270c679 into main Jun 22, 2022
@zchsh zchsh deleted the zs.meta-tags-for-social-share-cards branch June 22, 2022 21:53
@hashibot-web hashibot-web mentioned this pull request Jun 22, 2022
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.

None yet

2 participants