@rpcbase/router 0.7.0 → 0.9.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.7.0",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "scripts": {
@@ -23,6 +23,8 @@
23
23
  }
24
24
  }
25
25
  },
26
- "dependencies": {},
26
+ "dependencies": {
27
+ "react-router": "7.6.0"
28
+ },
27
29
  "devDependencies": {}
28
30
  }
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "react-router"
2
2
  export * from "./loadRoute"
3
+ export * from "./useApplyMeta"
@@ -0,0 +1,53 @@
1
+ import { useEffect } from "react"
2
+ import { useLocation } from "react-router"
3
+
4
+ import { defaultTitle, defaultMeta, pagesMeta } from "@/static/meta"
5
+
6
+
7
+ export const useApplyMeta = () => {
8
+ const location = useLocation()
9
+
10
+ useEffect(() => {
11
+ let pageMeta = pagesMeta[location.pathname]
12
+
13
+ if (!pageMeta) {
14
+ pageMeta = {title: defaultTitle, meta: defaultMeta}
15
+ }
16
+
17
+ document.title = pageMeta.title
18
+
19
+ // Remove previous dynamically inserted tags
20
+ document.querySelectorAll("[data-react-meta]").forEach((tag) => tag.remove())
21
+
22
+ // Inject new tags
23
+ pageMeta.meta.forEach((meta) => {
24
+ const metaElement = document.createElement("meta")
25
+ metaElement.setAttribute("data-react-meta", "true")
26
+
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
+ }
32
+ })
33
+
34
+ document.head.appendChild(metaElement)
35
+ })
36
+
37
+ // Update canonical link
38
+ const canonicalUrl = `${window.location.origin}${location.pathname}`
39
+
40
+ const existingCanonical = document.querySelector("link[rel=\"canonical\"]")
41
+ if (existingCanonical) {
42
+ existingCanonical.remove()
43
+ }
44
+
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
+
51
+ }, [location.pathname])
52
+
53
+ }