Skip to content

Commit 3732191

Browse files
authored
fix: use the dynamic env sveltekit to load the public env variables (#82)
# Description Use the dynamic module to load the public env variables ## Issue Ticket Number Fixes #81 <!-- Specify above which issue this fixes by referencing the issue number (`#<ISSUE_NUMBER>`) or issue URL. --> <!-- Example: Fixes https://github.com/cloudinary-community/svelte-cloudinary/issues/<ISSUE_NUMBER> --> ## Type of change <!-- Please select all options that are applicable. --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Fix or improve the documentation - [ ] This change requires a documentation update # Checklist <!-- These must all be followed and checked. --> - [ ] I have followed the contributing guidelines of this project as mentioned in [CONTRIBUTING.md](/CONTRIBUTING.md) - [ ] I have created an [issue](https://github.com/cloudinary-community/svelte-cloudinary/issues) ticket for this PR - [ ] I have checked to ensure there aren't other open [Pull Requests](https://github.com/cloudinary-community/svelte-cloudinary/pulls) for the same update/change? - [ ] I have performed a self-review of my own code - [ ] I have run tests locally to ensure they all pass - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes needed to the documentation
1 parent c42fd66 commit 3732191

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

docs/src/docs/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To install Svelte Cloudinary, you can use your favorite package manager includin
3434
Next, you need to setup the environment variable. Insert the following variable into your `.env.local` or `.env` file:
3535

3636
<Code>
37-
VITE_PUBLIC_CLOUDINARY_CLOUD_NAME="[Your Cloud Name]"
37+
PUBLIC_CLOUDINARY_CLOUD_NAME="[Your Cloud Name]"
3838
</Code>
3939

4040
<Callout>Don't have a Cloudinary account? <a href="https://cloudinary.com/users/register_free?utm_campaign=devx_sveltecloudinary&utm_medium=referral&utm_source=sveltecloudinary">Sign up for free</a> on cloudinary.com!</Callout>

svelte-cloudinary/src/lib/components/CldUploadWidget.svelte

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { onMount } from 'svelte';
33
import { triggerOnIdle, loadCloudinary } from '$lib/util.js';
44
import { checkCloudinaryCloudName } from '$lib/cloudinary.js';
5+
import { env } from '$env/dynamic/public';
56
import type {
67
CldUploadWidgetProps,
78
CldUploadWidgetInstanceMethods,
@@ -45,14 +46,15 @@
4546
};
4647
4748
// Validation
48-
checkCloudinaryCloudName(import.meta.env.VITE_PUBLIC_CLOUDINARY_CLOUD_NAME);
49+
const cloudName = env.PUBLIC_CLOUDINARY_CLOUD_NAME || import.meta.env.VITE_PUBLIC_CLOUDINARY_CLOUD_NAME;
50+
checkCloudinaryCloudName(cloudName);
4951
5052
// State
5153
let isLoading = true;
5254
const uploadOptions = {
53-
cloudName: import.meta.env.VITE_PUBLIC_CLOUDINARY_CLOUD_NAME,
54-
uploadPreset: uploadPreset || import.meta.env.PUBLIC_CLOUDINARY_UPLOAD_PRESET,
55-
apiKey: import.meta.env.VITE_PUBLIC_CLOUDINARY_API_KEY,
55+
cloudName,
56+
uploadPreset: uploadPreset || env.PUBLIC_CLOUDINARY_UPLOAD_PRESET || import.meta.env.PUBLIC_CLOUDINARY_UPLOAD_PRESET,
57+
apiKey: env.PUBLIC_CLOUDINARY_API_KEY || import.meta.env.VITE_PUBLIC_CLOUDINARY_API_KEY,
5658
...options
5759
};
5860

svelte-cloudinary/src/lib/helpers/getCldImageUrl.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { constructCloudinaryUrl } from '@cloudinary-util/url-loader';
22
import { checkCloudinaryCloudName } from '../cloudinary.js';
33
import type { ImageOptions, ConfigOptions, AnalyticsOptions } from '@cloudinary-util/url-loader';
4+
import { env } from '$env/dynamic/public';
45

56
import {
67
SVELTE_CLOUDINARY_ANALYTICS_ID,
@@ -22,8 +23,8 @@ export interface GetCldImageUrl {
2223
}
2324

2425
/**
25-
* Generates the Cloudinary url for the assets
26-
* based on the configuration passed to the function
26+
* Generates the Cloudinary url for the assets
27+
* based on the configuration passed to the function
2728
* @returns string
2829
*/
2930
export function getCldImageUrl(
@@ -32,15 +33,16 @@ export function getCldImageUrl(
3233
analytics?: AnalyticsOptions
3334
) {
3435

36+
const cloudName = env.PUBLIC_CLOUDINARY_CLOUD_NAME || import.meta.env.VITE_PUBLIC_CLOUDINARY_CLOUD_NAME;
3537
// Validation
36-
checkCloudinaryCloudName(import.meta.env.VITE_PUBLIC_CLOUDINARY_CLOUD_NAME);
38+
checkCloudinaryCloudName(cloudName);
3739

3840
return constructCloudinaryUrl({
3941
options,
4042
config: Object.assign(
4143
{
4244
cloud: {
43-
cloudName: import.meta.env.VITE_PUBLIC_CLOUDINARY_CLOUD_NAME
45+
cloudName: cloudName
4446
}
4547
},
4648
config

0 commit comments

Comments
 (0)