-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
293 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { XMLBuilder } from "fast-xml-parser"; | ||
import { | ||
generateSitemapUrl, | ||
getSitemapData, | ||
getSiteMapNewsData, | ||
} from "@/lib/api/sitemap"; | ||
|
||
export async function GET( | ||
request: NextRequest, | ||
{ params: { locale } }: LocaleProps | ||
) { | ||
const pages = await getSitemapData(locale); | ||
const pageData = pages.map(({ uri, dateUpdated }) => { | ||
return { | ||
loc: generateSitemapUrl(uri, locale), | ||
lastmod: dateUpdated, | ||
}; | ||
}); | ||
const { siteTitle, news } = await getSiteMapNewsData(locale); | ||
|
||
const today = new Date(); | ||
const recentNewsThreshold = new Date( | ||
today.getTime() - 1000 * 60 * 60 * 24 * 2 | ||
); | ||
|
||
const newsData = news.map(({ uri, dateUpdated, title, date }) => { | ||
const entry = { | ||
loc: generateSitemapUrl(uri, locale), | ||
lastmod: dateUpdated, | ||
}; | ||
|
||
if (new Date(date) > recentNewsThreshold) { | ||
entry["news:news"] = { | ||
"news:publication": { | ||
"news:name": siteTitle, | ||
"news:language": locale, | ||
}, | ||
"news:publication_date": date, | ||
"news:title": title, | ||
}; | ||
} | ||
|
||
return entry; | ||
}); | ||
|
||
const data = { | ||
"?xml": { | ||
$version: "1.0", | ||
$encoding: "UTF-8", | ||
}, | ||
urlset: { | ||
$xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9", | ||
"$xmlns:news": "http://www.google.com/schemas/sitemap-news/0.9", | ||
url: pageData.concat(newsData), | ||
}, | ||
}; | ||
const builder = new XMLBuilder({ | ||
attributeNamePrefix: "$", | ||
arrayNodeName: "url", | ||
ignoreAttributes: false, | ||
}); | ||
const output = builder.build(data); | ||
|
||
return new NextResponse(output, { | ||
status: 200, | ||
headers: { "Content-Type": "application/xml; charset=utf-8" }, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { NextResponse } from "next/server"; | ||
import { XMLBuilder } from "fast-xml-parser"; | ||
import { generateSitemapUrl } from "@/lib/api/sitemap"; | ||
import { languages } from "@/lib/i18n/settings"; | ||
|
||
export async function GET() { | ||
const data = { | ||
"?xml": { | ||
$version: "1.0", | ||
$encoding: "UTF-8", | ||
}, | ||
sitemapindex: { | ||
$xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9", | ||
sitemap: languages.map((language) => { | ||
return { loc: generateSitemapUrl("sitemap.xml", language, true) }; | ||
}), | ||
}, | ||
}; | ||
const builder = new XMLBuilder({ | ||
attributeNamePrefix: "$", | ||
arrayNodeName: "sitemap", | ||
ignoreAttributes: false, | ||
}); | ||
const output = builder.build(data); | ||
|
||
return new NextResponse(output, { | ||
status: 200, | ||
headers: { "Content-Type": "application/xml; charset=utf-8" }, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { XMLValidator, XMLParser } from "fast-xml-parser"; | ||
import { languages } from "../../../lib/i18n/settings"; | ||
|
||
const url = "/sitemap_index.xml"; | ||
|
||
context("GET /server_index.xml", () => { | ||
it("returns a well formed XML file", () => { | ||
cy.request({ | ||
url, | ||
method: "GET", | ||
}).then(({ body, headers }) => { | ||
expect(headers["content-type"]).to.eq("application/xml; charset=utf-8"); | ||
|
||
const valid = XMLValidator.validate(body); | ||
|
||
expect(valid).to.eq(true); | ||
}); | ||
}); | ||
it("contains a sitemap for each locale", () => { | ||
cy.request({ | ||
url, | ||
method: "GET", | ||
}).then(({ body }) => { | ||
const parser = new XMLParser(); | ||
const { | ||
sitemapindex: { sitemap }, | ||
} = parser.parse(body); | ||
|
||
expect(sitemap.length).to.eq(languages.length); | ||
}); | ||
}); | ||
it("contains valid sitemap links", () => { | ||
cy.request({ | ||
url, | ||
method: "GET", | ||
}).then(({ body }) => { | ||
const parser = new XMLParser(); | ||
const { | ||
sitemapindex: { sitemap }, | ||
} = parser.parse(body); | ||
|
||
sitemap.forEach(({ loc }) => { | ||
cy.request({ url: loc, method: "GET" }).then(({ body, headers }) => { | ||
expect(headers["content-type"]).to.eq( | ||
"application/xml; charset=utf-8" | ||
); | ||
|
||
const valid = XMLValidator.validate(body); | ||
|
||
expect(valid).to.eq(true); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { gql } from "@urql/core"; | ||
import { getSiteFromLocale } from "@/lib/helpers/site"; | ||
import queryAPI from "@/lib/api/client/query"; | ||
import tags from "@/lib/api/client/tags"; | ||
import { CRAFT_HOMEPAGE_URI } from "@/lib/constants"; | ||
import { fallbackLng } from "@/lib/i18n/settings"; | ||
|
||
interface PageMetadata { | ||
uri: string; | ||
dateUpdated: string; | ||
} | ||
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || ""; | ||
|
||
export const generateSitemapUrl = ( | ||
uri: string, | ||
locale: string, | ||
preserveLocale = false | ||
) => { | ||
const segments = uri === CRAFT_HOMEPAGE_URI ? [] : [uri]; | ||
|
||
if (preserveLocale || locale !== fallbackLng) { | ||
segments.unshift(locale); | ||
} | ||
|
||
segments.unshift(baseUrl); | ||
|
||
return segments.join("/"); | ||
}; | ||
|
||
export const getSiteMapNewsData = async (locale: string) => { | ||
const site = getSiteFromLocale(locale); | ||
|
||
const query = gql` | ||
query NewsSitemapData($site: [String]) { | ||
newsEntries(site: $site) { | ||
... on news_post_Entry { | ||
title | ||
uri | ||
date | ||
dateUpdated | ||
} | ||
} | ||
globalSets(site: $site) { | ||
... on siteInfo_GlobalSet { | ||
siteTitle | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const { data } = await queryAPI({ | ||
query, | ||
variables: { site }, | ||
fetchOptions: { | ||
next: { tags: [tags.globals] }, | ||
}, | ||
}); | ||
|
||
const { newsEntries, globalSets } = data; | ||
const { siteTitle } = globalSets.filter((set) => | ||
Object.hasOwn(set, "siteTitle") | ||
)[0]; | ||
|
||
return { siteTitle, news: newsEntries }; | ||
}; | ||
|
||
export const getSitemapData = async ( | ||
locale: string | ||
): Promise<Array<PageMetadata>> => { | ||
const site = getSiteFromLocale(locale); | ||
|
||
const query = gql` | ||
query SitemapData($site: [String]) { | ||
pages: entries( | ||
site: $site | ||
section: ["pages"] | ||
type: ["not", "redirectPage"] | ||
) { | ||
uri | ||
dateUpdated | ||
} | ||
homepage: homepageEntries(site: $site) { | ||
... on homepage_homepage_Entry { | ||
uri | ||
dateUpdated | ||
} | ||
} | ||
staff: staffProfilesEntries(site: $site) { | ||
... on staffProfiles_staffProfiles_Entry { | ||
dateUpdated | ||
uri | ||
} | ||
} | ||
slideshows: slideshowsEntries(site: $site) { | ||
... on slideshows_slideshow_Entry { | ||
dateUpdated | ||
uri | ||
} | ||
} | ||
glossary: glossaryTermsEntries(site: $site) { | ||
... on glossaryTerms_glossaryTerm_Entry { | ||
dateUpdated | ||
uri | ||
} | ||
} | ||
voice: slideshowsEntries(site: $site) { | ||
... on slideshows_slideshow_Entry { | ||
dateUpdated | ||
uri | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const { data } = await queryAPI({ | ||
query, | ||
variables: { site }, | ||
fetchOptions: { | ||
next: { tags: [tags.globals] }, | ||
}, | ||
}); | ||
|
||
return Object.values<any>(data).flat(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const CRAFT_HOMEPAGE_URI = "__home__"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters