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

Add alternate integration utility for using SEO in remix #650

Closed
wants to merge 4 commits into from

Conversation

cartogram
Copy link
Contributor

@cartogram cartogram commented Mar 6, 2023

WHY are these changes introduced?

This PR adds a simple utility for using the Hydrogen storefront SEO generated tags directly in a meta function.

WHAT is this pull request doing?

Given that we introduced the pattern / ability of returning an seo key in a loader (see #591) I became interested in seeing how an alternate approach would look if we didn't even bother with the <Seo /> component rendered in the root and instead provided compatibility functions for use in the page module exports (links, meta, scripts, etc...). This is just a proof of concept of this different adaptor approach.

With this approach we could basically just do the following:

// $productHandle.tsx

export async function loader({params, request, context}: LoaderArgs) {
  const {productHandle} = params;
  
  const {product} = await context.storefront.query<{
    product: ProductType & {selectedVariant?: ProductVariant};
  }>(PRODUCT_QUERY, {
    variables: {
      handle: productHandle,
    },
  });

  const seo = {
    title: product?.seo?.title ?? product?.title,
    description: product?.seo?.description ?? product?.description,
  }

  return defer({seo});
}

export const meta = (data) => tagsToMetaArray(data.product.seo, {tag: 'meta'});
export const links = (data) => tagsToMetaArray(data.product.seo, {tag: 'link'});
export const scripts = (data) => tagsToMetaArray(data.product.seo, {tag: 'script'});

The tagsToMetaArray name is wrong, and we'd probably provide one more layer of wrapping here (ie: generateMetaFromSeo, generateLinksFromSeo).

This approach is more aligned with how remix works and uses more of their built-ins which is the main benefit of going this route. I anticipate this leading to less bugs, confusion and ties us more directly to their design choices. An example of what I mean here that I think is worth calling out is the lack of route inheritance and no global values. I think these would again be best solved with light util functions as the remix docs describe.

Another benefit of this approach is that the entire integration lives in a single route file.

HOW to test your changes?

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
  • I've added or updated the documentation

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

1 participant