@graphcommerce/next-ui 4.2.1 → 4.2.2

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,24 @@
1
1
  # Change Log
2
2
 
3
+ ## 4.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1296](https://github.com/ho-nl/m2-pwa/pull/1296)
8
+ [`a9cff2ce6`](https://github.com/ho-nl/m2-pwa/commit/a9cff2ce63fce5b86e9fd6bf63c10c782326d50e)
9
+ Thanks [@paales](https://github.com/paales)! - Make sure the page is max height when no menuFab or
10
+ cartFab is present
11
+
12
+ * [#1296](https://github.com/ho-nl/m2-pwa/pull/1296)
13
+ [`8473123fa`](https://github.com/ho-nl/m2-pwa/commit/8473123fa7d3f3eb1d282d9b4205c803a88010ea)
14
+ Thanks [@paales](https://github.com/paales)! - implement handling for canonical URLs based on
15
+ NEXT_PUBLIC_SITE_URL
16
+
17
+ - [#1296](https://github.com/ho-nl/m2-pwa/pull/1296)
18
+ [`50e205c51`](https://github.com/ho-nl/m2-pwa/commit/50e205c51f4d0d67d41d22fd70e8ed9a0996489e)
19
+ Thanks [@paales](https://github.com/paales)! - make sure the scroll performance of galleries in
20
+ safari is better
21
+
3
22
  ## 4.2.1
4
23
 
5
24
  ### Patch Changes
@@ -1,5 +1,12 @@
1
1
  import { usePageContext } from '@graphcommerce/framer-next-pages'
2
+ import {
3
+ resolveHref,
4
+ getDomainLocale,
5
+ addBasePath,
6
+ addLocale,
7
+ } from 'next/dist/shared/lib/router/router'
2
8
  import Head from 'next/head'
9
+ import { useRouter } from 'next/router'
3
10
 
4
11
  // https://developers.google.com/search/docs/advanced/robots/robots_meta_tag#directives
5
12
  export type MetaRobots =
@@ -15,19 +22,60 @@ export type MetaRobots =
15
22
  | `max-video-preview:${number}`
16
23
  type MetaRobotsAll = ['all' | 'none']
17
24
 
25
+ type Canonical = `http://${string}` | `https://${string}` | `/${string}` | string
26
+
18
27
  export type PageMetaProps = {
19
28
  title: string
20
- canonical?: `http://${string}` | `https://${string}` | string
29
+ canonical?: Canonical
21
30
  metaDescription?: string
22
31
  metaRobots?: MetaRobotsAll | MetaRobots[]
23
32
  }
24
33
 
34
+ export function useCanonical(incomming?: Canonical) {
35
+ const router = useRouter()
36
+ let canonical = incomming
37
+
38
+ if (!canonical) return canonical
39
+
40
+ if (canonical?.startsWith('/')) {
41
+ if (!process.env.NEXT_PUBLIC_SITE_URL) {
42
+ if (process.env.NODE_ENV !== 'production') {
43
+ throw Error('NEXT_PUBLIC_SITE_URL is not defined in .env')
44
+ }
45
+ }
46
+
47
+ let [href, as] = resolveHref(router, canonical, true)
48
+
49
+ const curLocale = router.locale
50
+
51
+ // Copied from here https://github.com/vercel/next.js/blob/canary/packages/next/client/link.tsx#L313-L327
52
+ const localeDomain =
53
+ router &&
54
+ router.isLocaleDomain &&
55
+ getDomainLocale(as, curLocale, router && router.locales, router && router.domainLocales)
56
+
57
+ href = localeDomain || addBasePath(addLocale(as, curLocale, router && router.defaultLocale))
58
+
59
+ canonical = `${process.env.NEXT_PUBLIC_SITE_URL}${href}`
60
+ }
61
+
62
+ if (canonical && !(canonical ?? 'http').startsWith('http')) {
63
+ if (process.env.NODE_ENV !== 'production') {
64
+ throw new Error(
65
+ `canonical must start with '/', 'http://' or 'https://', '${canonical}' given`,
66
+ )
67
+ }
68
+ canonical = undefined
69
+ }
70
+
71
+ return canonical
72
+ }
73
+
25
74
  export function PageMeta(props: PageMetaProps) {
26
75
  const { active } = usePageContext()
27
- const { title, canonical, metaDescription, metaRobots = ['all'] } = props
76
+ const { title, canonical: canonicalBare, metaDescription, metaRobots = ['all'] } = props
28
77
 
29
- if (!(canonical ?? 'http').startsWith('http'))
30
- throw new Error(`canonical must start with http:// or https://, '${canonical}' given`)
78
+ const canonical = useCanonical(canonicalBare)
31
79
 
32
80
  if (!active) return null
33
81
  return (
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/next-ui",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "4.2.1",
5
+ "version": "4.2.2",
6
6
  "author": "",
7
7
  "license": "MIT",
8
8
  "sideEffects": false,