Last active
July 12, 2021 22:04
-
-
Save drmzio/579835a4cf57326377d29023232b7650 to your computer and use it in GitHub Desktop.
Get list of all pages using require.context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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