Skip to content

Instantly share code, notes, and snippets.

@deptno
Last active August 2, 2018 12:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deptno/f8a530b42bc2776f14d3524ade16aa4a to your computer and use it in GitHub Desktop.
Save deptno/f8a530b42bc2776f14d3524ade16aa4a to your computer and use it in GitHub Desktop.
next.js@3 static export utility
const root = `${__dirname}/pages`
const path = require('path')
const tree = require('directory-tree')
module.exports.exportPathMap = () => files(tree(root).children)
.reduce((ret, file) => {
ret[file] = {page: file}
return ret
}, {'/': {page: '/index'}})
function files(tree, result = []) {
result
.push(...tree
.map(child => {
if (child.children) {
result.push(...files(child.children))
return
}
return child
})
.filter(child => child && child.extension && child.extension.endsWith('.tsx'))
.map(tsx => {
if (tsx.size === 0) {
console.warn(`${tsx.path} has zero size`)
}
return tsx.path
.replace(root, '')
.replace(tsx.name, `${path.parse(tsx.name).name}`)
})
.filter(name => !(name.startsWith('/_') || name.startsWith('/test'))))
return result
}
//use this where component in component has `getInitialProps`
export const isomorphicQuery = query =>
Object.keys(query).length === 0 && typeof window !== 'undefined'
? qs.parse(location.search.slice(1))
: query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment