@graphcommerce/magento-store 5.1.0-canary.2 → 5.1.0-canary.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # Change Log
2
2
 
3
+ ## 5.1.0-canary.4
4
+
5
+ ### Minor Changes
6
+
7
+ - [#1755](https://github.com/graphcommerce-org/graphcommerce/pull/1755) [`472486194`](https://github.com/graphcommerce-org/graphcommerce/commit/472486194f4a085e8a4dc0251b862024fb62ec86) - Magento URL Rewrites support to GraphCommerce compatible URL's. ([@paales](https://github.com/paales))
8
+
9
+ - [#1755](https://github.com/graphcommerce-org/graphcommerce/pull/1755) [`472486194`](https://github.com/graphcommerce-org/graphcommerce/commit/472486194f4a085e8a4dc0251b862024fb62ec86) - Magento URL Rewrites redirect 301 and 302 support ([@paales](https://github.com/paales))
10
+
11
+ ### Patch Changes
12
+
13
+ - [#1755](https://github.com/graphcommerce-org/graphcommerce/pull/1755) [`dbe103963`](https://github.com/graphcommerce-org/graphcommerce/commit/dbe10396363313908858de5d681d4e38db94d132) - Redirect URL suffixes to their non suffix counterpart and vice versa ([@paales](https://github.com/paales))
14
+
15
+ ## 5.1.0-canary.3
16
+
17
+ ### Patch Changes
18
+
19
+ - [#1752](https://github.com/graphcommerce-org/graphcommerce/pull/1752) [`2a6a4d9ec`](https://github.com/graphcommerce-org/graphcommerce/commit/2a6a4d9ecfa1b58a66ba9b9d00016d6feda9aa95) - Updated dependencies to latest versions, except for nextjs; Solve tons of peer dependency issues.
20
+
21
+ - Updated the @mui/material package
22
+ - Removed dependencies on react-hook-form-mui and @playwright/test
23
+ - Upgraded dependencies including type-fest and graphql-mesh
24
+ - Solved peer dependency issues ([@paales](https://github.com/paales))
25
+
3
26
  ## 5.1.0-canary.2
4
27
 
5
28
  ## 5.1.0-canary.1
package/index.ts CHANGED
@@ -11,3 +11,4 @@ export * from './StoreConfig.gql'
11
11
  export * from './components/StoreSwitcherButton/StoreSwitcherButton'
12
12
  export * from './components/StoreSwitcherList/StoreSwitcherList'
13
13
  export * from './components/StoreSwitcherList/StoreSwitcherList.gql'
14
+ export * from './utils/redirectOrNotFound'
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/magento-store",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "5.1.0-canary.2",
5
+ "version": "5.1.0-canary.4",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,24 +12,22 @@
12
12
  }
13
13
  },
14
14
  "devDependencies": {
15
- "@graphcommerce/eslint-config-pwa": "^5.1.0-canary.2",
16
- "@graphcommerce/prettier-config-pwa": "^5.1.0-canary.2",
17
- "@graphcommerce/typescript-config-pwa": "^5.1.0-canary.2",
18
- "@playwright/test": "^1.21.1",
19
- "type-fest": "^2.12.2"
15
+ "@graphcommerce/eslint-config-pwa": "5.1.0-canary.4",
16
+ "@graphcommerce/prettier-config-pwa": "5.1.0-canary.4",
17
+ "@graphcommerce/typescript-config-pwa": "5.1.0-canary.4"
20
18
  },
21
19
  "dependencies": {
22
- "@graphcommerce/graphql": "5.1.0-canary.2",
23
- "@graphcommerce/graphql-mesh": "5.1.0-canary.2",
24
- "@graphcommerce/image": "5.1.0-canary.2",
25
- "@graphcommerce/next-ui": "5.1.0-canary.2"
20
+ "@graphcommerce/graphql": "5.1.0-canary.4",
21
+ "@graphcommerce/graphql-mesh": "5.1.0-canary.4",
22
+ "@graphcommerce/image": "5.1.0-canary.4",
23
+ "@graphcommerce/next-ui": "5.1.0-canary.4"
26
24
  },
27
25
  "peerDependencies": {
28
26
  "@lingui/react": "^3.13.2",
29
27
  "@lingui/core": "^3.13.2",
30
- "@mui/material": "5.5.3",
28
+ "@mui/material": "^5.10.16",
31
29
  "next": "^12.1.2",
32
- "react": "^18.0.0",
33
- "react-dom": "^18.0.0"
30
+ "react": "^18.2.0",
31
+ "react-dom": "^18.2.0"
34
32
  }
35
33
  }
@@ -0,0 +1,20 @@
1
+ query HandleRedirect($url: String!) {
2
+ route(url: $url) {
3
+ __typename
4
+ redirect_code
5
+ relative_url
6
+ type
7
+ ... on ProductInterface {
8
+ uid
9
+ url_key
10
+ }
11
+ ... on CategoryInterface {
12
+ uid
13
+ url_key
14
+ }
15
+ ... on CmsPage {
16
+ url_key
17
+ identifier
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,87 @@
1
+ import { ParsedUrlQuery } from 'querystring'
2
+ import { ApolloClient, flushMeasurePerf, NormalizedCacheObject } from '@graphcommerce/graphql'
3
+ import { nonNullable } from '@graphcommerce/next-ui'
4
+ import { Redirect } from 'next'
5
+ import { StoreConfigDocument } from '../StoreConfig.gql'
6
+ import { defaultLocale } from '../localeToStore'
7
+ import { HandleRedirectDocument } from './HandleRedirect.gql'
8
+
9
+ export type RedirectOr404Return = Promise<
10
+ | { redirect: Redirect; revalidate?: number | boolean }
11
+ | { notFound: true; revalidate?: number | boolean }
12
+ >
13
+
14
+ const notFound = () => {
15
+ flushMeasurePerf()
16
+ return { notFound: true, revalidate: 60 * 20 } as const
17
+ }
18
+
19
+ const redirect = (to: string, permanent: boolean, locale?: string) => {
20
+ const prefix = locale === defaultLocale() ? '' : `/${locale}`
21
+ const destination = `${prefix}${to}`
22
+
23
+ flushMeasurePerf()
24
+
25
+ if (process.env.NODE_ENV === 'development') {
26
+ // eslint-disable-next-line no-console
27
+ console.log(`${permanent ? 'Permanent' : 'Temporary'} redirect to ${to}`)
28
+ // eslint-disable-next-line no-param-reassign
29
+ permanent = false
30
+ }
31
+
32
+ return { redirect: { destination, permanent }, revalidate: 60 * 20 }
33
+ }
34
+
35
+ export async function redirectOrNotFound(
36
+ client: ApolloClient<NormalizedCacheObject>,
37
+ params?: ParsedUrlQuery,
38
+ locale?: string,
39
+ ): RedirectOr404Return {
40
+ try {
41
+ // Create a URL from the params provided.
42
+ const urlKey = Object.values(params ?? {})
43
+ .map((v) => (Array.isArray(v) ? v.join('/') : v))
44
+ .join('/')
45
+
46
+ // Get the configured suffixes from the store config
47
+ const { product_url_suffix: prodSuffix, category_url_suffix: catSuffix } =
48
+ (await client.query({ query: StoreConfigDocument })).data.storeConfig ?? {}
49
+
50
+ const candidates = new Set([urlKey])
51
+
52
+ // If the incomming URL contains a suffix, we check if the URL without the suffix exists
53
+ // if the incomming URL does not contain a suffix, we check if the URL with the suffix exists
54
+ const suffixes = [prodSuffix, catSuffix].filter(nonNullable)
55
+ suffixes.forEach((suffix) => {
56
+ candidates.add(
57
+ urlKey.endsWith(suffix) ? urlKey.slice(0, -suffix.length) : `${urlKey}${suffix}`,
58
+ )
59
+ })
60
+
61
+ const routePromises = [...candidates].map(async (url) => {
62
+ const result = await client.query({ query: HandleRedirectDocument, variables: { url } })
63
+ if (!result.data.route) throw Error('No data')
64
+ return result.data.route
65
+ })
66
+
67
+ const route = await Promise.any(routePromises)
68
+
69
+ // There is a URL, so we need to check if it can be found in the database.
70
+ const permanent = route?.redirect_code === 301
71
+
72
+ // Add special handling for the homepage.
73
+ if (route?.url_key && route.__typename.endsWith('Product')) {
74
+ if (process.env.NEXT_PUBLIC_SINGLE_PRODUCT_PAGE !== '1') {
75
+ console.warn('Redirects are only supported for NEXT_PUBLIC_SINGLE_PRODUCT_PAGE')
76
+ }
77
+ return redirect(`/p/${route.url_key}`, permanent, locale)
78
+ }
79
+
80
+ // The default URL for categories or CMS pages is handled by the pages/[...url].tsx file.
81
+ if (route?.url_key) return redirect(`/${route.url_key}`, permanent, locale)
82
+ } catch (e) {
83
+ // We're done
84
+ }
85
+
86
+ return notFound()
87
+ }