From f5afb5291930e2e781f1e51245d5a3101611797f Mon Sep 17 00:00:00 2001 From: Harlan Wilton Date: Fri, 29 Dec 2023 00:37:01 +1100 Subject: [PATCH] chore: safer transforms --- src/runtime/sitemap/urlset/normalise.ts | 9 +++++++-- test/integration/single/lastmod.test.ts | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/runtime/sitemap/urlset/normalise.ts b/src/runtime/sitemap/urlset/normalise.ts index bf8c47f..58a11ba 100644 --- a/src/runtime/sitemap/urlset/normalise.ts +++ b/src/runtime/sitemap/urlset/normalise.ts @@ -102,10 +102,15 @@ export function normaliseDate(date: string | Date): string export function normaliseDate(d: Date | string) { // lastmod must adhere to W3C Datetime encoding rules if (typeof d === 'string') { - // we may have a value like this "2023-12-21T13:49:27.963745", this needs to be converted to w3c datetime + // we may have milliseconds at the end with a dot prefix like ".963745", we should remove this + d = d.replace(/\.\d+$/, '') + console.log('match', d, d.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/)) + // we may have a value like this "2023-12-21T13:49:27", this needs to be converted to w3c datetime // accept if they are already in the right format, accept small format too such as "2023-12-21" - if (d.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/) || d.match(/^\d{4}-\d{2}-\d{2}$/)) + if (d.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/) || d.match(/^\d{4}-\d{2}-\d{2}$/)) { + console.log('pass') return d + } // otherwise we need to parse it d = new Date(d) // check for invalid date diff --git a/test/integration/single/lastmod.test.ts b/test/integration/single/lastmod.test.ts index 1e2b2dc..e949cb0 100644 --- a/test/integration/single/lastmod.test.ts +++ b/test/integration/single/lastmod.test.ts @@ -60,7 +60,7 @@ describe('lastmod', () => { https://nuxtseo.com/foo - 2023-12-21T02:49:27+00:00 + 2023-12-21T13:49:27 https://nuxtseo.com/quux