Next.js: Any way to get list of available pages / routes?

Created on 29 Nov 2017  路  3Comments  路  Source: vercel/next.js

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.
pages/
  components/
    component.js
  index.js
  catalog.js
  about.js
const listOfPages = await getListOfPages()
console.log(listOfPages)
// [ 'index', 'catalog', 'about' ]

Is it possible to implement such a thing?

Most helpful comment

@negamaxi you can use https://github.com/zeit/next.js/blob/canary/server/build/webpack.js#L53

const pages = await glob('pages/**/*.js', { cwd: __dirname })

All 3 comments

afaik there's no out-of-the-box way with next. if your structure is that simple, you could just list the directory with fs

we have a ton of dynamically generated pages, so we generate a sitemap.xml w/ next-routes, express-sitemap, and some code that fetches slugs from our db.

we then use sitemapper to turn the xml into a list of urls for tests (though i guess we could just do this while we built the sitemap).

if you don't have a sitemap (which i'm guessing you don't), there's a tool popular among SEOs called screaming frog that'll crawl your site & build an XML sitemap. free up to 500 pages & fairly reliable / credible

@negamaxi you can use https://github.com/zeit/next.js/blob/canary/server/build/webpack.js#L53

const pages = await glob('pages/**/*.js', { cwd: __dirname })

@brandonmp feel free to contribute an example for this. I'm pretty sure there's other people wanting that feature 馃憣

Was this page helpful?
0 / 5 - 0 ratings