Skip to content

Commit

Permalink
fix: prefer lastmod Z timezone over +00:00, drop ms
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jan 14, 2024
1 parent 127b694 commit 30f7440
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
17 changes: 11 additions & 6 deletions src/runtime/nitro/sitemap/urlset/normalise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,20 @@ 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"
// use as if if it's already valid
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', '')
// we may have a value like this "2023-12-21T13:49:27", this needs to be converted to w3c datetime
d = d.replace(/\.\d+$/, '')
// correct a time component without a timezone
if (d.includes('T')) {
const t = d.split('T')[1]
if (!t.includes('+') && !t.includes('-') && !t.includes('Z')) {
// add UTC timezone
d += 'Z'
}
}
// otherwise we need to parse it
d = new Date(d)
d.setMilliseconds(0)
// check for invalid date
if (Number.isNaN(d.getTime()))
return false
Expand All @@ -138,6 +143,6 @@ export function normaliseDate(d: Date | string) {
z(d.getUTCMinutes())
}:${
z(d.getUTCSeconds())
}+00:00`
}Z`
)
}
6 changes: 3 additions & 3 deletions test/integration/single/lastmod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ describe('lastmod', () => {
</url>
<url>
<loc>https://nuxtseo.com/baz</loc>
<lastmod>2023-12-21T02:49:27+00:00</lastmod>
<lastmod>2023-12-21T13:49:27Z</lastmod>
</url>
<url>
<loc>https://nuxtseo.com/crawled</loc>
</url>
<url>
<loc>https://nuxtseo.com/foo</loc>
<lastmod>2023-12-21T02:49:27+00:00</lastmod>
<lastmod>2023-12-21T13:49:27Z</lastmod>
</url>
<url>
<loc>https://nuxtseo.com/quux</loc>
</url>
<url>
<loc>https://nuxtseo.com/qux</loc>
<lastmod>2023-12-21T02:49:27+00:00</lastmod>
<lastmod>2023-12-21T13:49:27Z</lastmod>
</url>
<url>
<loc>https://nuxtseo.com/issue/206</loc>
Expand Down
8 changes: 6 additions & 2 deletions test/unit/lastmod.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, it } from 'vitest'
import { isValidW3CDate } from '../../src/runtime/nitro/sitemap/urlset/normalise'
import { isValidW3CDate, normaliseDate } from '../../src/runtime/nitro/sitemap/urlset/normalise'

describe('lastmod', () => {
it('default', async () => {
it('w3c validate', () => {
expect(isValidW3CDate('2023-12-21')).toBeTruthy()
expect(isValidW3CDate('2023-12-21T22:46:58Z')).toBeTruthy()
expect(isValidW3CDate('2023-12-21T22:46:58+00:00')).toBeTruthy()
Expand All @@ -11,4 +11,8 @@ describe('lastmod', () => {
expect(isValidW3CDate('1994-11-05T08:15:30-05:00')).toBeTruthy()
expect(isValidW3CDate('1994-11-05T08:15:30-05:00')).toBeTruthy()
})
it('date create', () => {
// time without timezone
expect(normaliseDate('2023-12-21T13:49:27.963745')).toMatchInlineSnapshot(`"2023-12-21T13:49:27Z"`)
})
})

0 comments on commit 30f7440

Please sign in to comment.