27
loading...
This website collects cookies to deliver better user experience
import { GetServerSideProps } from 'next';
export const getServerSideProps: GetServerSideProps = async ({ res }) => {};
// Default export to prevent next.js errors
const SitemapXML: React.FC = () => {
return null;
};
export default SitemapXML;
We can't use all the next.js data fetching goodness here like getStaticProps. Since we need to manipulate the server response the only function that let's us do that is getServerSideProps. Any other next.js data fetch function would not work here.
export const getServerSideProps: GetServerSideProps = async ({ res }) => {
const data = await getAllData();
};
export const getServerSideProps: GetServerSideProps = async ({ res }) => {
const data = await getAllData();
const transformedData = data.reduce((filtered, page) => {
// exclude documents that should not be in the sitemap e.g. noindex etc.
const isExcluded = excludeDocument(page);
if (isExcluded) return filtered;
filtered.push({
loc: page.url,
lastmod: page.last_publication_date || undefined,
priority: 0.7,
changefreq: 'daily',
});
return filtered;
}, []);
};
buildSitemapXml = (fields): string => {
const content = fields
.map((fieldData) => {
const field = Object.entries(fieldData).map(
([key, value]) => {
if (!value) return '';
return `<${key}>${value}</${key}>`;
},
);
return `<url>${field.join('')}</url>\n`;
})
.join('');
return this.withXMLTemplate(content);
};
withXMLTemplate = (content: string): string => {
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">\n${content}</urlset>`;
};
export const getServerSideProps: GetServerSideProps = async ({ res }) => {
const data = await getAllData();
const transformedData = data.reduce((filtered, page) => {
// exclude documents that should not be in the sitemap e.g. noindex etc.
const isExcluded = excludeDocument(page);
if (isExcluded) return filtered;
filtered.push({
loc: page.url,
lastmod: page.last_publication_date || undefined,
priority: 0.7,
changefreq: 'daily',
});
return filtered;
}, []);
const sitemapContent = buildSitemapXml(transformedData);
};
export const getServerSideProps: GetServerSideProps = async ({ res }) => {
const data = await getAllData();
const transformedData = data.reduce((filtered, page) => {
// exclude documents that should not be in the sitemap e.g. noindex etc.
const isExcluded = excludeDocument(page);
if (isExcluded) return filtered;
filtered.push({
loc: page.url,
lastmod: page.last_publication_date || undefined,
priority: 0.7,
changefreq: 'daily',
});
return filtered;
}, []);
const sitemapContent = buildSitemapXml(transformedData);
res.setHeader('Content-Type', 'text/xml');
res.write(sitemapContent);
res.end();
// Empty since we don't render anything
return {
props: {},
};
};
const sitemapContent = buildSitemapXml(transformedData);
/** Set Cache Control in vercel @see https://vercel.com/docs/edge-network/caching#stale-while-revalidate */
res.setHeader('Cache-Control', 's-maxage=30, stale-while-revalidate');
res.setHeader('Content-Type', 'text/xml');
res.write(sitemapContent);
res.end();
// Empty since we don't render anything
return {
props: {},
};
import { GetServerSideProps } from 'next';
import Sitemap from '../../util/Sitemap';
export const getServerSideProps: GetServerSideProps = async ({ res }) => {
const data = await getAllData();
const transformedData = data.reduce((filtered, page) => {
// exclude documents that should not be in the sitemap e.g. noindex etc.
const isExcluded = excludeDocument(page);
if (isExcluded) return filtered;
filtered.push({
loc: page.url,
lastmod: page.last_publication_date || undefined,
priority: 0.7,
changefreq: 'daily',
});
return filtered;
}, []);
const sitemapContent = buildSitemapXml(transformedData);
/** Set Cache Control in vercel @see https://vercel.com/docs/edge-network/caching#stale-while-revalidate */
res.setHeader('Cache-Control', 's-maxage=30, stale-while-revalidate');
res.setHeader('Content-Type', 'text/xml');
res.write(sitemapContent);
res.end();
// Empty since we don't render anything
return {
props: {},
};
};
// Default export to prevent next.js errors
const SitemapXML: React.FC = () => {
return null;
};
export default SitemapXML;