OUTDATED! NEW WAY: https://emilpriver.com/blog/sitemap-with-nextjs-after-9-4-update
Originally written at: My website
So, Nextjs is rea...
For further actions, you may consider blocking this person and/or reporting abuse
Thanks for this Emil, very useful.
I'm just updating my app to take advantage of the new next getStaticProps feature, so I can render my sitemap.xml as a static page at build time, rather than generating it on the server every time.
Do you have any thoughts about how to achieve this ? the res object is not available in getStaticProps, so I can't find a way to serve the xml directly to the client as you do above without rendering it in a page component at which point it ceases to be an xml and appears as a string. Any suggestions?
For sitemap I dunno. But for the
rss.xml
I just added this inside thegetStaticProps
of my/blog
page:I dunno if is a good way, but it works 🤷♂️
Maybe with sitemap is it possible to do something similar?
Thanks, I hadn't thought of that. I'll give it a try.
Any luck with this? I am currently working through the same issue.
I got it working locally fine but not yet pushed to server (I'm using vercel now). Not sure if that allows fs.writes.
Will report back when I've pushed to the server in the next few days.
I use fs.writes but I added on webpack finally...
Thanks!
I tried your solution but do you know how to implement it when you are using Typescript? i need to import existing logic to get data that is written in TS
The
next.config.js
file hasjs
extension. So looks difficult to use TypeScript here. One thing you can do is compile next.config.ts to next.config.js: github.com/vercel/next.js/issues/5...Hi ! great article thank you Emil. I implemented this post my next project it is working dev server but it fails in production. Why it is ? saalla.com/sitemap.xml . this my site. i dont see any error info at console. it just catch 500
Hard for me to help you with a link. I recommend to check logs at Vercel :)
Have you ever encountered an error like this before? maybe it is related to next.config.js
No I have not :/ I recommend to try other solutions as Next have new functions now, I am talking about getServerSidedProps for exempel ☺️
thank you for replies. i ll look them :)
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.
Hey Emil,
Thanks for the article it's really helpful. the link to your blog has 404.
Thank you :D I've changed the url
Nice, I will combine this approach with the npm sitemap package npmjs.com/package/sitemap to generate the XML
Hello Mark, why you need "sitemap" package?
Thanks for this Emil, it is clearly. But i got an error in production like this.
[GET] /sitemap.xml
21:12:35:81
2020-07-17T18:12:35.996Z 022e5e7d-a417-40ba-84c0-66ba45030366 ERROR TypeError: Cannot assign to read only property 'undefined' of object '#'
at getServerSideProps (/var/task/.next/serverless/pages/sitemap.xml.js:10268:13)
at renderToHTML (/var/task/node_modules/next/dist/next-server/server/render.js:37:200)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async renderReqToHTML (/var/task/.next/serverless/pages/sitemap.xml.js:4554:22)
at async render (/var/task/.next/serverless/pages/sitemap.xml.js:4601:22)
it tried it getinitialprops and getserversideprops . i shared screenshot
OMG.... finally a tutorial simple and objective! Thank you very much!
I'm happy it helps :D
I am very interested in your job.
I am a Next JS expert.
I want to work with you. Thanks