treefarmstudio / astro-sanity-minimal-starter

https://astro-sanity-minimal-starter.netlify.app/
MIT License
104 stars 14 forks source link

Are the .env Variables Being Used? #19

Open roblevintennis opened 1 year ago

roblevintennis commented 1 year ago

We have a .env.template which when renamed should supply env: https://github.com/littlesticks/astro-sanity-minimal-starter/blob/d57397da42bdbaa73b56aac5d42fdac757bdeab5/astro/.env.template

However, I noticed even after setting up PUBLIC_SANITY_PROJECT_ID and others those aren't actually being utilized and I had to supply my sanity project ID for the Astro integration: https://github.com/littlesticks/astro-sanity-minimal-starter/blob/38b562ca6f8c36b06d046cfacbb44cfb24259b80/astro/astro.config.mjs#L13

As well as in the sanity configuration: https://github.com/littlesticks/astro-sanity-minimal-starter/blob/5d99ae3181c9594950e06d906fd01e5b27f2a6dc/sanity/sanity.json#L7

So it appears the .env stuff is not being utilized at all. This is very confusing especially as a starter. I haven't dug in but it would seem we just need to leverage dotenv which is already a dependency just not utilized. Lmk if you'd like me to try to supply a PR for this.

roblevintennis commented 1 year ago

Fwiw, on my local diverged project (but started with the template) I've definitely had success for the astro.config.mjs with:

import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';

import sanity from 'astro-sanity';
// see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
import * as dotenv from 'dotenv';
dotenv.config();

// https://astro.build/config
export default defineConfig({
  site: 'https://astro-sanity-minimal-starter.netlify.app/',
  // Your public domain, e.g.: https://my-site.dev/
  integrations: [
    sitemap(),
    sanity({
      projectId: process.env.PUBLIC_SANITY_PROJECT_ID,
      dataset: process.env.PUBLIC_SANITY_DATASET,
      apiVersion: process.env.PUBLIC_SANITY_API_VERSION,
      useCdn: true,
    }),
  ],
});

I'm not sure how to get these over into the sanity.json using dotenv but I believe the solution to that could be to rename the .env variables in accordance to Sanity's predefined ones (if I'm understanding that docs page correctly): https://www.sanity.io/docs/studio-environment-variables

So PUBLIC_SANITY_PROJECT_ID would instead be called SANITY_STUDIO_API_PROJECT_ID and so on. Then, we'd just nuke the stanza in sanity.json as the .env will have defined those. So this gets removed:

  "api": {
    "projectId": "ndksm626", /* SANITY_STUDIO_API_PROJECT_ID in .env would take care of this and so on */
    "dataset": "production" /* SANITY_STUDIO_API_DATASET already takes care of this too */
  },

My assumption is whatever sanity library code pulls in the sanity.json configuration also knows how to pull from these SANITY_STUDIO_* per this doc and this too

roblevintennis commented 1 year ago

Update: for the sanity.json part of this issue I tried my idea above and when doing sanity start it didn't seem sanity knew which project was being ran so my idea failed lol. I even tried creating a setenv.mjs and calling within the package.json before sanity initialization but I believe that didn't work because dotenv.config() onlly makes that process.env available for duration of the script it's in (so "poof it's gone") by the time my setenv.mjs was ran.

There's got to be a way to do this and I probably just somehow wasn't properly following instructions but I don't yet know how to solve this without the hard coded projectId et al in sanity.json 🤷‍♂️

I saw reference to:

SANITY_STUDIO_API_DATASET=staging sanity build

But that's really no better then hard coding things in the sanity.json imo. Yeah, not really sure what the best practice is for sanity env variables here.

roblevintennis commented 1 year ago

Looks like there's possibly a plugin being made for this to allow .env.development to work: https://www.sanity.io/answers/hi-can-someone-tell-me-the-proper-way-to-enter-p1609953448229300 and https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1657637646828629 But I don't see it. Argh, I'm exceeding my "time box" so I'll have to put this aside for now. If you find a resource I'd be happy to make a PR I just don't yet know what to do for the sanity side of this 🤦