@uxf/router 10.0.0-beta.17 → 10.0.0-beta.18
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/README.md +6 -6
- package/package.json +1 -1
- package/router.d.ts +2 -2
- package/router.js +1 -1
- package/sitemapGenerator.d.ts +9 -3
- package/sitemapGenerator.js +17 -7
package/README.md
CHANGED
|
@@ -119,13 +119,13 @@ Create sitemap items
|
|
|
119
119
|
```tsx
|
|
120
120
|
// sitemap-items.ts in @app-routes
|
|
121
121
|
|
|
122
|
-
import {
|
|
122
|
+
import { createSitemapGenerator, routeToUrl } from "@app-routes";
|
|
123
123
|
|
|
124
|
-
export const sitemapItems =
|
|
125
|
-
.add("index", async () => ({ loc:
|
|
126
|
-
.add("blog/detail", async () => [
|
|
127
|
-
{ loc:
|
|
128
|
-
{ loc:
|
|
124
|
+
export const sitemapItems = createSitemapGenerator({baseUrl: 'http://localhost:3000', defaultPriority: 1})
|
|
125
|
+
.add("index", async (route) => ({ loc: routeToUrl(route) }))
|
|
126
|
+
.add("blog/detail", async (route) => [
|
|
127
|
+
{ loc: routeToUrl(route, {id: 1}), priority: 2 },
|
|
128
|
+
{ loc: routeToUrl(route, {id: 2}), priority: 2 },
|
|
129
129
|
])
|
|
130
130
|
.skip("admin/index")
|
|
131
131
|
.exhaustive();
|
package/package.json
CHANGED
package/router.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { LinkProps } from "next/link";
|
|
2
|
-
import { SitemapGeneratorType } from "./sitemapGenerator";
|
|
2
|
+
import { SitemapGeneratorOptions, SitemapGeneratorType } from "./sitemapGenerator";
|
|
3
3
|
import { QueryParams } from "./types";
|
|
4
4
|
export type FunctionParametersGenerator<RouteList> = {
|
|
5
5
|
[K in keyof RouteList]: RouteList[K] extends null ? [K] : [K, RouteList[K]];
|
|
@@ -9,7 +9,7 @@ type RouteToUrlFunction<RouteList> = (...args: FunctionParametersGenerator<Route
|
|
|
9
9
|
type Router<RouteList> = {
|
|
10
10
|
route: RouteFunction<RouteList>;
|
|
11
11
|
routeToUrl: RouteToUrlFunction<RouteList>;
|
|
12
|
-
|
|
12
|
+
createSitemapGenerator: (options?: SitemapGeneratorOptions) => SitemapGeneratorType<RouteList>;
|
|
13
13
|
routes: {
|
|
14
14
|
[key in keyof RouteList]: string;
|
|
15
15
|
};
|
package/router.js
CHANGED
|
@@ -70,7 +70,7 @@ function createRouter(routes) {
|
|
|
70
70
|
}
|
|
71
71
|
return activeRoute;
|
|
72
72
|
},
|
|
73
|
-
|
|
73
|
+
createSitemapGenerator: sitemapGenerator_1.createSitemapGenerator,
|
|
74
74
|
routes,
|
|
75
75
|
useQueryParams: () => (0, router_1.useRouter)().query,
|
|
76
76
|
};
|
package/sitemapGenerator.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
type ChangeFrequency = "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never";
|
|
1
2
|
export type SitemapItem = {
|
|
2
3
|
loc: string;
|
|
3
|
-
priority
|
|
4
|
+
priority?: number;
|
|
4
5
|
lastmod?: string | null;
|
|
5
|
-
changefreq?:
|
|
6
|
+
changefreq?: ChangeFrequency;
|
|
6
7
|
};
|
|
7
8
|
type RouteResolverResult = null | undefined | SitemapItem | Array<SitemapItem | null | undefined>;
|
|
8
9
|
type RouteResolver<Route> = (route: Route) => Promise<RouteResolverResult>;
|
|
@@ -17,5 +18,10 @@ export type SitemapGeneratorType<RouteList> = {
|
|
|
17
18
|
toXml: keyof RouteList extends never ? () => Promise<string> : MissingRoutesError<keyof RouteList>;
|
|
18
19
|
toJson: keyof RouteList extends never ? () => Promise<any> : MissingRoutesError<keyof RouteList>;
|
|
19
20
|
};
|
|
20
|
-
export
|
|
21
|
+
export type SitemapGeneratorOptions = {
|
|
22
|
+
baseUrl?: string;
|
|
23
|
+
defaultPriority?: number;
|
|
24
|
+
defaultChangeFreq?: ChangeFrequency;
|
|
25
|
+
};
|
|
26
|
+
export declare const createSitemapGenerator: <RouteList>(options?: SitemapGeneratorOptions) => SitemapGeneratorType<RouteList>;
|
|
21
27
|
export {};
|
package/sitemapGenerator.js
CHANGED
|
@@ -5,8 +5,9 @@ function filterNullish(val) {
|
|
|
5
5
|
return val.filter((item) => item !== null && item !== undefined);
|
|
6
6
|
}
|
|
7
7
|
class SitemapGenerator {
|
|
8
|
-
constructor() {
|
|
8
|
+
constructor(options) {
|
|
9
9
|
this.routeResolvers = {};
|
|
10
|
+
this.options = null;
|
|
10
11
|
this.exhaustive = () => this;
|
|
11
12
|
this.toXml = () => {
|
|
12
13
|
return this.getSitemapItems().then((sitemapItems) => {
|
|
@@ -14,12 +15,12 @@ class SitemapGenerator {
|
|
|
14
15
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
|
|
15
16
|
${sitemapItems
|
|
16
17
|
.map((item) => {
|
|
17
|
-
var _a;
|
|
18
|
+
var _a, _b;
|
|
18
19
|
return `<url>
|
|
19
|
-
<loc>${item.loc}</loc>
|
|
20
|
-
|
|
20
|
+
<loc>${(_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.baseUrl) !== null && _b !== void 0 ? _b : ""}${item.loc}</loc>
|
|
21
|
+
${item.priority ? `<priority>${item.priority}</priority>` : ""}
|
|
21
22
|
${item.lastmod ? `<lastmod>${item.lastmod}</lastmod>` : ""}
|
|
22
|
-
|
|
23
|
+
${item.changefreq ? `<changefreq>${item.changefreq}</changefreq>` : ""}
|
|
23
24
|
</url>`;
|
|
24
25
|
})
|
|
25
26
|
.join("")}
|
|
@@ -27,10 +28,11 @@ class SitemapGenerator {
|
|
|
27
28
|
});
|
|
28
29
|
};
|
|
29
30
|
this.toJson = () => this.getSitemapItems().then(JSON.stringify);
|
|
31
|
+
this.options = options !== null && options !== void 0 ? options : null;
|
|
30
32
|
}
|
|
31
33
|
async getSitemapItems() {
|
|
32
34
|
const routes = Object.keys(this.routeResolvers);
|
|
33
|
-
|
|
35
|
+
const allSitemapItems = await Promise.all(routes.map((route) => { var _a, _b; return (_b = (_a = this.routeResolvers)[route]) === null || _b === void 0 ? void 0 : _b.call(_a, route); })).then((resolverResults) => {
|
|
34
36
|
const sitemapItems = [];
|
|
35
37
|
resolverResults.forEach((result) => {
|
|
36
38
|
if (!result) {
|
|
@@ -44,6 +46,14 @@ class SitemapGenerator {
|
|
|
44
46
|
});
|
|
45
47
|
return sitemapItems;
|
|
46
48
|
});
|
|
49
|
+
return allSitemapItems.map((sitemapItem) => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
return ({
|
|
52
|
+
...sitemapItem,
|
|
53
|
+
changefreq: sitemapItem.changefreq !== undefined ? sitemapItem.changefreq : (_a = this.options) === null || _a === void 0 ? void 0 : _a.defaultChangeFreq,
|
|
54
|
+
priority: sitemapItem.priority !== undefined ? sitemapItem.priority : (_b = this.options) === null || _b === void 0 ? void 0 : _b.defaultPriority,
|
|
55
|
+
});
|
|
56
|
+
});
|
|
47
57
|
}
|
|
48
58
|
add(route, resolver) {
|
|
49
59
|
this.routeResolvers[route] = resolver;
|
|
@@ -53,5 +63,5 @@ class SitemapGenerator {
|
|
|
53
63
|
return this;
|
|
54
64
|
}
|
|
55
65
|
}
|
|
56
|
-
const createSitemapGenerator = () => new SitemapGenerator();
|
|
66
|
+
const createSitemapGenerator = (options) => new SitemapGenerator(options);
|
|
57
67
|
exports.createSitemapGenerator = createSitemapGenerator;
|