Skip to content

Commit

Permalink
fix(api): Use collection name when title isn't available (#2625)
Browse files Browse the repository at this point in the history
## What's the purpose of this pull request?

For SEO purposes, use the `name` info that is returned by Catalog's
public API when the `title` is `null`. This will be mainly useful for
Collection pages, in which the `title` is always `null`.

## How it works?

Through the nullish coalescing operator (`??`), when `title` doesn't
exist, the `name` will be used.

## How to test it?

Check if the metatag for title is being built correctly on a Collection
page.
For example, for the `Just Arrived` collection on `storeframework`
account:

|Before|After|
|----|----|
|<img width="1434" alt="Screenshot 2025-01-24 at 15 46 26"
src="https://github.com/user-attachments/assets/a8b8029c-fad1-44fb-8f10-65745e772986"
/>|<img width="1433" alt="Screenshot 2025-01-24 at 15 48 19"
src="https://github.com/user-attachments/assets/571153e7-9a4c-4ff8-9651-1c1bd1acdd61"
/>|


### Starters Deploy Preview

https://storeframework-cm652ufll028lmgv665a6xv0g-mygqwn0js.b.vtex.app/
([PR](vtex-sites/starter.store#656))
  • Loading branch information
lariciamota authored Jan 27, 2025
1 parent 50345ef commit 236bb3c
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions packages/api/src/platforms/vtex/resolvers/collection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { isCollectionPageType } from '../loaders/collection'
import { slugify } from '../utils/slugify'
import type { Resolver } from '..'
import type { Brand } from '../clients/commerce/types/Brand'
import type { CategoryTree } from '../clients/commerce/types/CategoryTree'
import type { CollectionPageType } from '../clients/commerce/types/Portal'
import { isCollectionPageType } from '../loaders/collection'
import { slugify } from '../utils/slugify'

export type Root = Brand | (CategoryTree & { level: number }) | CollectionPageType
export type Root =
| Brand
| (CategoryTree & { level: number })
| CollectionPageType

const isBrand = (x: any): x is Brand | CollectionPageType =>
x.type === 'brand' ||
Expand All @@ -32,7 +35,7 @@ export const StoreCollection: Record<string, Resolver<Root>> = {
seo: (root) =>
isBrand(root) || isCollectionPageType(root)
? {
title: root.title,
title: root.title ?? root.name,
description: root.metaTagDescription,
}
: {
Expand All @@ -43,10 +46,10 @@ export const StoreCollection: Record<string, Resolver<Root>> = {
isBrand(root)
? 'Brand'
: isCollectionPageType(root)
? root.pageType
: root.level === 0
? 'Department'
: 'Category',
? root.pageType
: root.level === 0
? 'Department'
: 'Category',
meta: (root) => {
const slug = slugifyRoot(root)

Expand All @@ -55,15 +58,15 @@ export const StoreCollection: Record<string, Resolver<Root>> = {
selectedFacets: [{ key: 'brand', value: slug }],
}
: isCollection(root)
? {
selectedFacets: [{ key: 'productclusterids', value: root.id }],
}
: {
selectedFacets: slug.split('/').map((segment, index) => ({
key: `category-${index + 1}`,
value: segment,
})),
}
? {
selectedFacets: [{ key: 'productclusterids', value: root.id }],
}
: {
selectedFacets: slug.split('/').map((segment, index) => ({
key: `category-${index + 1}`,
value: segment,
})),
}
},
breadcrumbList: async (root, _, ctx) => {
const {
Expand Down

0 comments on commit 236bb3c

Please sign in to comment.