@jjlmoya/utils-science 1.58.0 → 1.59.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": "@jjlmoya/utils-science",
3
- "version": "1.58.0",
3
+ "version": "1.59.0",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -37,7 +37,6 @@
37
37
  "predev": "node scripts/postinstall.mjs",
38
38
  "prestart": "node scripts/postinstall.mjs",
39
39
  "prebuild": "node scripts/postinstall.mjs",
40
- "postbuild": "node scripts/postbuild.mjs",
41
40
  "qa": "npm run lint && npm run test && npm run build",
42
41
  "cf:dry-run": "npm run build && wrangler deploy --dry-run",
43
42
  "cf:preview": "npm run build && wrangler deploy --config wrangler.staging.jsonc",
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from "vitest";
2
- import { getCacheControl, LONG_LIVED_ASSET_CACHE, SITEMAP_CACHE } from "../worker";
2
+ import { getCacheControl, LONG_LIVED_ASSET_CACHE } from "../worker";
3
3
 
4
4
  describe("MFE cache contract", () => {
5
5
  it("caches versioned assets and manifests for one year", () => {
@@ -8,7 +8,4 @@ describe("MFE cache contract", () => {
8
8
  expect(getCacheControl("/en/utilities/categories/science/tool/manifest.json")).toBe(LONG_LIVED_ASSET_CACHE);
9
9
  });
10
10
 
11
- it("keeps sitemaps refreshable", () => {
12
- expect(getCacheControl("/_utilities/en/science/sitemap.xml")).toBe(SITEMAP_CACHE);
13
- });
14
11
  });
package/src/worker.ts CHANGED
@@ -3,10 +3,8 @@ interface UtilityMfeEnvironment {
3
3
  }
4
4
 
5
5
  export const LONG_LIVED_ASSET_CACHE = "public, max-age=31536000, immutable";
6
- export const SITEMAP_CACHE = "public, max-age=3600, s-maxage=3600, must-revalidate";
7
6
 
8
7
  export const getCacheControl = (pathname: string): string | undefined => {
9
- if (pathname.endsWith("/sitemap.xml")) return SITEMAP_CACHE;
10
8
  if (pathname.endsWith("/manifest.json")) return LONG_LIVED_ASSET_CACHE;
11
9
  if (pathname.startsWith("/_utilities/")) {
12
10
  return LONG_LIVED_ASSET_CACHE;
@@ -1,17 +0,0 @@
1
- import { copyFileSync, existsSync, mkdirSync, rmSync, readdirSync } from 'node:fs';
2
- import { join, resolve } from 'node:path';
3
-
4
- const sourceRoot = resolve('dist/mfe-sitemaps');
5
- const destinationRoot = resolve('dist/_utilities');
6
-
7
- if (existsSync(sourceRoot)) {
8
- for (const locale of readdirSync(sourceRoot, { withFileTypes: true }).filter((entry) => entry.isDirectory())) {
9
- for (const vertical of readdirSync(join(sourceRoot, locale.name), { withFileTypes: true }).filter((entry) => entry.isDirectory())) {
10
- const source = join(sourceRoot, locale.name, vertical.name, 'sitemap.xml');
11
- const destinationDirectory = join(destinationRoot, locale.name, vertical.name);
12
- mkdirSync(destinationDirectory, { recursive: true });
13
- copyFileSync(source, join(destinationDirectory, 'sitemap.xml'));
14
- }
15
- }
16
- rmSync(sourceRoot, { recursive: true, force: true });
17
- }
@@ -1,75 +0,0 @@
1
- import type { APIRoute } from "astro";
2
- import { scienceCategory } from "../../../../category";
3
- import { ALL_ENTRIES } from "../../../../entries";
4
- import { LANGUAGE_CODES, type Language } from "../../../../i18n/languages";
5
- import { getCategoryPath, getUtilityPath } from "../../../../mfe/routes";
6
-
7
- const GAMEBOB_URL = "https://www.gamebob.dev";
8
- const JJLMOYA_URL = "https://www.jjlmoya.es";
9
-
10
- type LocalizedEntry = {
11
- i18n: Partial<Record<Language, () => Promise<{ slug: string }>>>;
12
- };
13
-
14
- const absoluteUrl = (locale: Language, path: string): string =>
15
- path.startsWith("http") ? path : `${locale === "es" ? JJLMOYA_URL : GAMEBOB_URL}${path}`;
16
-
17
- const escapeXml = (value: string): string => value
18
- .replaceAll("&", "&amp;")
19
- .replaceAll("\"", "&quot;")
20
- .replaceAll("'", "&apos;")
21
- .replaceAll("<", "&lt;")
22
- .replaceAll(">", "&gt;");
23
-
24
- const getContent = async (entry: LocalizedEntry, locale: Language): Promise<{ slug: string }> => {
25
- const loader = entry.i18n[locale] ?? entry.i18n.en;
26
- if (!loader) throw new Error(`Missing ${locale} and English fallback`);
27
- return loader();
28
- };
29
-
30
- export const prerender = true;
31
-
32
- export function getStaticPaths() {
33
- return LANGUAGE_CODES.map((locale) => ({
34
- params: { locale, vertical: "science" },
35
- props: { locale },
36
- }));
37
- }
38
-
39
- const buildUrlEntry = async (locale: Language, path: string, index: number): Promise<string> => {
40
- const links = await Promise.all(LANGUAGE_CODES.map(async (alternateLocale) => {
41
- const alternateCategory = await getContent(scienceCategory, alternateLocale);
42
- const alternateContent = index === 0 ? null : await getContent(ALL_ENTRIES[index - 1]!, alternateLocale);
43
- const alternatePath = alternateContent
44
- ? getUtilityPath(alternateLocale, alternateCategory.slug, alternateContent.slug)
45
- : getCategoryPath(alternateLocale, alternateCategory.slug);
46
- return ` <xhtml:link rel="alternate" hreflang="${alternateLocale}" href="${escapeXml(absoluteUrl(alternateLocale, alternatePath))}"/>`;
47
- }));
48
- return [
49
- " <url>",
50
- ` <loc>${escapeXml(absoluteUrl(locale, path))}</loc>`,
51
- ...links,
52
- ` <changefreq>${index === 0 ? "weekly" : "monthly"}</changefreq>`,
53
- ` <priority>${index === 0 ? "0.7" : "0.6"}</priority>`,
54
- " </url>",
55
- ].join("\n");
56
- };
57
-
58
- export const GET: APIRoute = async ({ props }) => {
59
- const locale = props.locale as Language;
60
- const category = await getContent(scienceCategory, locale);
61
- const contents = await Promise.all(ALL_ENTRIES.map((entry) => getContent(entry, locale)));
62
- const paths = [
63
- getCategoryPath(locale, category.slug),
64
- ...contents.map((content) => getUtilityPath(locale, category.slug, content.slug)),
65
- ];
66
- const urls = await Promise.all(paths.map((path, index) => buildUrlEntry(locale, path, index)));
67
-
68
- const xml = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">\n${urls.join("\n")}\n</urlset>`;
69
- return new Response(xml, {
70
- headers: {
71
- "Content-Type": "application/xml; charset=utf-8",
72
- "Cache-Control": "public, max-age=3600, s-maxage=3600, must-revalidate",
73
- },
74
- });
75
- };