Skip to content

Commit

Permalink
fix: always reformat lastmod for google
Browse files Browse the repository at this point in the history
Relates to #214
  • Loading branch information
harlan-zw committed Jan 15, 2024
1 parent 0b65cf8 commit 397d553
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
32 changes: 19 additions & 13 deletions src/runtime/nitro/sitemap/urlset/normalise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ 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') {
// use as if if it's already valid
if (isValidW3CDate(d))
return d
// correct a time component without a timezone
if (d.includes('T')) {
const t = d.split('T')[1]
Expand All @@ -123,6 +120,9 @@ export function normaliseDate(d: Date | string) {
d += 'Z'
}
}
// skip invalid w3c date
if (!isValidW3CDate(d))
return false
// otherwise we need to parse it
d = new Date(d)
d.setMilliseconds(0)
Expand All @@ -131,18 +131,24 @@ export function normaliseDate(d: Date | string) {
return false
}
const z = (n: number) => (`0${n}`).slice(-2)
return (
`${d.getUTCFullYear()
// need to normalise for google sitemap spec
const date = `${d.getUTCFullYear()
}-${
z(d.getUTCMonth() + 1)
}-${
z(d.getUTCDate())
}T${
z(d.getUTCHours())
}:${
z(d.getUTCMinutes())
}:${
z(d.getUTCSeconds())
}Z`
)
}`
// check if we have a time set
if (d.getUTCHours() > 0 || d.getUTCMinutes() > 0 || d.getUTCSeconds() > 0) {
return (
`${date}T${
z(d.getUTCHours())
}:${
z(d.getUTCMinutes())
}:${
z(d.getUTCSeconds())
}Z`
)
}
return date
}
4 changes: 2 additions & 2 deletions test/integration/content/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ describe('nuxt/content default', () => {
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://nuxtseo.com/blog/posts/bar</loc>
<lastmod>2021-10-20T00:00:00.000Z</lastmod>
<lastmod>2021-10-20</lastmod>
</url>
<url>
<loc>https://nuxtseo.com/blog/posts/fallback</loc>
<lastmod>2021-10-20T00:00:00.000Z</lastmod>
<lastmod>2021-10-20</lastmod>
</url>
</urlset>"
`)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/content/documentDriven.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ describe('nuxt/content documentDriven', () => {
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://nuxtseo.com/blog/posts/bar</loc>
<lastmod>2021-10-20T00:00:00.000Z</lastmod>
<lastmod>2021-10-20</lastmod>
</url>
<url>
<loc>https://nuxtseo.com/blog/posts/fallback</loc>
<lastmod>2021-10-20T00:00:00.000Z</lastmod>
<lastmod>2021-10-20</lastmod>
</url>
</urlset>"
`)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/single/lastmod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('lastmod', () => {
</url>
<url>
<loc>https://nuxtseo.com/issue/206</loc>
<lastmod>2023-12-21T22:46:58.441+00:00</lastmod>
<lastmod>2023-12-21T22:46:58Z</lastmod>
</url>
<url>
<loc>https://nuxtseo.com/sub/page</loc>
Expand Down

0 comments on commit 397d553

Please sign in to comment.