@blocklet/sdk 1.16.15-beta-a635f48d → 1.16.15-beta-e4ad74af
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.
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
type PageData = {
|
|
3
|
+
title?: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
ogImage?: string;
|
|
6
|
+
};
|
|
2
7
|
type FallbackOptions = {
|
|
3
8
|
cacheControl?: boolean | undefined;
|
|
4
9
|
maxAge?: string | number | undefined;
|
|
5
10
|
root?: string | undefined;
|
|
11
|
+
getPageData?: (req: Request) => Promise<PageData>;
|
|
6
12
|
};
|
|
7
|
-
declare const fallback: (file: string, options?: FallbackOptions) => (req: Request, res: Response, next: NextFunction) => void
|
|
13
|
+
declare const fallback: (file: string, options?: FallbackOptions) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
8
14
|
export = fallback;
|
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const path_1 = require("path");
|
|
7
7
|
const url_join_1 = __importDefault(require("url-join"));
|
|
8
|
+
const escape_1 = __importDefault(require("lodash/escape"));
|
|
8
9
|
const config_1 = require("../config");
|
|
9
10
|
const fallback = (file, options = {}) => {
|
|
10
11
|
const filePath = options.root ? (0, path_1.join)(options.root, file) : file;
|
|
@@ -12,36 +13,50 @@ const fallback = (file, options = {}) => {
|
|
|
12
13
|
throw new Error(`Fallback file not found at: ${filePath}`);
|
|
13
14
|
}
|
|
14
15
|
let source = (0, fs_1.readFileSync)(filePath).toString();
|
|
15
|
-
return (req, res, next) => {
|
|
16
|
+
return async (req, res, next) => {
|
|
16
17
|
if ((req.method === 'GET' || req.method === 'HEAD') && req.accepts('html')) {
|
|
17
18
|
res.type('html');
|
|
18
19
|
const pageGroup = req.headers['x-page-group'] || '';
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
const pageData = typeof options.getPageData === 'function' ? await options.getPageData(req) : {};
|
|
21
|
+
pageData.title = (0, escape_1.default)(pageData.title || config_1.env.appName);
|
|
22
|
+
pageData.description = (0, escape_1.default)(pageData.description || config_1.env.appDescription);
|
|
23
|
+
pageData.ogImage = (0, escape_1.default)(pageData.ogImage || (0, url_join_1.default)(config_1.env.appUrl || '/', '/.well-known/service/blocklet/og.png'));
|
|
24
|
+
// inject seo title
|
|
25
|
+
if (pageData.title) {
|
|
26
|
+
if (source.indexOf('<title>') === -1) {
|
|
27
|
+
source = source.replace('</head>', `<title>${pageData.title}</title></head>`);
|
|
24
28
|
}
|
|
25
29
|
else {
|
|
26
|
-
source = source.replace(
|
|
30
|
+
source = source.replace(/<title>(.+)<\/title>/, `<title>${pageData.title}</title>`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// inject seo description
|
|
34
|
+
if (pageData.description) {
|
|
35
|
+
if (source.indexOf('<meta name="description"') === -1) {
|
|
36
|
+
source = source.replace('</head>', `<meta name="description" content="${pageData.description}" data-react-helmet="true" /></head>`);
|
|
27
37
|
}
|
|
28
38
|
}
|
|
39
|
+
// inject open graph
|
|
40
|
+
if (source.indexOf('meta property="og:image"') === -1) {
|
|
41
|
+
const og = `<meta property="og:title" content="${pageData.title}" data-react-helmet="true" />
|
|
42
|
+
<meta property="og:description" content="${pageData.description}" data-react-helmet="true" />
|
|
43
|
+
<meta property="og:type" content="website" data-react-helmet="true" />
|
|
44
|
+
<meta property="og:url" content="${config_1.env.appUrl}" data-react-helmet="true" />
|
|
45
|
+
<meta property="og:image" content="${pageData.ogImage}" data-react-helmet="true" />`;
|
|
46
|
+
source = source.replace('</head>', `${og}</head>`);
|
|
47
|
+
}
|
|
29
48
|
// inline blocklet.js
|
|
49
|
+
const blockletJs = (0, config_1.getBlockletJs)(pageGroup);
|
|
30
50
|
if (blockletJs) {
|
|
31
51
|
source = source
|
|
32
52
|
.replace('<script src="__blocklet__.js"></script>', `<script>${blockletJs}</script>`)
|
|
33
53
|
.replace('<script src="__meta__.js"></script>', `<script>${blockletJs}</script>`);
|
|
34
54
|
}
|
|
35
|
-
// inject og meta
|
|
36
|
-
if (source.indexOf('meta property="og:image"') === -1 && config_1.env.appUrl) {
|
|
37
|
-
const og = `<meta property="og:title" content="${config_1.env.appName}" />
|
|
38
|
-
<meta property="og:description" content="${config_1.env.appDescription}" />
|
|
39
|
-
<meta property="og:type" content="website" />
|
|
40
|
-
<meta property="og:url" content="${config_1.env.appUrl}" />
|
|
41
|
-
<meta property="og:image" content="${(0, url_join_1.default)(config_1.env.appUrl, '/.well-known/service/blocklet/og.png')}" />`;
|
|
42
|
-
source = source.replace('</head>', `${og}</head>`);
|
|
43
|
-
}
|
|
44
55
|
res.send(source);
|
|
56
|
+
// istanbul-ignore-next
|
|
57
|
+
if (process.env.NODE_ENV === 'test') {
|
|
58
|
+
next(source);
|
|
59
|
+
}
|
|
45
60
|
}
|
|
46
61
|
else {
|
|
47
62
|
next();
|
|
@@ -29,7 +29,12 @@ declare const _default: {
|
|
|
29
29
|
cacheControl?: boolean;
|
|
30
30
|
maxAge?: string | number;
|
|
31
31
|
root?: string;
|
|
32
|
-
|
|
32
|
+
getPageData?: (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>) => Promise<{
|
|
33
|
+
title?: string;
|
|
34
|
+
description?: string;
|
|
35
|
+
ogImage?: string;
|
|
36
|
+
}>;
|
|
37
|
+
}) => (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: import("express").Response<any, Record<string, any>>, next: import("express").NextFunction) => Promise<void>;
|
|
33
38
|
sitemap: (generatorFn: (fn: (item: {
|
|
34
39
|
url: string;
|
|
35
40
|
img?: {
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.15-beta-
|
|
6
|
+
"version": "1.16.15-beta-e4ad74af",
|
|
7
7
|
"description": "graphql client to read/write data on abt node",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"typings": "lib/index.d.ts",
|
|
@@ -26,15 +26,15 @@
|
|
|
26
26
|
"author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
|
|
27
27
|
"license": "Apache-2.0",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@abtnode/client": "1.16.
|
|
30
|
-
"@abtnode/constant": "1.16.
|
|
29
|
+
"@abtnode/client": "1.16.14",
|
|
30
|
+
"@abtnode/constant": "1.16.14",
|
|
31
31
|
"@arcblock/did": "1.18.89",
|
|
32
32
|
"@arcblock/did-auth": "1.18.89",
|
|
33
33
|
"@arcblock/jwt": "1.18.89",
|
|
34
34
|
"@arcblock/ws": "1.18.89",
|
|
35
|
-
"@blocklet/constant": "1.16.
|
|
36
|
-
"@blocklet/env": "1.16.
|
|
37
|
-
"@blocklet/meta": "1.16.
|
|
35
|
+
"@blocklet/constant": "1.16.14",
|
|
36
|
+
"@blocklet/env": "1.16.14",
|
|
37
|
+
"@blocklet/meta": "1.16.14",
|
|
38
38
|
"@did-connect/authenticator": "^2.2.0",
|
|
39
39
|
"@did-connect/handler": "^2.2.0",
|
|
40
40
|
"@nedb/core": "^2.1.5",
|
|
@@ -73,6 +73,5 @@
|
|
|
73
73
|
"ts-jest": "^27.1.5",
|
|
74
74
|
"ts-node": "^10.9.1",
|
|
75
75
|
"typescript": "^5.0.4"
|
|
76
|
-
}
|
|
77
|
-
"gitHead": "a42c00d339896a089694a6275cee988422ecef69"
|
|
76
|
+
}
|
|
78
77
|
}
|