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

Generate SEO tags refactor for better types #616

Merged
merged 3 commits into from
Mar 20, 2023
Merged

Conversation

cartogram
Copy link
Contributor

WHY are these changes introduced?

This PR revisits some typescript improvements I have had on the back-burner.

WHAT is this pull request doing?

I've been struggling to achieve the proper type inference when iterating over the object keys in the SEO config. I experimented with Array methods and using the is operator, but the code was harder to follow. By moving the iteration outside of the nested array, it both simplifies things and removes a handful of casts.

There is still some more work to do here, but this was a first step that we can merge without issue.

HOW to test your changes?

Everything should still work, specifically SEO information still displays when running the demo-store.

All existing tests should still pass.

Post-merge steps

Checklist

  • I've read the Contributing Guidelines
  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've added a changeset if this PR contains user-facing or noteworthy changes
  • I've added tests to cover my changes

@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2023

We detected some changes in packages/*/package.json or packages/*/src, and there are no updates in the .changeset.
If the changes are user-facing and should cause a version bump, run npm run changeset add to track your changes and include them in the next release CHANGELOG.
If you are making simple updates to examples or documentation, you do not need to add a changeset.

@cartogram cartogram requested a review from a team March 3, 2023 16:50
@cartogram cartogram mentioned this pull request Mar 3, 2023
14 tasks
packages/hydrogen/src/seo/generate-seo-tags.ts Outdated Show resolved Hide resolved
entries.filter((value) => !!value),
);
break;
}
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 can add a type check here for never to make sure we've exhausted all options

Copy link
Contributor

@frandiox frandiox left a comment

Choose a reason for hiding this comment

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

👋 I've added a few comments below.

Question: would it be interesting to modify the schema to specify string patterns?

handler: Maybe<`@${string}`>;
url: Maybe<`http${string}`>;
// etc

I'm not sure since perhaps most of this data comes from the SFAPI as string, not inlined 🤔

}

return 'image/jpeg';
return tagResults.flat().sort((a, b) => a.key.localeCompare(b.key));
}

type MetaTagProps =
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand the following is not part of this PR but perhaps from the previous one. In any case, what's the reason of the many generateTag signature overloads? Does it improve the error vs a simplified type using generics?

export function generateTag<T extends TagKey>(
  tagName: T,
  input: ComponentPropsWithoutRef<T>,
  group?: string,
): CustomHeadTagObject {
 // ...
}

I see the current overloads show the following errors:

image

If we simplify the overloads, the errors look more like this:

image

Which I think is more consistent? 🤔

| 'BlogPosting'
| 'Thing';

function inferSchemaType(url: Maybe<string> | undefined): SchemaType {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function doesn't seem to be used anywhere nor exported.

Comment on lines 730 to 737
function inferMimeType(url: Maybe<string> | undefined) {
const ext = url && url.split('.').pop();

if (ext === 'svg') {
return 'image/svg+xml';
}

if (ext === 'png') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I barely use switch statements but this function seems like the perfect fit for it.

@cartogram cartogram merged commit cc74c30 into 2023-01 Mar 20, 2023
@cartogram cartogram deleted the seo-type-improvements branch March 20, 2023 18:03
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