@rpcbase/router 0.9.0 → 0.10.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/router",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "scripts": {
@@ -1,53 +1,71 @@
1
1
  import { useEffect } from "react"
2
2
  import { useLocation } from "react-router"
3
3
 
4
- import { defaultTitle, defaultMeta, pagesMeta } from "@/static/meta"
5
-
6
-
7
4
  export const useApplyMeta = () => {
8
5
  const location = useLocation()
9
6
 
10
7
  useEffect(() => {
11
- let pageMeta = pagesMeta[location.pathname]
8
+ const loadMeta = async () => {
9
+ let defaultTitle = ""
10
+ let defaultMeta = []
11
+ let pagesMeta = {}
12
12
 
13
- if (!pageMeta) {
14
- pageMeta = {title: defaultTitle, meta: defaultMeta}
15
- }
13
+ try {
14
+ const module = await import("@/static/meta")
15
+ defaultTitle = module.defaultTitle || defaultTitle
16
+ defaultMeta = module.defaultMeta || defaultMeta
17
+ pagesMeta = module.pagesMeta || pagesMeta
18
+ } catch (error) {
19
+ if (import.meta.env.MODE !== "production") {
20
+ console.warn(
21
+ "Failed to load meta data from '@/static/meta'.",
22
+ error
23
+ )
24
+ }
25
+ return
26
+ }
16
27
 
17
- document.title = pageMeta.title
28
+ let pageMeta = pagesMeta[location.pathname]
18
29
 
19
- // Remove previous dynamically inserted tags
20
- document.querySelectorAll("[data-react-meta]").forEach((tag) => tag.remove())
30
+ if (!pageMeta) {
31
+ pageMeta = { title: defaultTitle, meta: defaultMeta }
32
+ }
21
33
 
22
- // Inject new tags
23
- pageMeta.meta.forEach((meta) => {
24
- const metaElement = document.createElement("meta")
25
- metaElement.setAttribute("data-react-meta", "true")
34
+ document.title = pageMeta.title
26
35
 
27
- // Set all attributes from the meta object
28
- Object.entries(meta).forEach(([key, value]) => {
29
- if (value) {
30
- metaElement.setAttribute(key, value.toString())
31
- }
36
+ // Remove previous dynamically inserted tags
37
+ document.querySelectorAll("[data-react-meta]").forEach((tag) => tag.remove())
38
+
39
+ // Inject new tags
40
+ pageMeta.meta.forEach((meta) => {
41
+ const metaElement = document.createElement("meta")
42
+ metaElement.setAttribute("data-react-meta", "true")
43
+
44
+ // Set all attributes from the meta object
45
+ Object.entries(meta).forEach(([key, value]) => {
46
+ if (value) {
47
+ metaElement.setAttribute(key, value.toString())
48
+ }
49
+ })
50
+
51
+ document.head.appendChild(metaElement)
32
52
  })
33
53
 
34
- document.head.appendChild(metaElement)
35
- })
54
+ // Update canonical link
55
+ const canonicalUrl = `${window.location.origin}${location.pathname}`
36
56
 
37
- // Update canonical link
38
- const canonicalUrl = `${window.location.origin}${location.pathname}`
57
+ const existingCanonical = document.querySelector("link[rel=\"canonical\"]")
58
+ if (existingCanonical) {
59
+ existingCanonical.remove()
60
+ }
39
61
 
40
- const existingCanonical = document.querySelector("link[rel=\"canonical\"]")
41
- if (existingCanonical) {
42
- existingCanonical.remove()
62
+ const canonicalLink = document.createElement("link")
63
+ canonicalLink.setAttribute("rel", "canonical")
64
+ canonicalLink.setAttribute("href", canonicalUrl)
65
+ canonicalLink.setAttribute("data-react-meta", "true")
66
+ document.head.appendChild(canonicalLink)
43
67
  }
44
68
 
45
- const canonicalLink = document.createElement("link")
46
- canonicalLink.setAttribute("rel", "canonical")
47
- canonicalLink.setAttribute("href", canonicalUrl)
48
- canonicalLink.setAttribute("data-react-meta", "true")
49
- document.head.appendChild(canonicalLink)
50
-
69
+ loadMeta()
51
70
  }, [location.pathname])
52
-
53
71
  }