Skip to content

Commit 2ced40f

Browse files
Hdd87d0rich
andauthored
add sitemap to repository (#8)
* add sitemap to repository * fixed lint * changes as per comments * access hostname in appconfig in sitemxl.ts * lint fix * fixed dependencies sitemap * update hostname * lintfix * fix lint by going to null option * baseUrl has protocol and website name only * added '/docs-toolkit' to url writen onto sitemap * add prefix to nuxt schema and run prepare * define prefix in app.config.ts * use prefix via appconfig into url in sitemap * filter out terms: used suggestion from review * lint fix * Update nuxt.schema.ts add JSDoc to show type of prefix is string Co-authored-by: Nikolai Dorofeev <[email protected]> * Update server/routes/sitemap.xml.ts filter out draft pages Co-authored-by: Nikolai Dorofeev <[email protected]> * install UFO * replace + with joinURL() * update JSDoc for sitemap.baseUrl * lint fix * Apply suggestions from code review Co-authored-by: Nikolai Dorofeev <[email protected]> * removed + * type check * lint fix * pre render sitemap.xml * lint fix * Update server/routes/sitemap.xml.ts removed unneeded comment Co-authored-by: Nikolai Dorofeev <[email protected]> * removed prefix, updated sitemap --------- Co-authored-by: Nikolai Dorofeev <[email protected]>
1 parent e8d7234 commit 2ced40f

File tree

5 files changed

+126
-8
lines changed

5 files changed

+126
-8
lines changed

nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default defineNuxtConfig({
3333
modules: ['@nuxt/content', '@nuxtjs/tailwindcss', 'nuxt-icon'],
3434
nitro: {
3535
prerender: {
36-
routes: ['/robots.txt']
36+
routes: ['/robots.txt', '/sitemap.xml']
3737
}
3838
},
3939
content: {

nuxt.schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export default defineNuxtSchema({
101101
/**
102102
* Base URL for sitemap. All links in sitemap will be relative to this URL.
103103
*
104-
* @example 'https://exactpro.github.io/docs-toolkit'
104+
* @example 'https://exactpro.github.io'
105105
* @type {?string}
106106
*/
107107
baseUrl: undefined

package-lock.json

Lines changed: 75 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"nuxt": "^3.3.1",
6161
"nuxt-icon": "^0.3.3",
6262
"rehype-external-links": "^2.1.0",
63+
"sitemap": "^7.1.1",
6364
"vue-gtag-next": "^1.14.0"
6465
}
6566
}

server/routes/sitemap.xml.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2023 Exactpro (Exactpro Systems Limited)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { SitemapStream, streamToPromise } from 'sitemap'
18+
import { joinURL, parseURL, withProtocol } from 'ufo'
19+
import { serverQueryContent } from '#content/server'
20+
const appConfig = useAppConfig()
21+
22+
const path = parseURL(appConfig.exactproDocs.seo?.sitemap?.baseUrl ?? '')
23+
24+
export default defineEventHandler(async (event) => {
25+
// Fetch all documents
26+
const docs = await serverQueryContent(event)
27+
.where({ _partial: false, _draft: false })
28+
.find()
29+
if (!path.host || !path.protocol) {
30+
throw new Error('host or protocol not found')
31+
}
32+
const sitemap = new SitemapStream({
33+
hostname: withProtocol(path.host, path.protocol)
34+
})
35+
36+
for (const doc of docs) {
37+
if (!doc._path) {
38+
continue
39+
}
40+
sitemap.write({
41+
url: joinURL(path.pathname, doc._path),
42+
changefreq: 'monthly'
43+
})
44+
}
45+
sitemap.end()
46+
47+
return streamToPromise(sitemap)
48+
})

0 commit comments

Comments
 (0)