Skip to content

Instantly share code, notes, and snippets.

@drmzio
Last active July 12, 2021 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmzio/579835a4cf57326377d29023232b7650 to your computer and use it in GitHub Desktop.
Save drmzio/579835a4cf57326377d29023232b7650 to your computer and use it in GitHub Desktop.
Get list of all pages using require.context
export const getPages = () => {
const ctx = require.context('./', true, /\.js$/);
return ctx
.keys()
.filter(page => !page.startsWith('./_')) // Filters out _app.js and _document.js
.map(page => {
page = page
.replace(/(\/index)?\.js$/i, '') // Removes the index.js files
.replace(/\.(\/)?/i, '/'); // Normalize the homepage directory to "/"
return page;
});
}
// Your Next.js index page
export default function IndexPage() {
const pages = getPages();
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment