@leadertechie/personal-site-kit 0.1.0-alpha.11 → 0.1.0-alpha.12
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.
|
@@ -6,6 +6,9 @@ export interface PrerenderOptions {
|
|
|
6
6
|
siteTitle?: string;
|
|
7
7
|
copyright?: string;
|
|
8
8
|
templateRenderer?: (props: TemplateProps) => Promise<string> | string;
|
|
9
|
+
apiHandler?: {
|
|
10
|
+
fetch: (request: Request, env: any, ctx: any) => Promise<Response> | Response;
|
|
11
|
+
};
|
|
9
12
|
}
|
|
10
13
|
export declare class WebsitePrerender {
|
|
11
14
|
private routes;
|
|
@@ -14,6 +17,7 @@ export declare class WebsitePrerender {
|
|
|
14
17
|
private siteTitle;
|
|
15
18
|
private copyright;
|
|
16
19
|
private templateRenderer;
|
|
20
|
+
private apiHandler?;
|
|
17
21
|
constructor(options?: PrerenderOptions);
|
|
18
22
|
private fetchStaticDetails;
|
|
19
23
|
private fetchAboutMeData;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"website-prerender.d.ts","sourceRoot":"","sources":["../../src/prerender/website-prerender.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,aAAa,EAAE,MAAM,YAAY,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,EAAuB,MAAM,gBAAgB,CAAC;AAE1E,MAAM,WAAW,gBAAgB;IAC/B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,kBAAkB,CAAC,EAAE,WAAW,EAAE,CAAC;IACnC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"website-prerender.d.ts","sourceRoot":"","sources":["../../src/prerender/website-prerender.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,aAAa,EAAE,MAAM,YAAY,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,EAAuB,MAAM,gBAAgB,CAAC;AAE1E,MAAM,WAAW,gBAAgB;IAC/B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,kBAAkB,CAAC,EAAE,WAAW,EAAE,CAAC;IACnC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;IACtE,UAAU,CAAC,EAAE;QAAE,KAAK,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK,OAAO,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAA;KAAE,CAAC;CAChG;AAED,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,kBAAkB,CAAgB;IAC1C,OAAO,CAAC,WAAW,CAAgB;IACnC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,gBAAgB,CAAqD;IAC7E,OAAO,CAAC,UAAU,CAAC,CAAoF;gBAE3F,OAAO,GAAE,gBAAqB;YAmB5B,kBAAkB;YAiClB,gBAAgB;IAajB,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,OAAO,CAAC,QAAQ,CAAC;CA4F5E"}
|
package/dist/prerender.js
CHANGED
|
@@ -301,12 +301,19 @@ class WebsitePrerender {
|
|
|
301
301
|
this.siteTitle = options.siteTitle || "My Personal Website";
|
|
302
302
|
this.copyright = options.copyright || "2026 My Personal Website";
|
|
303
303
|
this.templateRenderer = options.templateRenderer || createHtmlTemplate;
|
|
304
|
+
this.apiHandler = options.apiHandler;
|
|
304
305
|
}
|
|
305
|
-
async fetchStaticDetails(apiUrl) {
|
|
306
|
+
async fetchStaticDetails(apiUrl, env) {
|
|
306
307
|
try {
|
|
307
|
-
|
|
308
|
-
if (
|
|
309
|
-
const
|
|
308
|
+
let data;
|
|
309
|
+
if (this.apiHandler) {
|
|
310
|
+
const res = await this.apiHandler.fetch(new Request(`${apiUrl}/api/static`), env, {});
|
|
311
|
+
if (res.ok) data = await res.json();
|
|
312
|
+
} else {
|
|
313
|
+
const res = await fetch(`${apiUrl}/api/static`);
|
|
314
|
+
if (res.ok) data = await res.json();
|
|
315
|
+
}
|
|
316
|
+
if (data) {
|
|
310
317
|
this.siteTitle = data.siteTitle || this.siteTitle;
|
|
311
318
|
this.copyright = data.copyright || this.copyright;
|
|
312
319
|
const normalizeUrl = (url) => {
|
|
@@ -324,10 +331,15 @@ class WebsitePrerender {
|
|
|
324
331
|
} catch (e) {
|
|
325
332
|
}
|
|
326
333
|
}
|
|
327
|
-
async fetchAboutMeData(apiUrl) {
|
|
334
|
+
async fetchAboutMeData(apiUrl, env) {
|
|
328
335
|
try {
|
|
329
|
-
|
|
330
|
-
|
|
336
|
+
if (this.apiHandler) {
|
|
337
|
+
const res = await this.apiHandler.fetch(new Request(`${apiUrl}/api/aboutme`), env, {});
|
|
338
|
+
if (res.ok) return await res.json();
|
|
339
|
+
} else {
|
|
340
|
+
const res = await fetch(`${apiUrl}/api/aboutme`);
|
|
341
|
+
if (res.ok) return await res.json();
|
|
342
|
+
}
|
|
331
343
|
} catch (e) {
|
|
332
344
|
}
|
|
333
345
|
return null;
|
|
@@ -335,9 +347,12 @@ class WebsitePrerender {
|
|
|
335
347
|
async fetch(request, env, ctx) {
|
|
336
348
|
const apiUrl = env?.API_URL || "https://api.example.com";
|
|
337
349
|
const baseSiteUrl = env?.BASE_SITE_URL || "https://site.example.com";
|
|
338
|
-
await this.fetchStaticDetails(apiUrl);
|
|
350
|
+
await this.fetchStaticDetails(apiUrl, env);
|
|
339
351
|
const url = new URL(request.url);
|
|
340
352
|
if (url.pathname.startsWith("/api/")) {
|
|
353
|
+
if (this.apiHandler) {
|
|
354
|
+
return this.apiHandler.fetch(request, env, ctx);
|
|
355
|
+
}
|
|
341
356
|
return fetch(`${apiUrl}${url.pathname}${url.search}`);
|
|
342
357
|
}
|
|
343
358
|
if (url.pathname.startsWith("/images/")) {
|
|
@@ -348,12 +363,16 @@ class WebsitePrerender {
|
|
|
348
363
|
return new Response(image.body, {
|
|
349
364
|
headers: {
|
|
350
365
|
"content-type": image.httpMetadata?.contentType || "image/jpeg",
|
|
351
|
-
"cache-control": "public, max-age=86400"
|
|
366
|
+
"cache-control": "public, max-age=86400",
|
|
367
|
+
"access-control-allow-origin": "*"
|
|
352
368
|
}
|
|
353
369
|
});
|
|
354
370
|
}
|
|
355
371
|
} catch (e) {
|
|
356
372
|
}
|
|
373
|
+
if (this.apiHandler) {
|
|
374
|
+
return this.apiHandler.fetch(request, env, ctx);
|
|
375
|
+
}
|
|
357
376
|
return new Response("Not found", { status: 404 });
|
|
358
377
|
}
|
|
359
378
|
if (url.pathname.startsWith("/assets/") || url.pathname === "/logo.png" || url.pathname === "/favicon.ico") {
|
|
@@ -390,7 +409,7 @@ class WebsitePrerender {
|
|
|
390
409
|
}
|
|
391
410
|
let hydrationScript = "";
|
|
392
411
|
if (url.pathname === "/about-me" || url.pathname === "/about-me/") {
|
|
393
|
-
const aboutMeData = await this.fetchAboutMeData(apiUrl);
|
|
412
|
+
const aboutMeData = await this.fetchAboutMeData(apiUrl, env);
|
|
394
413
|
if (aboutMeData) {
|
|
395
414
|
hydrationScript = `<script>window.__HYDRATION_DATA__ = ${JSON.stringify(aboutMeData)};<\/script>`;
|
|
396
415
|
}
|
package/package.json
CHANGED