-
Notifications
You must be signed in to change notification settings - Fork 26.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serve sitemap.xml and robots.txt from "/" #226
Comments
/
/
Personally I stick a proper web server in front of my node apps. In the Next app I'm currently developing I stick Nginx in front with the following bit of config in my vhost: root /path/to/my/next/apps/static/directory;
try_files $uri @dynamic;
location @dynamic {
proxy_pass http://localhost:3000;
proxy_buffering off;
proxy_set_header Host $http_host;
} That way when you request "/robots.txt" or any other URL, nginx first looks to see if there is a matching file in the "static" directory, and if it's not there, it proxies the request through to my local Next app. That being said, I don't know why Next requires you to have "/static/" at the beginning of the URL for any static content. It seems unnecessary. Any production system will have a proper web server at the front serving up the static content anyway. |
Although that method seems straightforward @mikecardwell, when pairing next with now, we do not have that luxury. In the interest of staying inside the Zeit ecosystem and using zero-config tools, I'd love for this functionality to be possible when using these two tools together. |
I agree. However, I don't think it should be specific to robots.txt / sitemap.xml, I think it should be for all content. Requests for "/foo" should look for content in the static directory first, and only then fall back to the dynamic route if it doesn't exist. |
That's an excellent point @mikecardwell, that solution seems the most robust given thus-far. How about some chime-in from the Zeit folks? |
As @mikecardwell said, I also wonder if we really need |
For now I'd like to preserve The dynamic mapping solution I really don't like. I think the solution to this problem is #25. You would do something like this to match your example: if ('/robots.txt' === req.url) {
return sendFile(res, './static/robots.txt')
} |
I recently ran into this same problem trying to add a root-scoped service worker script to my next.js-based site/app. Until #25 is implemented in some way, I'm using this hacky workaround:
import React from 'react'
export default class extends React.Component {
static async getInitialProps ({ res }) {
if (!res) return {}
res.setHeader('Content-Type', 'application/javascript')
res.end(`// custom service worker
// PUT YOUR SERVICE WORKER JS LOGIC HERE
`, 'utf8')
return {}
}
render () {
return null
}
} And then I register the service worker in componentDidMount () {
if (navigator && navigator.serviceWorker) {
navigator.serviceWorker.register('/sw.js')
}
} Note that my experience with and understanding of next.js and React is very superficial, so there may be a better way of doing this (and I'm sure this violates some best practices), but at least it works. Theoretically this would work for |
@nextdrew sorry about having had to use a nasty workaround. #25 is coming very soon :) |
🤔 |
Maybe I got this wrong, but the |
You can already read in the issue it was superseded by another proposal. |
This worked for me using express https://github.com/ekalinin/sitemap.js#example-of-using-sitemapjs-with-express It also supports other servers and there may be a way to create an automatically generated sitemap based on defined routes. |
Hey! I'm building my new static site using Next.js and stumbled upon this as well. In this post I explain how I automatically generate the sitemap after I Hope this helps anybody stumbling upon this as well! |
The Issue
Known as an important aspect of having great SEO, a
sitemap.xml
in/
should be possible with next but currently is not.Known as an important aspect of privacy and security, a
robots.txt
in/
should be possible with next but currently is not.Proposal
These two files could be mapped to
/
if they exist in/static
. I notice that the zeit website does not have a robots.txt nor a sitemap.xml so I assume this issue simply has not presented itself yet. Let us know what you folks think about the feature.The text was updated successfully, but these errors were encountered: