From a7bc36678a94b4d1a01aea2a29e70234e0ccbea3 Mon Sep 17 00:00:00 2001 From: Tiago Gimenes Date: Mon, 13 Jun 2022 14:29:18 -0300 Subject: [PATCH] use portal slugs --- @generated/graphql/index.ts | 5 ++--- @generated/graphql/persisted.json | 2 +- CHANGELOG.md | 1 + package.json | 2 +- src/pages/[...slug].tsx | 1 + src/pages/[slug]/p.tsx | 30 ++++++++++++++---------------- src/server/index.ts | 6 +++--- store.config.js | 4 ++-- yarn.lock | 8 ++++---- 9 files changed, 29 insertions(+), 30 deletions(-) diff --git a/@generated/graphql/index.ts b/@generated/graphql/index.ts index 1376419f1..5be3daf73 100644 --- a/@generated/graphql/index.ts +++ b/@generated/graphql/index.ts @@ -2799,18 +2799,17 @@ export type ProductPageQueryQuery = { } export type ServerProductPageQueryQueryVariables = Exact<{ - id: Scalars['String'] + slug: Scalars['String'] }> export type ServerProductPageQueryQuery = { product: { - slug: string sku: string gtin: string name: string description: string id: string - seo: { title: string; description: string } + seo: { title: string; description: string; canonical: string } brand: { name: string } breadcrumbList: { itemListElement: Array<{ item: string; name: string; position: number }> diff --git a/@generated/graphql/persisted.json b/@generated/graphql/persisted.json index 8e3d4199c..3c31c0410 100644 --- a/@generated/graphql/persisted.json +++ b/@generated/graphql/persisted.json @@ -3,7 +3,7 @@ "CollectionPageQuery": "query CollectionPageQuery {\n site {\n siteMetadata {\n titleTemplate\n title\n description\n }\n }\n}\n", "ServerCollectionPageQuery": "query ServerCollectionPageQuery($slug: String!) {\n collection(slug: $slug) {\n seo {\n title\n description\n }\n breadcrumbList {\n itemListElement {\n item\n name\n position\n }\n }\n meta {\n selectedFacets {\n key\n value\n }\n }\n }\n}\n", "ProductPageQuery": "query ProductPageQuery {\n site {\n siteMetadata {\n title\n description\n titleTemplate\n siteUrl\n }\n }\n}\n", - "ServerProductPageQuery": "query ServerProductPageQuery($id: String!) {\n product(locator: [{key: \"id\", value: $id}]) {\n id: productID\n slug\n seo {\n title\n description\n }\n brand {\n name\n }\n sku\n gtin\n name\n description\n breadcrumbList {\n itemListElement {\n item\n name\n position\n }\n }\n image {\n url\n alternateName\n }\n offers {\n lowPrice\n highPrice\n priceCurrency\n offers {\n availability\n price\n priceValidUntil\n priceCurrency\n itemCondition\n seller {\n identifier\n }\n listPrice\n }\n }\n isVariantOf {\n productGroupID\n name\n }\n additionalProperty {\n propertyID\n name\n value\n valueReference\n }\n }\n}\n", + "ServerProductPageQuery": "query ServerProductPageQuery($slug: String!) {\n product(locator: [{key: \"slug\", value: $slug}]) {\n id: productID\n seo {\n title\n description\n canonical\n }\n brand {\n name\n }\n sku\n gtin\n name\n description\n breadcrumbList {\n itemListElement {\n item\n name\n position\n }\n }\n image {\n url\n alternateName\n }\n offers {\n lowPrice\n highPrice\n priceCurrency\n offers {\n availability\n price\n priceValidUntil\n priceCurrency\n itemCondition\n seller {\n identifier\n }\n listPrice\n }\n }\n isVariantOf {\n productGroupID\n name\n }\n additionalProperty {\n propertyID\n name\n value\n valueReference\n }\n }\n}\n", "HomePageQuery": "query HomePageQuery {\n site {\n siteMetadata {\n title\n description\n titleTemplate\n }\n }\n}\n", "SearchPageQuery": "query SearchPageQuery {\n site {\n siteMetadata {\n titleTemplate\n title\n description\n }\n }\n}\n", "ValidateCartMutation": "mutation ValidateCartMutation($cart: IStoreCart!) {\n validateCart(cart: $cart) {\n order {\n orderNumber\n acceptedOffer {\n seller {\n identifier\n }\n quantity\n price\n listPrice\n itemOffered {\n sku\n name\n image {\n url\n alternateName\n }\n brand {\n name\n }\n isVariantOf {\n productGroupID\n name\n }\n gtin\n additionalProperty {\n propertyID\n name\n value\n valueReference\n }\n }\n }\n }\n messages {\n text\n status\n }\n }\n}\n", diff --git a/CHANGELOG.md b/CHANGELOG.md index a77d3d781..cf2ff8a57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- 301 redirects when visiting old VTEX product routes ([#87](https://github.com/vtex-sites/gatsby.store/pull/87)) ### Changed diff --git a/package.json b/package.json index 0f3d170d8..d63b39070 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "@envelop/graphql-jit": "^1.1.1", "@envelop/parser-cache": "^2.2.0", "@envelop/validation-cache": "^2.2.0", - "@faststore/api": "^1.9.6", + "@faststore/api": "^1.9.7", "@faststore/sdk": "^1.9.4", "@faststore/ui": "^1.9.4", "@vtex/graphql-utils": "^1.8.42", diff --git a/src/pages/[...slug].tsx b/src/pages/[...slug].tsx index f0906df7e..170fa875a 100644 --- a/src/pages/[...slug].tsx +++ b/src/pages/[...slug].tsx @@ -196,6 +196,7 @@ export const getServerData = async ({ from: encodeURIComponent(`/${slug}`), }) + if (notFound) { return { status: 301, props: null, diff --git a/src/pages/[slug]/p.tsx b/src/pages/[slug]/p.tsx index 27a2f699f..bbc1c1e56 100644 --- a/src/pages/[slug]/p.tsx +++ b/src/pages/[slug]/p.tsx @@ -17,6 +17,8 @@ import type { } from '@generated/graphql' import { ITEMS_PER_SECTION } from 'src/constants' +import storeConfig from '../../../store.config' + import 'src/styles/pages/pdp.scss' export type Props = PageProps< @@ -31,8 +33,6 @@ function Page(props: Props) { const { data: { site }, serverData, - location: { host }, - slug, } = props // No data was found @@ -40,13 +40,14 @@ function Page(props: Props) { return null } - const { product } = serverData - const title = product?.seo.title ?? site?.siteMetadata?.title ?? '' - const description = - product?.seo.description ?? site?.siteMetadata?.description ?? '' + const { + product, + product: { seo }, + } = serverData - const canonical = - host !== undefined ? `https://${host}/${slug}/p` : `/${slug}/p` + const title = seo.title || site?.siteMetadata?.title || '' + const description = seo.description || site?.siteMetadata?.description || '' + const canonical = `https://${storeConfig.storeUrl}${seo.canonical}` return ( <> @@ -58,7 +59,7 @@ function Page(props: Props) { language={locale} openGraph={{ type: 'og:product', - url: `${site?.siteMetadata?.siteUrl}${slug}`, + url: canonical, title, description, images: product.image.map((img) => ({ @@ -131,21 +132,20 @@ export const querySSG = graphql` ` export const querySSR = gql` - query ServerProductPageQuery($id: String!) { - product(locator: [{ key: "id", value: $id }]) { + query ServerProductPageQuery($slug: String!) { + product(locator: [{ key: "slug", value: $slug }]) { id: productID - slug seo { title description + canonical } brand { name } - slug sku gtin name @@ -191,13 +191,11 @@ export const getServerData = async ({ params: Record }) => { const ONE_YEAR_CACHE = `s-maxage=31536000, stale-while-revalidate` - const id = slug.split('-').pop() - const { isNotFoundError } = await import('@faststore/api') const { execute } = await import('src/server/index') const { data, errors = [] } = await execute({ operationName: querySSR, - variables: { id }, + variables: { slug }, }) const notFound = errors.find(isNotFoundError) diff --git a/src/server/index.ts b/src/server/index.ts index d52bfc054..1676fdcb8 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1,17 +1,17 @@ /* eslint-disable react-hooks/rules-of-hooks */ import { envelop, + useAsyncSchema, useExtendContext, useMaskedErrors, - useAsyncSchema, } from '@envelop/core' -import type { FormatErrorHandler } from '@envelop/core' import { useGraphQlJit } from '@envelop/graphql-jit' import { useParserCache } from '@envelop/parser-cache' import { useValidationCache } from '@envelop/validation-cache' import { getContextFactory, getSchema, isFastStoreError } from '@faststore/api' -import type { Options as APIOptions } from '@faststore/api' import { GraphQLError } from 'graphql' +import type { FormatErrorHandler } from '@envelop/core' +import type { Options as APIOptions } from '@faststore/api' import persisted from '../../@generated/graphql/persisted.json' import storeConfig from '../../store.config' diff --git a/store.config.js b/store.config.js index 1b28ed955..c99b22331 100644 --- a/store.config.js +++ b/store.config.js @@ -25,7 +25,7 @@ module.exports = { server: process.env.BASE_SITE_URL || 'http://localhost:9000', pages: { home: '/', - pdp: '/apple-magic-mouse-99988212/p', + pdp: '/apple-magic-mouse/p', collection: '/office', }, }, @@ -34,7 +34,7 @@ module.exports = { cypress: { pages: { home: '/', - pdp: '/apple-magic-mouse-99988212/p', + pdp: '/apple-magic-mouse/p', collection: '/office', collection_filtered: '/office/?category-1=office&marca=acer&facets=category-1%2Cmarca', diff --git a/yarn.lock b/yarn.lock index 04be536eb..6c5e0e803 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1974,10 +1974,10 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" -"@faststore/api@^1.9.6": - version "1.9.6" - resolved "https://registry.yarnpkg.com/@faststore/api/-/api-1.9.6.tgz#c94c32fc2fb2098d6016914475b508e3f9afcf42" - integrity sha512-J3zi7g+szhQBLU9jgYgFDwP4oQFJB+4BpFx/Qvtn6voRsScRoP1Sz9Yk/1lZ2t1T5IZFTn4Qw/oudK2ii5+hMA== +"@faststore/api@^1.9.7": + version "1.9.7" + resolved "https://registry.yarnpkg.com/@faststore/api/-/api-1.9.7.tgz#00c2ecad235a273860035c87e4be3b9c3dea4115" + integrity sha512-khpdJ7J4W2P9SZdF9cesfceEjrK0E0Z6xLd6hZmZEJXba2vhuvNHP1zgUYpFVkEawT1IBN7y0akLp7EygqPp5w== dependencies: "@graphql-tools/schema" "^8.2.0" "@rollup/plugin-graphql" "^1.0.0"