-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Environment Variables In VitePress - issue#4516 #4517
Open
KareimGazer
wants to merge
1
commit into
vuejs:main
Choose a base branch
from
KareimGazer:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+69
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
outline: deep | ||
--- | ||
|
||
# Environment Variables | ||
|
||
## Basic Workflow | ||
|
||
Using environment variables in VitePress is essential for managing sensitive data such as API keys and service URLs. | ||
VitePress allows you to load environment variables from `.env` files using the `loadEnv` function to load these variables into your configuration (e.g. `.vitepress/config.ts`). | ||
|
||
### Step 1: Create the .env File | ||
|
||
The first step in setting up environment variables is to create a `.env` file in the root directory of your VitePress project. | ||
This file will store your sensitive information and configuration settings. | ||
|
||
### Step 2: Defining Environment Variables | ||
|
||
Open the `.env` file and define your environment variables. | ||
Remember that in Vite, all environment variables that need to be exposed to your application must be prefixed with `VITE_.` Here’s an example of what your `.env` file might look like | ||
|
||
```bash | ||
VITE_ALGOLIA_APP_ID=your_algolia_app_id | ||
VITE_ALGOLIA_API_KEY=your_algolia_api_key | ||
VITE_ALGOLIA_INDEX_NAME=your_algolia_index_name | ||
``` | ||
|
||
### Step 3: Load Environment Variables in VitePress Configuration | ||
|
||
Next, you need to load these environment variables into your VitePress configuration. This is done using the `loadEnv` function from VitePress. | ||
|
||
```js | ||
import { loadEnv } from 'vitepress'; | ||
|
||
// Load environment variables | ||
const env = loadEnv('', process.cwd()); | ||
``` | ||
|
||
### Step 4: Accessing Environment Variables in Your Application | ||
|
||
for example, configuring your blog to use Algolia | ||
|
||
```js | ||
import { loadEnv } from 'vitepress'; | ||
import { defineConfig } from 'vitepress'; | ||
|
||
// Load environment variables | ||
const env = loadEnv('', process.cwd()); | ||
|
||
export default defineConfig({ | ||
themeConfig: { | ||
search: { | ||
provider: 'algolia', | ||
options: { | ||
appId: env.VITE_ALGOLIA_APP_ID, | ||
apiKey: env.VITE_ALGOLIA_API_KEY, | ||
indexName: env.VITE_ALGOLIA_INDEX_NAME, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
``` | ||
|
||
## Important Considerations | ||
|
||
- **Prefix Requirement**: Ensure all relevant environment variables are prefixed with `VITE_`. Only these will be accessible in your application. | ||
- **File Loading Order**: Vite loads `.env` files based on specific priorities. For example, `.env.production` will override settings from `.env`. | ||
- **Restarting Your Server**: After making changes to your `.env` file, restart your VitePress server for changes to take effect. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be misleading.
While it depends on the specifics of the use case, using environment variables won't protect sensitive data in most cases. Usually the values of the variables end up in the built JS files, where anyone can access them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's true
I used the API keys as GitHub secrets in my CI/CD pipeline but still these values need to be read from the environment even if they are not written in the
.env
file.Could you think of a better, more specific example?