@serverless-stack/static-site-env

1.18.4 • Public • Published

@serverless-stack/static-site-env npm

A simple CLI (@serverless-stack/static-site-env that allows your static site or Next.js app to load the environment variables from your SST app. This means that you won't have to hard code the config from your backend. Supports React.js, Next.js, and Svelte.

Installation

Run the following in the root of your static site or Next.js app.

# With npm
npm install @serverless-stack/static-site-env --save-dev
# Or with Yarn
yarn add @serverless-stack/static-site-env --dev

Usage

Once installed, tweak the start command in your package.json scripts.

Note that, you need to have sst start running for this to work.

React.js

"scripts": {
  "start": "sst-env -- react-scripts start",
},

Start your local dev environment as usual.

npm run start

Next.js

"scripts": {
  "dev": "sst-env -- next dev",
},

And run.

npm run dev

Svelte

"scripts": {
  "dev": "sst-env -- vite",
},

And run.

npm run dev

Options

--path

A relative path to the directory containing the sst.json file.

Note that, the sst-env CLI will traverse up the directories to look for the root of your SST app. If the static site or Next.js app is located outside the SST app folder, for example:

/
  backend/
    sst.json
  frontend/
    package.json

Pass in --path to specify the relative path of the SST app.

"scripts": {
  "start": "sst-env --path ../backend -- react-scripts start",
},

How it works

Here's what's going on behind the scenes.

  1. The sst start command generates a file with the values specified by StaticSite, ReactStaticSite, ViteStaticSite, or NextjsSite construct's environment prop.
  2. The sst-env CLI will traverse up the directories to look for the root of your SST app. If the static site or Next.js app is located outside the SST app folder, pass in --path to specify the relative path of the SST app.
  3. It'll then find the file that's generated in step 1.
  4. It'll load these as environment variables before running the start command.

Versions

Current Tags

Version History

Package Sidebar

Install

npm i @serverless-stack/static-site-env

Homepage

sst.dev

Weekly Downloads

1,802

Version

1.18.4

License

MIT

Unpacked Size

12.5 kB

Total Files

22

Last publish

Collaborators

  • thdxr
  • fanjie
  • jayair