rasengan 1.0.0-beta.28 → 1.0.0-beta.29
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/lib/esm/index.js +1 -1
- package/lib/esm/server/functions/netlify/functions/api.js +134 -134
- package/lib/esm/server/functions/vercel/api/index.js +125 -131
- package/lib/esm/server/utils/handleRequest.js +1 -1
- package/lib/types/index.d.ts +1 -1
- package/lib/types/server/functions/netlify/functions/api.d.ts +1 -1
- package/lib/types/server/functions/vercel/api/index.d.ts +1 -1
- package/lib/types/server/utils/handleRequest.d.ts +1 -1
- package/package.json +1 -1
package/lib/esm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Imports
|
|
2
2
|
import createFetchRequest from "./server/utils/createFetchRequest.js";
|
|
3
|
-
import handleRequest from "./server/utils/handleRequest.js";
|
|
3
|
+
import { handleRequest } from "./server/utils/handleRequest.js";
|
|
4
4
|
// Exports
|
|
5
5
|
// export * from "./decorators/index.js";
|
|
6
6
|
export * from "./core/index.js";
|
|
@@ -34,142 +34,142 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
34
34
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
37
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
38
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
39
|
+
if (ar || !(i in from)) {
|
|
40
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
41
|
+
ar[i] = from[i];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
45
|
+
};
|
|
46
|
+
import fs from "node:fs/promises";
|
|
47
|
+
import fsSync from "node:fs";
|
|
48
|
+
import path, { join } from "node:path";
|
|
49
|
+
import { createStaticHandler, createStaticRouter, } from "react-router-dom/server.js";
|
|
47
50
|
// @ts-ignore
|
|
48
|
-
import {
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
//
|
|
52
|
-
//
|
|
53
|
-
// const host = req.headers.get("host")
|
|
54
|
-
// // Get app path
|
|
55
|
-
// // const appPath = join(__dirname, "..");
|
|
56
|
-
// const appPath = process.cwd();
|
|
57
|
-
// // ! Robots Fix
|
|
58
|
-
// if (url === "/robots.txt") {
|
|
59
|
-
// // Check if robots.txt exists using fs
|
|
60
|
-
// // If it does, return it
|
|
61
|
-
// try {
|
|
62
|
-
// await fs.access(path.resolve(join(appPath, "dist/client/robots.txt")));
|
|
63
|
-
// return new Response(path.resolve(join(appPath, "dist/client/robots.txt")))
|
|
64
|
-
// } catch (err: any) {
|
|
65
|
-
// return new Response(`
|
|
66
|
-
// user-agent: *
|
|
67
|
-
// disallow: /downloads/
|
|
68
|
-
// disallow: /private/
|
|
69
|
-
// allow: /
|
|
70
|
-
// user-agent: magicsearchbot
|
|
71
|
-
// disallow: /uploads/
|
|
72
|
-
// `)
|
|
73
|
-
// }
|
|
74
|
-
// }
|
|
75
|
-
// // ! Sitemap Fix
|
|
76
|
-
// if (url === "/sitemap.xml") {
|
|
77
|
-
// return new Response(path.resolve(join(appPath, "dist/client/sitemap.xml")))
|
|
78
|
-
// }
|
|
79
|
-
// // ! Manifest Fix
|
|
80
|
-
// if (url === "/manifest.json") {
|
|
81
|
-
// return new Response(path.resolve(join(appPath, "dist/client/manifest.json")))
|
|
82
|
-
// }
|
|
83
|
-
// // ! Handle assets
|
|
84
|
-
// if (url.includes("/assets")) {
|
|
85
|
-
// // get segments from /assets to the end
|
|
86
|
-
// const segments = url.split("/");
|
|
87
|
-
// const segmentsWithoutOrigin = [...segments];
|
|
88
|
-
// for (let segment of segments) {
|
|
89
|
-
// if (segment === "assets") {
|
|
90
|
-
// break;
|
|
91
|
-
// }
|
|
92
|
-
// segmentsWithoutOrigin.shift();
|
|
93
|
-
// }
|
|
94
|
-
// // replace assets by client/assets
|
|
95
|
-
// const filePath = join(appPath, "dist/client", segmentsWithoutOrigin.join("/"));
|
|
96
|
-
// const file = await fs.readFile(filePath, "utf-8");
|
|
97
|
-
// return new Response(file, {
|
|
98
|
-
// headers: {
|
|
99
|
-
// "Content-Type": url.endsWith(".js") ? "text/javascript" : "text/css",
|
|
100
|
-
// "Cache-Control": "max-age=31536000",
|
|
101
|
-
// },
|
|
102
|
-
// });
|
|
103
|
-
// }
|
|
104
|
-
// // Template html
|
|
105
|
-
// let templateHtml = "";
|
|
106
|
-
// // Always read fresh template in development
|
|
107
|
-
// const serverFilePath = join(appPath, "dist/server/entry-server.js");
|
|
108
|
-
// const bootstrapDirPath = join(appPath, "dist/client/assets")
|
|
109
|
-
// // Read the entry sever file
|
|
110
|
-
// let entry = await import(serverFilePath);
|
|
111
|
-
// // replace bootstrap script with compiled scripts
|
|
112
|
-
// let bootstrap =
|
|
113
|
-
// "/assets/" +
|
|
114
|
-
// fsSync
|
|
115
|
-
// .readdirSync(bootstrapDirPath)
|
|
116
|
-
// .filter((fn) => fn.includes("entry-client") && fn.endsWith(".js"))[0];
|
|
117
|
-
// // replace styles with compiled styles
|
|
118
|
-
// let styles =
|
|
119
|
-
// "/assets/" +
|
|
120
|
-
// fsSync
|
|
121
|
-
// .readdirSync(join(appPath, "dist/client/assets"))
|
|
122
|
-
// .filter((fn) => fn.includes("entry-client") && fn.endsWith(".css"))[0];
|
|
123
|
-
// // Extract render and staticRoutes from entry
|
|
124
|
-
// const { render, staticRoutes, loadTemplateHtml } = entry;
|
|
125
|
-
// // Create static handler
|
|
126
|
-
// let handler = createStaticHandler(staticRoutes);
|
|
127
|
-
// // Create fetch request for static routing
|
|
128
|
-
// // @ts-ignore
|
|
129
|
-
// let fetchRequest = createFetchRequest(req, host);
|
|
130
|
-
// let context = await handler.query(fetchRequest);
|
|
131
|
-
// // Handle redirects
|
|
132
|
-
// const status = (context as Response).status;
|
|
133
|
-
// if (status === 302) {
|
|
134
|
-
// const redirect = (context as Response).headers.get("Location");
|
|
135
|
-
// if (redirect) {
|
|
136
|
-
// return Response.redirect(redirect);
|
|
137
|
-
// }
|
|
138
|
-
// }
|
|
139
|
-
// // Helmet context
|
|
140
|
-
// const helmetContext = {} as { helmet: any };
|
|
141
|
-
// // Create static router
|
|
142
|
-
// let router = createStaticRouter(
|
|
143
|
-
// handler.dataRoutes,
|
|
144
|
-
// context as StaticHandlerContext
|
|
145
|
-
// );
|
|
146
|
-
// const rendered = await render(router, context, helmetContext);
|
|
147
|
-
// // Load template html
|
|
148
|
-
// if (!templateHtml) {
|
|
149
|
-
// templateHtml = loadTemplateHtml(helmetContext, bootstrap, styles);
|
|
150
|
-
// }
|
|
151
|
-
// // Replacing the app-html placeholder with the rendered html
|
|
152
|
-
// let html = templateHtml.replace(`rasengan-body-app`, rendered.html ?? "");
|
|
153
|
-
// // Send the rendered html page
|
|
154
|
-
// return new Response(html, {
|
|
155
|
-
// status: 200,
|
|
156
|
-
// headers: {
|
|
157
|
-
// "Content-Type": "text/html",
|
|
158
|
-
// "Cache-Control": "max-age=31536000",
|
|
159
|
-
// },
|
|
160
|
-
// });
|
|
161
|
-
// } catch (e: any) {
|
|
162
|
-
// console.log(e.stack);
|
|
163
|
-
// return new Response(e.stack, {
|
|
164
|
-
// status: 500,
|
|
165
|
-
// });
|
|
166
|
-
// }
|
|
167
|
-
// }
|
|
51
|
+
import { createFetchRequest } from "rasengan";
|
|
52
|
+
// // @ts-ignore
|
|
53
|
+
// import { handleRequest } from "rasengan";
|
|
54
|
+
// // @ts-ignore
|
|
55
|
+
// import type { Context } from "@netlify/functions"
|
|
168
56
|
export default (function (req, context) { return __awaiter(void 0, void 0, void 0, function () {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
57
|
+
var url, host, appPath, err_1, segments, segmentsWithoutOrigin, _i, segments_1, segment, filePath, file, file, templateHtml, serverFilePath, bootstrapDirPath, entry, bootstrap, styles, render, staticRoutes, loadTemplateHtml, handler, fetchRequest, context_1, status_1, redirect, helmetContext, router, rendered, html, e_1;
|
|
58
|
+
var _a;
|
|
59
|
+
return __generator(this, function (_b) {
|
|
60
|
+
switch (_b.label) {
|
|
61
|
+
case 0:
|
|
62
|
+
_b.trys.push([0, 12, , 13]);
|
|
63
|
+
url = req.url;
|
|
64
|
+
host = req.headers.get("host");
|
|
65
|
+
appPath = process.cwd();
|
|
66
|
+
if (!(url === "/robots.txt")) return [3 /*break*/, 4];
|
|
67
|
+
_b.label = 1;
|
|
68
|
+
case 1:
|
|
69
|
+
_b.trys.push([1, 3, , 4]);
|
|
70
|
+
return [4 /*yield*/, fs.access(path.resolve(join(appPath, "dist/client/robots.txt")))];
|
|
71
|
+
case 2:
|
|
72
|
+
_b.sent();
|
|
73
|
+
return [2 /*return*/, new Response(path.resolve(join(appPath, "dist/client/robots.txt")))];
|
|
74
|
+
case 3:
|
|
75
|
+
err_1 = _b.sent();
|
|
76
|
+
return [2 /*return*/, new Response("\n user-agent: *\n disallow: /downloads/\n disallow: /private/\n allow: /\n \n user-agent: magicsearchbot\n disallow: /uploads/\n ")];
|
|
77
|
+
case 4:
|
|
78
|
+
// ! Sitemap Fix
|
|
79
|
+
if (url === "/sitemap.xml") {
|
|
80
|
+
return [2 /*return*/, new Response(path.resolve(join(appPath, "dist/client/sitemap.xml")))];
|
|
81
|
+
}
|
|
82
|
+
// ! Manifest Fix
|
|
83
|
+
if (url === "/manifest.json") {
|
|
84
|
+
return [2 /*return*/, new Response(path.resolve(join(appPath, "dist/client/manifest.json")))];
|
|
85
|
+
}
|
|
86
|
+
if (!url.includes("/assets")) return [3 /*break*/, 6];
|
|
87
|
+
segments = url.split("/");
|
|
88
|
+
segmentsWithoutOrigin = __spreadArray([], segments, true);
|
|
89
|
+
for (_i = 0, segments_1 = segments; _i < segments_1.length; _i++) {
|
|
90
|
+
segment = segments_1[_i];
|
|
91
|
+
if (segment === "assets") {
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
segmentsWithoutOrigin.shift();
|
|
95
|
+
}
|
|
96
|
+
filePath = join(appPath, "dist/client", segmentsWithoutOrigin.join("/"));
|
|
97
|
+
return [4 /*yield*/, fs.readFile(filePath, "utf-8")];
|
|
98
|
+
case 5:
|
|
99
|
+
file = _b.sent();
|
|
100
|
+
return [2 /*return*/, new Response(file, {
|
|
101
|
+
headers: {
|
|
102
|
+
"Content-Type": url.endsWith(".js") ? "text/javascript" : "text/css",
|
|
103
|
+
"Cache-Control": "max-age=31536000",
|
|
104
|
+
},
|
|
105
|
+
})];
|
|
106
|
+
case 6:
|
|
107
|
+
if (!(url.endsWith(".js") || url.endsWith(".css"))) return [3 /*break*/, 8];
|
|
108
|
+
return [4 /*yield*/, fs.readFile(url, "utf-8")];
|
|
109
|
+
case 7:
|
|
110
|
+
file = _b.sent();
|
|
111
|
+
return [2 /*return*/, new Response(file, {
|
|
112
|
+
headers: {
|
|
113
|
+
"Content-Type": url.endsWith(".js") ? "text/javascript" : "text/css",
|
|
114
|
+
"Cache-Control": "max-age=31536000",
|
|
115
|
+
},
|
|
116
|
+
})];
|
|
117
|
+
case 8:
|
|
118
|
+
templateHtml = "";
|
|
119
|
+
serverFilePath = join(appPath, "dist/server/entry-server.js");
|
|
120
|
+
bootstrapDirPath = join(appPath, "dist/client/assets");
|
|
121
|
+
return [4 /*yield*/, import(serverFilePath)];
|
|
122
|
+
case 9:
|
|
123
|
+
entry = _b.sent();
|
|
124
|
+
bootstrap = "/assets/" +
|
|
125
|
+
fsSync
|
|
126
|
+
.readdirSync(bootstrapDirPath)
|
|
127
|
+
.filter(function (fn) { return fn.includes("entry-client") && fn.endsWith(".js"); })[0];
|
|
128
|
+
styles = "/assets/" +
|
|
129
|
+
fsSync
|
|
130
|
+
.readdirSync(join(appPath, "dist/client/assets"))
|
|
131
|
+
.filter(function (fn) { return fn.includes("entry-client") && fn.endsWith(".css"); })[0];
|
|
132
|
+
render = entry.render, staticRoutes = entry.staticRoutes, loadTemplateHtml = entry.loadTemplateHtml;
|
|
133
|
+
handler = createStaticHandler(staticRoutes);
|
|
134
|
+
fetchRequest = createFetchRequest(req, host);
|
|
135
|
+
return [4 /*yield*/, handler.query(fetchRequest)];
|
|
136
|
+
case 10:
|
|
137
|
+
context_1 = _b.sent();
|
|
138
|
+
status_1 = context_1.status;
|
|
139
|
+
if (status_1 === 302) {
|
|
140
|
+
redirect = context_1.headers.get("Location");
|
|
141
|
+
if (redirect) {
|
|
142
|
+
return [2 /*return*/, Response.redirect(redirect)];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
helmetContext = {};
|
|
146
|
+
router = createStaticRouter(handler.dataRoutes, context_1);
|
|
147
|
+
return [4 /*yield*/, render(router, context_1, helmetContext)];
|
|
148
|
+
case 11:
|
|
149
|
+
rendered = _b.sent();
|
|
150
|
+
// Load template html
|
|
151
|
+
if (!templateHtml) {
|
|
152
|
+
templateHtml = loadTemplateHtml(helmetContext, bootstrap, styles);
|
|
153
|
+
}
|
|
154
|
+
html = templateHtml.replace("rasengan-body-app", (_a = rendered.html) !== null && _a !== void 0 ? _a : "");
|
|
155
|
+
// Send the rendered html page
|
|
156
|
+
return [2 /*return*/, new Response(html, {
|
|
157
|
+
status: 200,
|
|
158
|
+
headers: {
|
|
159
|
+
"Content-Type": "text/html",
|
|
160
|
+
"Cache-Control": "max-age=31536000",
|
|
161
|
+
},
|
|
162
|
+
})];
|
|
163
|
+
case 12:
|
|
164
|
+
e_1 = _b.sent();
|
|
165
|
+
console.log(e_1.stack);
|
|
166
|
+
return [2 /*return*/, new Response(e_1.stack, {
|
|
167
|
+
status: 500,
|
|
168
|
+
})];
|
|
169
|
+
case 13: return [2 /*return*/];
|
|
173
170
|
}
|
|
174
171
|
});
|
|
175
172
|
}); });
|
|
173
|
+
// export default async (req: Request, context: Context) => {
|
|
174
|
+
// return await handleRequest(req);
|
|
175
|
+
// }
|
|
@@ -34,142 +34,136 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
34
34
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
38
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
39
|
+
if (ar || !(i in from)) {
|
|
40
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
41
|
+
ar[i] = from[i];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
45
|
+
};
|
|
46
|
+
import fs from "node:fs/promises";
|
|
47
|
+
import fsSync from "node:fs";
|
|
48
|
+
import path, { join } from "node:path";
|
|
49
|
+
import { createStaticHandler, createStaticRouter, } from "react-router-dom/server.js";
|
|
46
50
|
// @ts-ignore
|
|
47
|
-
|
|
48
|
-
import { handleRequest } from "rasengan";
|
|
51
|
+
import { createFetchRequest } from "rasengan";
|
|
49
52
|
// Create server for production only
|
|
50
|
-
// export default async function handler(req: VercelRequest, res: VercelResponse) {
|
|
51
|
-
// // @ts-ignore
|
|
52
|
-
// const __filename = fileURLToPath(import.meta.url);
|
|
53
|
-
// const __dirname = dirname(__filename);
|
|
54
|
-
// console.log({ __dirname })
|
|
55
|
-
// try {
|
|
56
|
-
// // Get URL
|
|
57
|
-
// const url = req.url;
|
|
58
|
-
// const host = req.headers.host;
|
|
59
|
-
// // Get app path
|
|
60
|
-
// const appPath = process.cwd();
|
|
61
|
-
// // ! Robots Fix
|
|
62
|
-
// if (url === "/robots.txt") {
|
|
63
|
-
// // Check if robots.txt exists using fs
|
|
64
|
-
// // If it does, return it
|
|
65
|
-
// try {
|
|
66
|
-
// await fs.access(path.resolve(join(appPath, "dist/client/robots.txt")));
|
|
67
|
-
// return res.send(path.resolve(join(appPath, "dist/client/robots.txt")));
|
|
68
|
-
// } catch (err: any) {
|
|
69
|
-
// return res.send(`
|
|
70
|
-
// user-agent: *
|
|
71
|
-
// disallow: /downloads/
|
|
72
|
-
// disallow: /private/
|
|
73
|
-
// allow: /
|
|
74
|
-
// user-agent: magicsearchbot
|
|
75
|
-
// disallow: /uploads/
|
|
76
|
-
// `);
|
|
77
|
-
// }
|
|
78
|
-
// }
|
|
79
|
-
// // ! Sitemap Fix
|
|
80
|
-
// if (url === "/sitemap.xml") {
|
|
81
|
-
// return res.send(path.resolve(join(appPath, "dist/client/sitemap.xml")));
|
|
82
|
-
// }
|
|
83
|
-
// // ! Manifest Fix
|
|
84
|
-
// if (url === "/manifest.json") {
|
|
85
|
-
// return res.send(path.resolve(join(appPath, "dist/client/manifest.json")));
|
|
86
|
-
// }
|
|
87
|
-
// // ! Handle assets
|
|
88
|
-
// if (url.includes("/assets")) {
|
|
89
|
-
// // get segments from /assets to the end
|
|
90
|
-
// const segments = url.split("/");
|
|
91
|
-
// const segmentsWithoutOrigin = [...segments];
|
|
92
|
-
// for (let segment of segments) {
|
|
93
|
-
// if (segment === "assets") {
|
|
94
|
-
// break;
|
|
95
|
-
// }
|
|
96
|
-
// segmentsWithoutOrigin.shift();
|
|
97
|
-
// }
|
|
98
|
-
// // replace assets by client/assets
|
|
99
|
-
// const filePath = join(appPath, "dist/client", segmentsWithoutOrigin.join("/"));
|
|
100
|
-
// const file = await fs.readFile(filePath, "utf-8");
|
|
101
|
-
// return new Response(file, {
|
|
102
|
-
// headers: {
|
|
103
|
-
// "Content-Type": url.endsWith(".js") ? "text/javascript" : "text/css",
|
|
104
|
-
// "Cache-Control": "max-age=31536000",
|
|
105
|
-
// },
|
|
106
|
-
// });
|
|
107
|
-
// }
|
|
108
|
-
// // Template html
|
|
109
|
-
// let templateHtml = "";
|
|
110
|
-
// // Always read fresh template in development
|
|
111
|
-
// const serverFilePath = join(appPath, "dist/server/entry-server.js");
|
|
112
|
-
// const bootstrapDirPath = join(appPath, "dist/client/assets")
|
|
113
|
-
// // Read the entry sever file
|
|
114
|
-
// let entry = await import(serverFilePath);
|
|
115
|
-
// // replace bootstrap script with compiled scripts
|
|
116
|
-
// let bootstrap =
|
|
117
|
-
// "/assets/" +
|
|
118
|
-
// fsSync
|
|
119
|
-
// .readdirSync(bootstrapDirPath)
|
|
120
|
-
// .filter((fn) => fn.includes("entry-client") && fn.endsWith(".js"))[0];
|
|
121
|
-
// // replace styles with compiled styles
|
|
122
|
-
// let styles =
|
|
123
|
-
// "/assets/" +
|
|
124
|
-
// fsSync
|
|
125
|
-
// .readdirSync(join(appPath, "dist/client/assets"))
|
|
126
|
-
// .filter((fn) => fn.includes("entry-client") && fn.endsWith(".css"))[0];
|
|
127
|
-
// // Extract render and staticRoutes from entry
|
|
128
|
-
// const { render, staticRoutes, loadTemplateHtml } = entry;
|
|
129
|
-
// // Create static handler
|
|
130
|
-
// let handler = createStaticHandler(staticRoutes);
|
|
131
|
-
// // Create fetch request for static routing
|
|
132
|
-
// // @ts-ignore
|
|
133
|
-
// let fetchRequest = createFetchRequest(req, host);
|
|
134
|
-
// let context = await handler.query(fetchRequest);
|
|
135
|
-
// // Handle redirects
|
|
136
|
-
// const status = (context as Response).status;
|
|
137
|
-
// if (status === 302) {
|
|
138
|
-
// const redirect = (context as Response).headers.get("Location");
|
|
139
|
-
// if (redirect) return res.redirect(redirect);
|
|
140
|
-
// }
|
|
141
|
-
// // Helmet context
|
|
142
|
-
// const helmetContext = {} as { helmet: any };
|
|
143
|
-
// // Create static router
|
|
144
|
-
// let router = createStaticRouter(
|
|
145
|
-
// handler.dataRoutes,
|
|
146
|
-
// context as StaticHandlerContext
|
|
147
|
-
// );
|
|
148
|
-
// const rendered = await render(router, context, helmetContext);
|
|
149
|
-
// // Load template html
|
|
150
|
-
// if (!templateHtml) {
|
|
151
|
-
// templateHtml = loadTemplateHtml(helmetContext, bootstrap, styles);
|
|
152
|
-
// }
|
|
153
|
-
// // Replacing the app-html placeholder with the rendered html
|
|
154
|
-
// let html = templateHtml.replace(`rasengan-body-app`, rendered.html ?? "");
|
|
155
|
-
// // Send the rendered html page
|
|
156
|
-
// return res
|
|
157
|
-
// .status(200)
|
|
158
|
-
// .setHeader("Content-Type", "text/html")
|
|
159
|
-
// .setHeader("Cache-Control", "max-age=31536000")
|
|
160
|
-
// .end(html);
|
|
161
|
-
// } catch (e: any) {
|
|
162
|
-
// console.log(e.stack);
|
|
163
|
-
// res.status(500).end(e.stack);
|
|
164
|
-
// }
|
|
165
|
-
// }
|
|
166
53
|
export default function handler(req, res) {
|
|
54
|
+
var _a;
|
|
167
55
|
return __awaiter(this, void 0, void 0, function () {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
case
|
|
56
|
+
var url, host, appPath, err_1, segments, segmentsWithoutOrigin, _i, segments_1, segment, filePath, file, file, templateHtml, serverFilePath, bootstrapDirPath, entry, bootstrap, styles, render, staticRoutes, loadTemplateHtml, handler_1, fetchRequest, context, status_1, redirect, helmetContext, router, rendered, html, e_1;
|
|
57
|
+
return __generator(this, function (_b) {
|
|
58
|
+
switch (_b.label) {
|
|
59
|
+
case 0:
|
|
60
|
+
_b.trys.push([0, 12, , 13]);
|
|
61
|
+
url = req.url;
|
|
62
|
+
host = req.headers.host;
|
|
63
|
+
appPath = process.cwd();
|
|
64
|
+
if (!(url === "/robots.txt")) return [3 /*break*/, 4];
|
|
65
|
+
_b.label = 1;
|
|
66
|
+
case 1:
|
|
67
|
+
_b.trys.push([1, 3, , 4]);
|
|
68
|
+
return [4 /*yield*/, fs.access(path.resolve(join(appPath, "dist/client/robots.txt")))];
|
|
69
|
+
case 2:
|
|
70
|
+
_b.sent();
|
|
71
|
+
return [2 /*return*/, res.send(path.resolve(join(appPath, "dist/client/robots.txt")))];
|
|
72
|
+
case 3:
|
|
73
|
+
err_1 = _b.sent();
|
|
74
|
+
return [2 /*return*/, res.send("\n user-agent: *\n disallow: /downloads/\n disallow: /private/\n allow: /\n \n user-agent: magicsearchbot\n disallow: /uploads/\n ")];
|
|
75
|
+
case 4:
|
|
76
|
+
// ! Sitemap Fix
|
|
77
|
+
if (url === "/sitemap.xml") {
|
|
78
|
+
return [2 /*return*/, res.send(path.resolve(join(appPath, "dist/client/sitemap.xml")))];
|
|
79
|
+
}
|
|
80
|
+
// ! Manifest Fix
|
|
81
|
+
if (url === "/manifest.json") {
|
|
82
|
+
return [2 /*return*/, res.send(path.resolve(join(appPath, "dist/client/manifest.json")))];
|
|
83
|
+
}
|
|
84
|
+
if (!url.includes("/assets")) return [3 /*break*/, 6];
|
|
85
|
+
segments = url.split("/");
|
|
86
|
+
segmentsWithoutOrigin = __spreadArray([], segments, true);
|
|
87
|
+
for (_i = 0, segments_1 = segments; _i < segments_1.length; _i++) {
|
|
88
|
+
segment = segments_1[_i];
|
|
89
|
+
if (segment === "assets") {
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
segmentsWithoutOrigin.shift();
|
|
93
|
+
}
|
|
94
|
+
filePath = join(appPath, "dist/client", segmentsWithoutOrigin.join("/"));
|
|
95
|
+
return [4 /*yield*/, fs.readFile(filePath, "utf-8")];
|
|
96
|
+
case 5:
|
|
97
|
+
file = _b.sent();
|
|
98
|
+
return [2 /*return*/, new Response(file, {
|
|
99
|
+
headers: {
|
|
100
|
+
"Content-Type": url.endsWith(".js") ? "text/javascript" : "text/css",
|
|
101
|
+
"Cache-Control": "max-age=31536000",
|
|
102
|
+
},
|
|
103
|
+
})];
|
|
104
|
+
case 6:
|
|
105
|
+
if (!(url.endsWith(".js") || url.endsWith(".css"))) return [3 /*break*/, 8];
|
|
106
|
+
return [4 /*yield*/, fs.readFile(url, "utf-8")];
|
|
107
|
+
case 7:
|
|
108
|
+
file = _b.sent();
|
|
109
|
+
return [2 /*return*/, res
|
|
110
|
+
.status(200)
|
|
111
|
+
.setHeader("Content-Type", url.endsWith(".js") ? "text/javascript" : "text/css")
|
|
112
|
+
.setHeader("Cache-Control", "max-age=31536000")
|
|
113
|
+
.end(file)];
|
|
114
|
+
case 8:
|
|
115
|
+
templateHtml = "";
|
|
116
|
+
serverFilePath = join(appPath, "dist/server/entry-server.js");
|
|
117
|
+
bootstrapDirPath = join(appPath, "dist/client/assets");
|
|
118
|
+
return [4 /*yield*/, import(serverFilePath)];
|
|
119
|
+
case 9:
|
|
120
|
+
entry = _b.sent();
|
|
121
|
+
bootstrap = "/assets/" +
|
|
122
|
+
fsSync
|
|
123
|
+
.readdirSync(bootstrapDirPath)
|
|
124
|
+
.filter(function (fn) { return fn.includes("entry-client") && fn.endsWith(".js"); })[0];
|
|
125
|
+
styles = "/assets/" +
|
|
126
|
+
fsSync
|
|
127
|
+
.readdirSync(join(appPath, "dist/client/assets"))
|
|
128
|
+
.filter(function (fn) { return fn.includes("entry-client") && fn.endsWith(".css"); })[0];
|
|
129
|
+
render = entry.render, staticRoutes = entry.staticRoutes, loadTemplateHtml = entry.loadTemplateHtml;
|
|
130
|
+
handler_1 = createStaticHandler(staticRoutes);
|
|
131
|
+
fetchRequest = createFetchRequest(req, host);
|
|
132
|
+
return [4 /*yield*/, handler_1.query(fetchRequest)];
|
|
133
|
+
case 10:
|
|
134
|
+
context = _b.sent();
|
|
135
|
+
status_1 = context.status;
|
|
136
|
+
if (status_1 === 302) {
|
|
137
|
+
redirect = context.headers.get("Location");
|
|
138
|
+
if (redirect)
|
|
139
|
+
return [2 /*return*/, res.redirect(redirect)];
|
|
140
|
+
}
|
|
141
|
+
helmetContext = {};
|
|
142
|
+
router = createStaticRouter(handler_1.dataRoutes, context);
|
|
143
|
+
return [4 /*yield*/, render(router, context, helmetContext)];
|
|
144
|
+
case 11:
|
|
145
|
+
rendered = _b.sent();
|
|
146
|
+
// Load template html
|
|
147
|
+
if (!templateHtml) {
|
|
148
|
+
templateHtml = loadTemplateHtml(helmetContext, bootstrap, styles);
|
|
149
|
+
}
|
|
150
|
+
html = templateHtml.replace("rasengan-body-app", (_a = rendered.html) !== null && _a !== void 0 ? _a : "");
|
|
151
|
+
// Send the rendered html page
|
|
152
|
+
return [2 /*return*/, res
|
|
153
|
+
.status(200)
|
|
154
|
+
.setHeader("Content-Type", "text/html")
|
|
155
|
+
.setHeader("Cache-Control", "max-age=31536000")
|
|
156
|
+
.end(html)];
|
|
157
|
+
case 12:
|
|
158
|
+
e_1 = _b.sent();
|
|
159
|
+
console.log(e_1.stack);
|
|
160
|
+
res.status(500).end(e_1.stack);
|
|
161
|
+
return [3 /*break*/, 13];
|
|
162
|
+
case 13: return [2 /*return*/];
|
|
172
163
|
}
|
|
173
164
|
});
|
|
174
165
|
});
|
|
175
166
|
}
|
|
167
|
+
// export default async function handler(req: VercelRequest, res: VercelResponse) {
|
|
168
|
+
// return await handleRequest(req, res);
|
|
169
|
+
// }
|
|
@@ -50,7 +50,7 @@ import { createStaticHandler, createStaticRouter, } from "react-router-dom/serve
|
|
|
50
50
|
// @ts-ignore
|
|
51
51
|
import { createFetchRequest } from "rasengan";
|
|
52
52
|
// Create server for production only
|
|
53
|
-
export
|
|
53
|
+
export function handleRequest(req, res) {
|
|
54
54
|
var _a;
|
|
55
55
|
return __awaiter(this, void 0, void 0, function () {
|
|
56
56
|
var url, host, appPath, err_1, segments, segmentsWithoutOrigin, _i, segments_1, segment, filePath, file, file, templateHtml, serverFilePath, bootstrapDirPath, entry, bootstrap, styles, render, staticRoutes, loadTemplateHtml, handler, fetchRequest, context, status_1, redirect, helmetContext, router, rendered, html, e_1;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import createFetchRequest from "./server/utils/createFetchRequest.js";
|
|
2
|
-
import handleRequest from "./server/utils/handleRequest.js";
|
|
2
|
+
import { handleRequest } from "./server/utils/handleRequest.js";
|
|
3
3
|
export * from "./core/index.js";
|
|
4
4
|
export * from "./routing/index.js";
|
|
5
5
|
export * from "./config/index.js";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: (req: Request, context: Context) => Promise<
|
|
1
|
+
declare const _default: (req: Request, context: Context) => Promise<Response>;
|
|
2
2
|
export default _default;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { VercelRequest, VercelResponse } from "@vercel/node";
|
|
2
|
-
export default function handler(req: VercelRequest, res: VercelResponse): Promise<
|
|
2
|
+
export default function handler(req: VercelRequest, res: VercelResponse): Promise<Response | VercelResponse>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export declare function handleRequest(req: any, res?: any): Promise<any>;
|