@websline/cms-view-utils 1.2.0 → 1.2.1

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": "@websline/cms-view-utils",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src",
@@ -0,0 +1,20 @@
1
+ import { buildCmsSiteConfigUrl } from "./urlResolver.js";
2
+ import { buildCmsHeaders } from "./headers.js";
3
+ import { HttpError } from "../shared/errors.js";
4
+
5
+ const fetchSiteConfig = async (request) => {
6
+ const cmsUrl = buildCmsSiteConfigUrl();
7
+
8
+ const response = await fetch(cmsUrl, {
9
+ method: "GET",
10
+ headers: buildCmsHeaders(request),
11
+ });
12
+
13
+ if (!response.ok) {
14
+ throw new HttpError(response.status, "CMS Error");
15
+ }
16
+
17
+ return response.json();
18
+ };
19
+
20
+ export { fetchSiteConfig };
@@ -1,19 +1,21 @@
1
- const buildCmsPageUrl = ({ draftUuid, path }) => {
2
- const base = import.meta.env.CMS_URL;
1
+ const CMS_BASE_URL = import.meta.env.CMS_URL;
3
2
 
3
+ const buildCmsPageUrl = ({ draftUuid, path }) => {
4
4
  if (draftUuid) {
5
- return `${base}/api/public/pages/preview/${draftUuid}`;
5
+ return `${CMS_BASE_URL}/api/public/pages/preview/${draftUuid}`;
6
6
  }
7
7
 
8
8
  const normalizedPath = path.startsWith("/") ? path : `/${path}`;
9
9
 
10
- return `${base}/api/public/pages${normalizedPath}`;
10
+ return `${CMS_BASE_URL}/api/public/pages${normalizedPath}`;
11
11
  };
12
12
 
13
- const buildCmsSitemapUrl = () => {
14
- const base = import.meta.env.CMS_URL;
13
+ const buildCmsSiteConfigUrl = () => {
14
+ return `${CMS_BASE_URL}/api/public/site-config`;
15
+ };
15
16
 
16
- return `${base}/api/public/sitemap`;
17
+ const buildCmsSitemapUrl = () => {
18
+ return `${CMS_BASE_URL}/api/public/sitemap`;
17
19
  };
18
20
 
19
- export { buildCmsPageUrl, buildCmsSitemapUrl };
21
+ export { buildCmsPageUrl, buildCmsSiteConfigUrl, buildCmsSitemapUrl };
@@ -1,3 +1,6 @@
1
+ import { fetchSiteConfig } from "../cms/fetchSiteConfig.js";
2
+ import { HttpError } from "../shared/errors.js";
3
+
1
4
  const buildRobotsTxt = ({ siteUrl, allowIndexing }) => {
2
5
  if (!allowIndexing) {
3
6
  return `User-agent: *
@@ -12,29 +15,31 @@ Sitemap: ${siteUrl}/sitemap.xml
12
15
  `;
13
16
  };
14
17
 
15
- const resolveSiteUrl = (request) => {
16
- const url = new URL(request.url);
17
- return `${url.protocol}//${url.host}`;
18
- };
19
-
20
- // TODO: replace with CMS setting once available
21
- const resolveAllowIndexing = () => true;
22
-
23
18
  const createRobotsHandler =
24
19
  () =>
25
20
  async ({ request }) => {
26
- const siteUrl = resolveSiteUrl(request);
27
- const allowIndexing = resolveAllowIndexing();
28
-
29
- const body = buildRobotsTxt({ siteUrl, allowIndexing });
30
-
31
- return new Response(body, {
32
- status: 200,
33
- headers: {
34
- "Content-Type": "text/plain; charset=utf-8",
35
- "Cache-Control": "no-store",
36
- },
37
- });
21
+ try {
22
+ const siteConfig = await fetchSiteConfig(request);
23
+ const siteUrl = siteConfig?.url;
24
+ const allowIndexing = siteConfig?.mode === "PRODUCTION";
25
+
26
+ const body = buildRobotsTxt({ siteUrl, allowIndexing });
27
+
28
+ return new Response(body, {
29
+ status: 200,
30
+ headers: {
31
+ "Content-Type": "text/plain; charset=utf-8",
32
+ "Cache-Control": "no-store",
33
+ },
34
+ });
35
+ } catch (error) {
36
+ if (error instanceof HttpError) {
37
+ return new Response(`Robots error: ${error.message}`, {
38
+ status: error.status,
39
+ });
40
+ }
41
+ throw error;
42
+ }
38
43
  };
39
44
 
40
45
  export { createRobotsHandler };
@@ -1,20 +1,18 @@
1
1
  import { fetchSitemap } from "../cms/fetchSitemap.js";
2
+ import { fetchSiteConfig } from "../cms/fetchSiteConfig.js";
2
3
  import { buildSitemapXml } from "./sitemapXml.js";
3
4
  import { HttpError } from "../shared/errors.js";
4
5
 
5
- const resolveSiteUrl = (request) => {
6
- const url = new URL(request.url);
7
- return `${url.protocol}//${url.host}`;
8
- };
9
-
10
6
  const createSitemapHandler =
11
7
  () =>
12
8
  async ({ request }) => {
13
- const siteUrl = resolveSiteUrl(request);
14
-
15
9
  try {
16
- const entries = await fetchSitemap(request);
17
- const xml = buildSitemapXml(entries, siteUrl);
10
+ const [entries, siteConfig] = await Promise.all([
11
+ fetchSitemap(request),
12
+ fetchSiteConfig(request),
13
+ ]);
14
+
15
+ const xml = buildSitemapXml(entries, siteConfig?.url);
18
16
 
19
17
  return new Response(xml, {
20
18
  status: 200,