diff --git a/helpers/get-json-directory.js b/helpers/get-json-directory.js new file mode 100644 index 0000000..f4bdbe2 --- /dev/null +++ b/helpers/get-json-directory.js @@ -0,0 +1,23 @@ +import fs from 'fs-extra' + + + +const directoriesToTry = [ + './static/', + + // Vercel Serverless Function + './.next/server/chunks/static' +] + +export async function getJsonDirectory () { + + for ( const directory of directoriesToTry ) { + const directoryExists = await fs.pathExists( directory ) + + if ( directoryExists ) { + return directory + } + } + + throw new Error( 'Could not find json directory' ) +} diff --git a/pages/formula/[...slug].js b/pages/formula/[...slug].js index 4f54a68..b7730da 100644 --- a/pages/formula/[...slug].js +++ b/pages/formula/[...slug].js @@ -1,6 +1,7 @@ import { promises as fs } from 'fs' import Layout from '@/components/layout/default.js' +import { getJsonDirectory } from '@/helpers/get-json-directory.js'; // Based on // https://github.com/lfades/static-tweet/blob/b2a1044d881cca88e7761491d4f22c8908497d92/pages/t/%5Btweet%5D.js @@ -54,6 +55,10 @@ export async function getStaticProps({ params }) { const server = await fs.readdir('./.next/server') const chunks = await fs.readdir('./.next/server/chunks') const staticDirectory = await fs.readdir('./.next/server/chunks/static') + const jsonDirectory = await getJsonDirectory() + + + console.log('jsonDirectory', jsonDirectory) // console.log('endpointListings', endpointListings[0]) @@ -63,7 +68,8 @@ export async function getStaticProps({ params }) { dotnext, server, chunks, - staticDirectory + staticDirectory, + jsonDirectory } const start = '/formula/'