Thanks for this method, i used another one which had to take advantage of experimental features, you could better this code using the sitemap package. eg;
importReactfrom"react";import{SitemapItem,EnumChangefreq}from"sitemap";import{SitemapStream,streamToPromise}from"sitemap";import{NextApiRequest,NextApiResponse,NextPageContext}from"next";interfaceRouteItemextendsOmit<SitemapItem,"img"|"links"|"video">{title:string;// used as string titlesitemap:boolean;// used to filter routes when construction sitemapnavigation:boolean;// used to filter routes when construction navigationimg?:SitemapItem["img"];// transform type to optionallinks?:SitemapItem["links"];// transform type to optionalvideo?:SitemapItem["video"];// transform type to optional}exportconstroutes:RouteItem[]=[{title:"Landing",sitemap:true,navigation:false,url:"/",changefreq:EnumChangefreq.YEARLY,},];interfacePageContextextendsNextPageContext{req:NextApiRequest;res:NextApiResponse;}classSitemapextendsReact.Component{staticasyncgetInitialProps({req,res}:PageContext){consthostname="https://"+req.headers.host;res.setHeader("Content-Type","text/xml");constsmStream=newSitemapStream({hostname});for(constrouteofroutes)smStream.write(route);smStream.end();constsitemap=awaitstreamToPromise(smStream).then((sm)=>sm.toString());res.write(sitemap);res.end();}}exportdefaultSitemap;
Yes, I thought as much, I believe it's rather a choice of preference, thanks for the very informative post, both methods work great and in the end, it only comes down to a fight for bytes :)
BTW, here is the method I previously used, github.com/zeit/next.js/issues/905...
as you can see it has to leverage experimental features which are never good in a production env, so thanks for your method, I will be switching all my deployments to this method
Thanks for this method, i used another one which had to take advantage of experimental features, you could better this code using the sitemap package. eg;
@ook0 I agree with mark in using the sitemap package because it is typesafe and also has this possibility npmjs.com/package/sitemap#create-s...
My goal was to use as less packages as needed, there for didn`t I use sitemap package. But I think sitemap package is a better solution
Yes, I thought as much, I believe it's rather a choice of preference, thanks for the very informative post, both methods work great and in the end, it only comes down to a fight for bytes :)
BTW, here is the method I previously used, github.com/zeit/next.js/issues/905...
as you can see it has to leverage experimental features which are never good in a production env, so thanks for your method, I will be switching all my deployments to this method
and if you would like to go a step further, I included robots using the same method.