Closed Cappuuuu closed 2 years ago
why don't you use dotenv? you can set a variable to be your origin.
modules: [
...
['@nuxtjs/robots', {
UserAgent: '*',
Sitemap: (req) => `https://${req.headers.host}/sitemap.xml`
}],
...
]
modules: [ ... ['@nuxtjs/robots', { UserAgent: '*', Sitemap: (req) => `https://${req.headers.host}/sitemap.xml` }], ... ]
Did the trick for me! Would be nice to add this up in the README.md since now it is kinda a hidden feature
modules: [ ... ['@nuxtjs/robots', { UserAgent: '*', Sitemap: (req) => `https://${req.headers.host}/sitemap.xml` }], ... ]
Be aware that this would not work on target: 'static'
mode
@IsraelOrtuno could you please explain why it is not working for static mode?
@mklueh Static generated sites won't be running any logic in the server side (there's also no server at all) and in this case, the sitemap URL is being generated by a function which will also require access the request headers. For that case it's better to use a ENV variable as @Tymo93 mentioned.
@IsraelOrtuno I see, but isn't there just a hook that gives you all the pages generated after the build is completed? Because I guess Nuxt knows what pages were touched anyway
Not sure about the hook but robots.txt serves for more purposes than adding the sitemap URL (which will likely be generated by your own app or a third party module). The point here is that if you pretend to have a dynamic sitemap URL (or any other property in your robots file), it has to run on SSR as that function needs to be executed, otherwise you can make it static with an ENV var.
in front-end, I can get window.location.origin like this screenshot
but i want set dynamic sitemap in nuxt.config.js in robots.txt ex:
robots: { UserAgent: '*', Sitemap: ${window.location.origin}/sitemap.xml }
, here window object is not exist , Is there any good way to resolve it ? Thanks !