-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate the Seo component and add
getSeoMeta
for migration (#1875)
* Deprecate the Seo component and add `getSeoMeta` for migration
- Loading branch information
Showing
13 changed files
with
1,794 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
'@shopify/hydrogen': patch | ||
--- | ||
|
||
Deprecate the `<Seo />` component in favor of directly using Remix meta route exports. Add the `getSeoMeta` to make migration easier: | ||
|
||
Migration steps: | ||
|
||
**1. Remove the `<Seo />` component from `root.jsx`:** | ||
|
||
```diff | ||
export default function App() { | ||
const nonce = useNonce(); | ||
const data = useLoaderData<typeof loader>(); | ||
|
||
return ( | ||
<html lang="en"> | ||
<head> | ||
<meta charSet="utf-8" /> | ||
<meta name="viewport" content="width=device-width,initial-scale=1" /> | ||
- <Seo /> | ||
<Meta /> | ||
<Links /> | ||
</head> | ||
<body> | ||
<Layout {...data}> | ||
<Outlet /> | ||
</Layout> | ||
<ScrollRestoration nonce={nonce} /> | ||
<Scripts nonce={nonce} /> | ||
<LiveReload nonce={nonce} /> | ||
</body> | ||
</html> | ||
); | ||
} | ||
|
||
``` | ||
|
||
**2. Add a Remix meta export to each route that returns an `seo` property from a `loader` or `handle`:** | ||
|
||
```diff | ||
+import {getSeoMeta} from '@shopify/hydrogen'; | ||
|
||
export async function loader({context}) { | ||
const {shop} = await context.storefront.query(` | ||
query layout { | ||
shop { | ||
name | ||
description | ||
} | ||
} | ||
`); | ||
|
||
return { | ||
seo: { | ||
title: shop.title, | ||
description: shop.description, | ||
}, | ||
}; | ||
} | ||
|
||
+export const meta = ({data}) => { | ||
+ return getSeoMeta(data.seo); | ||
+}; | ||
``` | ||
|
||
**3. Merge root route meta data** | ||
|
||
If your root route loader also returns an `seo` property, make sure to merge that data: | ||
|
||
```ts | ||
export const meta = ({data, matches}) => { | ||
return getSeoMeta( | ||
matches[0].data.seo, | ||
// the current route seo data overrides the root route data | ||
data.seo, | ||
); | ||
}; | ||
``` | ||
|
||
Or more simply: | ||
|
||
```ts | ||
export const meta = ({data, matches}) => { | ||
return getSeoMeta(...matches.map((match) => match.data.seo)); | ||
}; | ||
``` | ||
|
||
**4. Override meta** | ||
|
||
Sometimes `getSeoMeta` might produce a property in a way you'd like to change. Map over the resulting array to change it. For example, Hydrogen removes query parameters from canonical URLs, add them back: | ||
|
||
```ts | ||
export const meta = ({data, location}) => { | ||
return getSeoMeta(data.seo).map((meta) => { | ||
if (meta.rel === 'canonical') { | ||
return { | ||
...meta, | ||
href: meta.href + location.search, | ||
}; | ||
} | ||
|
||
return meta; | ||
}); | ||
}; | ||
``` |
429 changes: 413 additions & 16 deletions
429
packages/hydrogen/docs/generated/generated_docs_data.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {ReferenceEntityTemplateSchema} from '@shopify/generate-docs'; | ||
|
||
const data: ReferenceEntityTemplateSchema = { | ||
name: 'getSeoMeta', | ||
category: 'utilities', | ||
isVisualComponent: false, | ||
related: [], | ||
description: `Generate a [Remix meta array](https://remix.run/docs/en/main/route/meta) from one or more SEO configuration objects. Pass SEO configuration for the parent route(s) and the current route to preserve meta data for all active routes. Similar to [\`Object.assign()\`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign), each property is overwritten based on the object order. The exception is \`jsonLd\`, which is preserved so that each route has it's own independent jsonLd meta data. Learn more about [how SEO works in Hydrogen](https://shopify.dev/docs/custom-storefronts/hydrogen/seo).`, | ||
type: 'utility', | ||
defaultExample: { | ||
description: 'I am the default example', | ||
codeblock: { | ||
tabs: [ | ||
{ | ||
title: 'JavaScript', | ||
code: './getSeoMeta.example.jsx', | ||
language: 'js', | ||
}, | ||
{ | ||
title: 'TypeScript', | ||
code: './getSeoMeta.example.tsx', | ||
language: 'ts', | ||
}, | ||
], | ||
title: 'Example code', | ||
}, | ||
}, | ||
definitions: [ | ||
{ | ||
title: 'getSeoMeta', | ||
type: 'GetSeoMetaTypeForDocs', | ||
description: '', | ||
}, | ||
], | ||
}; | ||
|
||
export default data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {getSeoMeta} from '@shopify/hydrogen'; | ||
|
||
export async function loader({context}) { | ||
const {shop} = await context.storefront.query(` | ||
query layout { | ||
shop { | ||
name | ||
description | ||
} | ||
} | ||
`); | ||
|
||
return { | ||
seo: { | ||
title: shop.title, | ||
description: shop.description, | ||
}, | ||
}; | ||
} | ||
|
||
export const meta = ({data, matches}) => { | ||
// Pass one or more arguments, preserving properties from parent routes | ||
return getSeoMeta(matches[0].data.seo, data.seo); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {MetaFunction} from '@remix-run/react'; | ||
import {LoaderFunctionArgs} from '@remix-run/server-runtime'; | ||
import {getSeoMeta} from '@shopify/hydrogen'; | ||
|
||
export async function loader({context}: LoaderFunctionArgs) { | ||
const {shop} = await context.storefront.query(` | ||
query layout { | ||
shop { | ||
name | ||
description | ||
} | ||
} | ||
`); | ||
|
||
return { | ||
seo: { | ||
title: shop.title, | ||
description: shop.description, | ||
}, | ||
}; | ||
} | ||
|
||
export const meta: MetaFunction<typeof loader> = ({data, matches}) => { | ||
// Pass one or more arguments, preserving properties from parent routes | ||
return getSeoMeta((matches as any).data.seo, data!.seo); | ||
}; |
Oops, something went wrong.