I have a NextJS app and am using next-routes to handle all routing.
My routing module currently looks like this:
const routes = require('next-routes')();
const { getEntries } = require('../data/contentful');
module.exports = async () => {
const globalSettings = await getEntries({
content_type: 'globalSettings',
});
routes
.add('caseStudies', `/${globalSettings.fields.caseStudiesSlug}`, 'caseStudies')
.add('caseStudy', `/${globalSettings.fields.caseStudiesSlug]}/:slug`, 'caseStudy')
.add('home', `/`, 'index')
.add('page', `/:slug*`, 'page'));
return routes;
};
I can get this working for server side, but to use next-routes on client side, I need this module to immediately return the routes object rather than an async function. e.g.
const routes = require('next-routes')();
const { getEntries } = require('../data/contentful');
// Do this first, then module.exports
const globalSettings = await getEntries({
content_type: 'globalSettings',
});
module.exports = routes
.add('caseStudies', `/${globalSettings.fields.caseStudiesSlug}`, 'caseStudies')
.add('caseStudy', `/${globalSettings.fields.caseStudiesSlug]}/:slug`, 'caseStudy')
.add('home', `/`, 'index')
.add('page', `/:slug*`, 'page'));
This doesn't work because await
must be inside an async function. How can I complete my async API call before doing my module.exports of the routes object?