Skip to content
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

Closed
dawsbot opened this issue Nov 8, 2016 · 15 comments
Closed

Serve sitemap.xml and robots.txt from "/" #226

dawsbot opened this issue Nov 8, 2016 · 15 comments

Comments

@dawsbot
Copy link

dawsbot commented Nov 8, 2016

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.

@dawsbot dawsbot changed the title Serve sitemap.xml and robots.txt Serve sitemap.xml and robots.txt from / Nov 8, 2016
@dawsbot dawsbot changed the title Serve sitemap.xml and robots.txt from / Serve sitemap.xml and robots.txt from "/" Nov 8, 2016
@mikecardwell
Copy link

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.

@dawsbot
Copy link
Author

dawsbot commented Nov 11, 2016

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.

@mikecardwell
Copy link

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.

@dawsbot
Copy link
Author

dawsbot commented Nov 11, 2016

That's an excellent point @mikecardwell, that solution seems the most robust given thus-far. How about some chime-in from the Zeit folks?

@nkzawa
Copy link
Contributor

nkzawa commented Nov 14, 2016

As @mikecardwell said, I also wonder if we really need /static.
Falling back to dynamic routes sounds nice but confusing 🤔
Any thoughts ? @rauchg

@rauchg
Copy link
Member

rauchg commented Nov 14, 2016

For now I'd like to preserve /static. It's really a huge time saver in some situations with very little added code or surface for problems for us.

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')
}

@rauchg rauchg closed this as completed Nov 14, 2016
@nexdrew
Copy link

nexdrew commented Nov 17, 2016

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:

pages/sw.js

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() (which only runs on the client) of my index/home page component, like so:

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 /robots.txt and /sitemap.xml too (via pages/robots.txt.js and pages/sitemap.xml.js respectively).

@rauchg
Copy link
Member

rauchg commented Nov 17, 2016

@nextdrew sorry about having had to use a nasty workaround. #25 is coming very soon :)

@rogvold
Copy link

rogvold commented Feb 12, 2018

That helped me https://github.com/zeit/next.js/blob/canary/examples/root-static-files/server.js

@goloroden
Copy link

@rauchg In November 2016 you said

" #25 is coming very soon :)"

AFAICS it's not there yet. Any timeframe for this?

@timneutkens
Copy link
Member

AFAICS it's not there yet. Any timeframe for this?

🤔

https://github.com/zeit/next.js#custom-server-and-routing

@goloroden
Copy link

Maybe I got this wrong, but the -s flag does not exist, does it?

@timneutkens
Copy link
Member

You can already read in the issue it was superseded by another proposal.

@ThatGuySam
Copy link

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.

@embiem
Copy link
Contributor

embiem commented Oct 19, 2018

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 yarn build && yarn export my site. Basically, I look at the files under pages/ and create a very basic sitemap.

Hope this helps anybody stumbling upon this as well!

@lock lock bot locked as resolved and limited conversation to collaborators Oct 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants