Skip to content

Commit 61a86fb

Browse files
Merge pull request #187 from cloudinary/fix/vue-ssr-analytics-token
Fix/vue ssr analytics token
2 parents c6ffecb + 275fcef commit 61a86fb

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

packages/vue3/src/components/AdvancedImage.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<img v-if="isBrowser()" ref="imageRef" />
3-
<img v-else src="{{getSsrSrc()}}" />
3+
<img v-else :src="getSsrSrc()" />
44
</template>
55

66
<script setup lang="ts">
@@ -37,7 +37,7 @@ const props = defineProps<ImgProps>();
3737
const imageRef = ref(null);
3838
let htmlLayerInstance;
3939
40-
const getSsrSrc = () => serverSideSrc(props.plugins, props.cldImg);
40+
const getSsrSrc = () => serverSideSrc(props.plugins, props.cldImg, SDKAnalyticsConstants);
4141
4242
/**
4343
* On mount, creates a new HTMLLayer instance and initializes with ref to img element,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
import { AdvancedImage } from "../../src";
5+
import { CloudinaryImage } from "@cloudinary/url-gen/assets/CloudinaryImage";
6+
import { createSSRApp } from "vue";
7+
import { renderToString } from "vue/server-renderer";
8+
import { SDKAnalyticsConstants } from "../../src/internal/SDKAnalyticsConstants";
9+
import { testIf } from "./utils";
10+
11+
const cloudinaryImage = new CloudinaryImage("sample", { cloudName: "demo" });
12+
13+
describe("analytics", () => {
14+
testIf(
15+
!(process.env.VUE3_TEST_ENV === "DIST"),
16+
"creates an img with analytics using src",
17+
async () => {
18+
// Update src analytics value
19+
SDKAnalyticsConstants.sdkSemver = "1.0.0";
20+
SDKAnalyticsConstants.techVersion = "10.2.5";
21+
22+
const app = createSSRApp({
23+
template: '<AdvancedImage :cldImg="cldImg" />',
24+
data: () => ({
25+
cldImg: cloudinaryImage,
26+
}),
27+
components: { AdvancedImage },
28+
});
29+
const html = await renderToString(app);
30+
expect(html).toMatch(
31+
'<img src="https://res.cloudinary.com/demo/image/upload/sample?_a=AL'
32+
);
33+
}
34+
);
35+
36+
testIf(
37+
process.env.VUE3_TEST_ENV === "DIST",
38+
"creates an img with analytics using dist",
39+
async () => {
40+
const app = createSSRApp({
41+
template: '<AdvancedImage :cldImg="cldImg" />',
42+
data: () => ({
43+
cldImg: cloudinaryImage,
44+
}),
45+
components: { AdvancedImage },
46+
});
47+
const html = await renderToString(app);
48+
expect(html).toMatch(
49+
'<img src="https://res.cloudinary.com/demo/image/upload/sample?_a=AL'
50+
);
51+
}
52+
);
53+
});

packages/vue3/tests/unit/analytics.spec.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
import { AdvancedImage } from "../../src";
22
import { CloudinaryImage } from "@cloudinary/url-gen/assets/CloudinaryImage";
33
import { mount } from "@vue/test-utils";
4-
import { waitTicks } from "./utils";
4+
import { testIf, waitTicks } from "./utils";
55
import { SDKAnalyticsConstants } from "../../src/internal/SDKAnalyticsConstants";
66

77
const cloudinaryImage = new CloudinaryImage("sample", { cloudName: "demo" });
88

9-
/**
10-
* Run test if condition is true
11-
* Otherwise act as passing test
12-
* @param condition
13-
* @param args
14-
*/
15-
const testIf = (condition: boolean, ...args: [string, () => void]) =>
16-
condition ? test(...args) : {};
17-
189
describe("analytics", () => {
1910
testIf(
2011
!(process.env.VUE3_TEST_ENV === "DIST"),

packages/vue3/tests/unit/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ export const waitTicks = async (ticks: number) => {
1111
await nextTick();
1212
}
1313
};
14+
15+
/**
16+
* Run test if condition is true
17+
* Otherwise act as passing test
18+
* @param condition
19+
* @param args
20+
*/
21+
export const testIf = (condition: boolean, ...args: [string, () => void]) =>
22+
condition ? test(...args) : {};

0 commit comments

Comments
 (0)