Last active
August 2, 2018 12:24
-
-
Save deptno/f8a530b42bc2776f14d3524ade16aa4a to your computer and use it in GitHub Desktop.
next.js@3 static export utility
This file contains 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
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 | |
} |
This file contains 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
//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