The deals are next on the README’s to-do list, and after them comes curbside pickup, which brings the store’s site its first key. Generic Curbside’s widget needs the store’s publishable key to take orders, and the service also hands out a secret key for its admin. Almost every developer has a story about a key that ended up somewhere it shouldn’t, and it’s usually told with a wince.
In this lesson, you’ll find out where a key goes in a Vite project, which values Vite makes public, and what happens when a key gets committed by mistake. You’ll make that mistake on purpose in a throwaway project, so the store’s history stays clean and you know what to do if it ever happens for real. Start by setting up that throwaway project.
Read a Key From .env
A key typed into the pickup page’s JavaScript would end up in every commit and in every copy of the project. So projects keep keys in a separate file named .env, which stays on your computer, and the code reads the values from there. To try this without touching the store’s site, you’ll create a small Vite project next to it.
First, stop the store’s dev server with Ctrl+C if it’s running, so the new project can use the same address. Then go up one folder from generic-grocery and create the project with Vite’s own starter, create-vite:
$ cd ..
$ npm create vite@latest key-test -- --template vanilla
Need to install the following packages:
[email protected]
Ok to proceed? (y) y
...
◆ Install with npm and start now?
│ ● Yes / ○ No
The -- passes --template vanilla on to create-vite, which then makes a plain JavaScript project without a framework. npm offers to download create-vite only the first time, so type y at that prompt. When create-vite asks whether to install and start, press Enter for Yes:
◇ Install with npm and start now?
│ Yes
│
◇ Scaffolding project in /Users/martin/projects/key-test...
│
◇ Installing dependencies with npm...
added 15 packages, and audited 16 packages in 2s
...
◇ Starting dev server...
...
VITE v8.3.8 ready in 221 ms
➜ Local: http://localhost:5173/
On macOS, npm also reports the blocked install script for fsevents that you’ve met before, and you can leave it unapproved again. The dev server is running, but you’ll change files first, so press Ctrl+C to stop it.
Open the new project in its own window with File → New Window and then File → Open Folder…, and pick key-test. The starter comes with a counter demo, a stylesheet, and images, and you can ignore all of them. You only need two files. Replace everything in src/main.js with this code:
src/main.js
// Two fake keys from .env: which ones does Vite pass on?
const publishable = import.meta.env.VITE_CURBSIDE_KEY;
const secret = import.meta.env.CURBSIDE_SECRET_KEY;
document.querySelector("#app").textContent =
`Publishable: ${publishable}. Secret: ${secret}.`;
import.meta.env is an object that Vite fills in with values for your code. Next, create a file named .env in the key-test folder, next to package.json, with two fake keys:
.env
# Fake Generic Curbside keys, for trying out .env only
VITE_CURBSIDE_KEY=curb_pub_fake_4tQ8xLm2
CURBSIDE_SECRET_KEY=curb_sec_fake_9Hd2sPq7
Each key definition is a name, an equals sign, and a value, without quotes. Open a terminal in the new window, run npm run dev, and open the Local address in VS Code’s integrated browser. The page shows one line of text:
Publishable: curb_pub_fake_4tQ8xLm2. Secret: undefined.