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