-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: sitemaps #576
Merged
feat: sitemaps #576
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this static endpoint generates a sitemap index for each locale |
||
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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sitemap data is tagged as |
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parse news posts separately, if they are less than 2 days old then Google News markup is added to indicate new news release