astro-tractstack 2.0.10 → 2.0.11

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": "astro-tractstack",
3
- "version": "2.0.10",
3
+ "version": "2.0.11",
4
4
  "description": "Astro integration for TractStack - redeeming the web from boring experiences",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -2,12 +2,26 @@ import type { APIRoute } from '@/types/astro';
2
2
  import { getBrandConfig } from '@/utils/api/brandConfig';
3
3
 
4
4
  // Helper functions for date formatting
5
- function dateToUnixTimestamp(dateString: string): number {
6
- return new Date(dateString).getTime();
5
+ function dateToUnixTimestamp(dateString: string | undefined | null): number {
6
+ if (!dateString) {
7
+ return 0;
8
+ }
9
+ const timestamp = new Date(dateString).getTime();
10
+ // Return 0 if the date string is invalid
11
+ return isNaN(timestamp) ? 0 : timestamp;
7
12
  }
8
13
 
9
- function formatDateToYYYYMMDD(dateString: string): string {
14
+ function formatDateToYYYYMMDD(
15
+ dateString: string | undefined | null
16
+ ): string | null {
17
+ if (!dateString) {
18
+ return null;
19
+ }
10
20
  const date = new Date(dateString);
21
+ // Check if the date is valid
22
+ if (isNaN(date.getTime())) {
23
+ return null;
24
+ }
11
25
  return date.toISOString().split('T')[0];
12
26
  }
13
27
 
@@ -54,16 +68,26 @@ export const GET: APIRoute = async ({ request }) => {
54
68
 
55
69
  const entries = contentMap
56
70
  .map((c: any) => {
71
+ // Use a single date, fallback to created, check for validity
72
+ const formatted = formatDateToYYYYMMDD(c.changed || c.created);
73
+
74
+ // If we have no valid date or no slug, skip this entry.
75
+ if (!formatted || !c.slug) {
76
+ return null;
77
+ }
78
+
57
79
  if (c.type === 'StoryFragment') {
58
80
  const thisPriority = c.slug === homeSlug ? '1.0' : '0.8';
59
81
  const thisUrl =
60
82
  c.slug === homeSlug
61
83
  ? new URL('/', siteUrl).href
62
84
  : new URL(c.slug, siteUrl).href;
63
- const thisChanged = (c?.changed && dateToUnixTimestamp(c.changed)) || 0;
85
+
86
+ const thisChanged = dateToUnixTimestamp(c.changed);
64
87
  const thisCreated = dateToUnixTimestamp(c.created);
88
+
65
89
  const daysDelta = (thisChanged - thisCreated) / (1000 * 60 * 60 * 24);
66
- const formatted = formatDateToYYYYMMDD(c.changed || c.created);
90
+
67
91
  const thisFreq =
68
92
  daysDelta < 3
69
93
  ? 'daily'
@@ -74,12 +98,15 @@ export const GET: APIRoute = async ({ request }) => {
74
98
  : 'yearly';
75
99
  return `<url><loc>${thisUrl}</loc><lastmod>${formatted}</lastmod><changefreq>${thisFreq}</changefreq><priority>${thisPriority}</priority></url>`;
76
100
  }
101
+
77
102
  if (c.type === 'Pane' && c.isContext) {
78
103
  const thisUrl = new URL(`context/${c.slug}`, siteUrl).href;
79
- const thisChanged = (c?.changed && dateToUnixTimestamp(c.changed)) || 0;
104
+
105
+ const thisChanged = dateToUnixTimestamp(c.changed);
80
106
  const thisCreated = dateToUnixTimestamp(c.created);
107
+
81
108
  const daysDelta = (thisChanged - thisCreated) / (1000 * 60 * 60 * 24);
82
- const formatted = formatDateToYYYYMMDD(c.changed || c.created);
109
+
83
110
  const thisFreq =
84
111
  daysDelta < 3
85
112
  ? 'daily'
@@ -90,8 +117,11 @@ export const GET: APIRoute = async ({ request }) => {
90
117
  : 'yearly';
91
118
  return `<url><loc>${thisUrl}</loc><lastmod>${formatted}</lastmod><changefreq>${thisFreq}</changefreq><priority>0.4</priority></url>`;
92
119
  }
120
+
121
+ // Return null for any other types we don't care about
122
+ return null;
93
123
  })
94
- .filter((n) => n);
124
+ .filter((n) => n); // This filter removes all the null entries
95
125
 
96
126
  const xmlBody = entries.join('');
97
127
  const xml = `${xmlTop}${xmlBody}${xmlBottom}`;