ricokahler / next-plugin-preval

Pre-evaluate async functions during builds and import them like JSON
MIT License
257 stars 13 forks source link

Issue with async/await - Returns Promise with resolved data but still a Promise #25

Closed Vadorequest closed 3 years ago

Vadorequest commented 3 years ago

Here's my pre-fetcher:

import { getAirtableSchema } from '@/modules/core/airtable/airtableSchema';
import consolidateSanitizedAirtableDataset from '@/modules/core/airtable/consolidateSanitizedAirtableDataset';
import fetchAndSanitizeAirtableDatasets from '@/modules/core/airtable/fetchAndSanitizeAirtableDatasets';
import { AirtableSchema } from '@/modules/core/airtable/types/AirtableSchema';
import { AirtableDatasets } from '@/modules/core/data/types/AirtableDatasets';
import { SanitizedAirtableDataset } from '@/modules/core/data/types/SanitizedAirtableDataset';
import { supportedLocales } from '@/modules/core/i18n/i18nConfig';
import { I18nLocale } from '@/modules/core/i18n/types/I18nLocale';
import uniq from 'lodash.uniq';
import preval from 'next-plugin-preval';

const fetchAirtableDataset = async (): Promise<SanitizedAirtableDataset> => {
  const airtableSchema: AirtableSchema = getAirtableSchema();
  const supportedLanguages = uniq<string>(supportedLocales.map((supportedLocale: I18nLocale) => supportedLocale.lang));
  const datasets: AirtableDatasets = await fetchAndSanitizeAirtableDatasets(airtableSchema, supportedLanguages);

  return consolidateSanitizedAirtableDataset(airtableSchema, datasets.sanitized);
};

export default preval(fetchAirtableDataset());

Here's how I had to write my code to make it work in getStaticProps:

  const dataset: SanitizedAirtableDataset = await (await import('@/modules/core/preval/fetchAirtableDataset')).default;

Here is what we're supposed to write (this doesn't work):

  const dataset: SanitizedAirtableDataset = await import('@/modules/core/preval/fetchAirtableDataset');
Vadorequest commented 3 years ago

This was somehow related to an issue in my own code. My IDE had renamed the preval.ts file automatically and removed the preval part, which broke the plugin. I'll make a few more tests but it seems that's it.