Skip to content

Commit

Permalink
fix: only transform lastmod when it's not w3c format
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jan 14, 2024
1 parent e96cfec commit 127b694
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .playground/nuxt.config.ts
Expand Up @@ -158,7 +158,7 @@ export default defineNuxtConfig({
},
'/about': {
sitemap: {
lastmod: "2023-01-21",
lastmod: '2023-01-21',
changefreq: 'daily',
priority: 0.3,
images: [
Expand Down
20 changes: 14 additions & 6 deletions src/runtime/nitro/sitemap/urlset/normalise.ts
Expand Up @@ -98,19 +98,27 @@ export function normaliseSitemapUrls(data: SitemapUrlInput[], resolvers: NitroUr
)
}

const IS_VALID_W3C_DATE = [
/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/,
/^\d{4}-[01]\d-[0-3]\d$/,
/^\d{4}-[01]\d$/,
/^\d{4}$/,
]
export function isValidW3CDate(d: string) {
return IS_VALID_W3C_DATE.some(r => r.test(d))
}

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') {
// accept if they are already in the right format, accept small format too such as "2023-12-21"
if (isValidW3CDate(d))
return d
// we may have milliseconds at the end with a dot prefix like ".963745", we should remove this
d = d.replace('Z', '')
d = d.replace(/\.\d+$/, '')
// 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"
const validW3CDate = /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/
if (d.match(validW3CDate) || d.match(/^\d{4}-\d{2}-\d{2}$/))
return d

d = d.replace(/\.\d+$/, '')
// otherwise we need to parse it
d = new Date(d)
// check for invalid date
Expand Down
14 changes: 14 additions & 0 deletions test/unit/lastmod.test.ts
@@ -0,0 +1,14 @@
import { describe, expect, it } from 'vitest'
import { isValidW3CDate } from '../../src/runtime/nitro/sitemap/urlset/normalise'

describe('lastmod', () => {
it('default', async () => {
expect(isValidW3CDate('2023-12-21')).toBeTruthy()
expect(isValidW3CDate('2023-12-21T22:46:58Z')).toBeTruthy()
expect(isValidW3CDate('2023-12-21T22:46:58+00:00')).toBeTruthy()
expect(isValidW3CDate('2023-12-21T22:46:58.441+00:00')).toBeTruthy()
expect(isValidW3CDate('1994-11-05T13:15:30Z')).toBeTruthy()
expect(isValidW3CDate('1994-11-05T08:15:30-05:00')).toBeTruthy()
expect(isValidW3CDate('1994-11-05T08:15:30-05:00')).toBeTruthy()
})
})

0 comments on commit 127b694

Please sign in to comment.