-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix 347 add sitemap and robots.txt (#404)
* Add sitemap.xml.ts and robots.txt files * Add tests for generating municipality sitemap data and sitemap XML string * Update robots.txt fixed typo * Move replaceLetters function to shared and use it in generateMunipacitySitemapData * Run eslint -fix
- Loading branch information
1 parent
f6694bf
commit f5c01f2
Showing
6 changed files
with
145 additions
and
25 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,33 @@ | ||
import { | ||
generateMunipacitySitemapData, | ||
generateSitemap, | ||
} from '../../utils/generateMunipacitySitemap' | ||
import { Municipality } from '../../utils/types' | ||
|
||
const municipalities = [{ Name: 'Stockholm' }, { Name: 'Göteborg' }] as Municipality[] | ||
describe('generateSitemap', () => { | ||
it('should generate valid municipality sitemap data', () => { | ||
const siteMap = generateMunipacitySitemapData({ municipalities }) | ||
expect(siteMap).toEqual([ | ||
{ | ||
url: 'https://klimatkollen.se/kommun/Stockholm', | ||
name: 'Stockholm', | ||
lastModified: expect.any(Date), | ||
changeFrequency: 'yearly', | ||
priority: 1, | ||
}, | ||
{ | ||
url: 'https://klimatkollen.se/kommun/Göteborg', | ||
name: 'Göteborg', | ||
lastModified: expect.any(Date), | ||
changeFrequency: 'yearly', | ||
priority: 1, | ||
}, | ||
]) | ||
}) | ||
it('should generate a valid sitemap XML string', () => { | ||
const siteMap = generateMunipacitySitemapData({ municipalities }) | ||
const sitemapXml = generateSitemap(siteMap) | ||
expect(() => new DOMParser().parseFromString(sitemapXml, 'text/xml')).not.toThrow() | ||
}) | ||
}) |
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,28 @@ | ||
// pages/sitemap.xml.js | ||
|
||
import { NextApiResponse } from 'next' | ||
import { ClimateDataService } from '../utils/climateDataService' | ||
import { | ||
generateMunipacitySitemapData, | ||
generateSitemap, | ||
} from '../utils/generateMunipacitySitemap' | ||
|
||
export async function getServerSideProps({ res }: { res: NextApiResponse }) { | ||
const municipalities = new ClimateDataService().getMunicipalities() | ||
const municipalitiesSitemap = generateMunipacitySitemapData({ | ||
municipalities, | ||
}) | ||
// Generate the XML sitemap with the blog data | ||
const sitemap = generateSitemap(municipalitiesSitemap) | ||
|
||
res.setHeader('Content-Type', 'text/xml') | ||
// Send the XML to the browser | ||
res.write(sitemap) | ||
res.end() | ||
|
||
return { | ||
props: {}, | ||
} | ||
} | ||
|
||
export default function SiteMap() {} |
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,3 @@ | ||
User-agent: * | ||
Disallow: | ||
Sitemap: https://klimatkollen.se/sitemap.xml |
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,59 @@ | ||
import { replaceLetters } from './shared' | ||
import { Municipality } from './types' | ||
|
||
type SiteMap = { | ||
url: string | ||
lastModified: Date | ||
changeFrequency: string | ||
priority: number | ||
name?: string | ||
} | ||
const BASE_URL = 'https://klimatkollen.se' | ||
export const generateMunipacitySitemapData = ({ | ||
municipalities, | ||
}: { | ||
municipalities: Municipality[] | ||
}): SiteMap[] => municipalities.map((m) => ({ | ||
url: `${BASE_URL}/kommun/${replaceLetters(m.Name).toLowerCase()}`, | ||
name: m.Name, | ||
lastModified: new Date(), | ||
changeFrequency: 'yearly', | ||
priority: 1, | ||
})) | ||
export const generateSitemap = ( | ||
siteMap: SiteMap[], | ||
) => `<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"> | ||
<url> | ||
<loc>${BASE_URL}/utslappen/karta</loc> | ||
<name>Utsläppskarta</name> | ||
<lastmod>${new Date().toISOString()}</lastmod> | ||
<changefreq>yearly</changefreq> | ||
</url> | ||
<url> | ||
<loc>${BASE_URL}/om-oss</loc> | ||
<name>Om oss</name> | ||
<lastmod>${new Date().toISOString()}</lastmod> | ||
<changefreq>monthly</changefreq> | ||
</url> | ||
<url> | ||
<loc>${BASE_URL}/in-english</loc> | ||
<name>In English</name> | ||
<lastmod>${new Date().toISOString()}</lastmod> | ||
<changefreq>monthly</changefreq> | ||
</url> | ||
${siteMap | ||
.map( | ||
(s) => ` | ||
<url> | ||
<loc>${s.url}</loc> | ||
<name>${s.name}</name> | ||
<lastmod>${s.lastModified.toISOString()}</lastmod> | ||
<changefreq>${s.changeFrequency}</changefreq> | ||
<priority>${s.priority}</priority> | ||
</url> | ||
`, | ||
) | ||
.join('')} | ||
</urlset> | ||
` |
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