1

I would like to get data from a CMS and store it globally using Jotai. But I keep getting

Uncaught TypeError: Expected parameter accessToken

When I try to use environmental variables.

At the same time, I don't have this error when I use my token directly. I am also fine using the env variables outside Jotai

My code is

import { createClient } from "contentful";

const client = createClient({
   space: process.env.CONTENTFUL_SPACE_ID!,
   accessToken: process.env.CONTENTFUL_ACCESS_KEY!,
})

export const asyncAtom = atom(async () => {
   const res = await client.getEntries<ILocations>({
      content_type: "locations"
   })

   return res.items
})

export const loadableAtom = loadable(asyncAtom)

While this works fine

const client = createClient({
   space: 'blahblah',
   accessToken: 'blahblahblah',
})
Ken White
  • 123,280
  • 14
  • 225
  • 444
Danny Lenko
  • 45
  • 1
  • 8
  • 2
    Not familiar with Jotai, but in regards to next where is that `loadableAtom` being referenced? If this code is at all referenced by client-side code then those env vars will not be accessible at runtime. Sensitive data like that should be encapsulated in either next api endpoints, getServerSideProps, or getStaticProps. Those three locations can access the env directly. Whereas client-side env vars must be prefixed in your .env file with `NEXT_PUBLIC_`. But such vars should only be used if the value is not sensitive. – Seth Jan 19 '23 at 05:17
  • @Seth, thank you for the explanation. Jotai uses hooks to get globally stored data. So seems like getStaticProps is not my choice. But, yeah, the prefix works. And it is time to test the api now – Danny Lenko Jan 19 '23 at 05:30

0 Answers0