@uniformdev/canvas-next-rsc 19.46.0 → 19.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- import { type ComponentInstance, type RootComponentInstance } from '@uniformdev/canvas';
1
+ import { type ComponentInstance, type RootComponentInstance, RouteGetResponseComposition } from '@uniformdev/canvas';
2
2
  import { ReactNode } from 'react';
3
- export type CompositionContext = {
3
+ export type CompositionContext = Omit<RouteGetResponseComposition, 'compositionApiResponse'> & {
4
4
  composition: RootComponentInstance;
5
5
  path: string;
6
6
  state?: number;
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { CANVAS_DRAFT_STATE, CANVAS_EDITOR_STATE, CANVAS_PUBLISHED_STATE } from '@uniformdev/canvas';
3
+ import { cookies, headers } from 'next/headers';
3
4
  import { notFound, redirect } from 'next/navigation';
4
5
  import { Suspense } from 'react';
5
6
  import { getManifestFromApi } from '../client/manifestClient';
@@ -56,16 +57,31 @@ const UniformCompositionInner = async ({ params, searchParams, children }) => {
56
57
  if (resolveResult.type === 'notFound') {
57
58
  notFound();
58
59
  }
60
+ const { compositionApiResponse, ...rest } = resolveResult;
61
+ // retrieve the manifest
62
+ const manifest = await getManifestFromApi({
63
+ searchParams,
64
+ });
59
65
  // evaluate the composition
60
66
  const { cookieValue, composition: evaluatedComposition, seenComponents, } = await evaluateComposition({
61
- root: resolveResult.compositionApiResponse.composition,
67
+ root: compositionApiResponse.composition,
62
68
  params,
63
69
  searchParams: searchParams !== null && searchParams !== void 0 ? searchParams : {},
70
+ dynamicInputs: rest.dynamicInputs,
71
+ manifest,
72
+ headers: headers(),
73
+ cookies: cookies()
74
+ .getAll()
75
+ .reduce((acc, cookie) => {
76
+ acc[cookie.name] = cookie.value;
77
+ return acc;
78
+ }, {}),
64
79
  });
65
80
  const context = {
66
81
  composition: evaluatedComposition,
67
82
  path,
68
83
  searchParams,
84
+ ...rest,
69
85
  };
70
86
  return (_jsxs(UniformProvider, { cookieValue: cookieValue, seenComponents: seenComponents, children: [_jsx(UniformScript, { enabled: isCanvasEditing }), _jsx(UniformComponent, { data: evaluatedComposition, context: context }), Boolean(children) && children] }));
71
87
  };
@@ -28,7 +28,7 @@ export function UniformSlot({ name, children, emptyPlaceholder, data: parentData
28
28
  emptyPlaceholder,
29
29
  });
30
30
  const elements = children ? children({ child, component, key: `wrapped-inner-${index}` }) : child;
31
- return createElement(Fragment, { key: index }, elements);
31
+ return createElement(Fragment, { key: `wrapped-${index}` }, elements);
32
32
  });
33
33
  if (!wrapperComponent) {
34
34
  return React.createElement(React.Fragment, undefined, finalChildren);
@@ -0,0 +1,72 @@
1
+ import { CANVAS_LOCALE_TAG_PARAM, CANVAS_LOCALIZATION_SLOT, CANVAS_LOCALIZATION_TYPE, } from '@uniformdev/canvas';
2
+ import { evaluateComposition } from '../evaluateComposition';
3
+ describe('evaluateComposition', () => {
4
+ it('should localize a compoisition when dynmaic inputs include locale token', async () => {
5
+ var _a, _b;
6
+ const root = {
7
+ _id: 'root',
8
+ _name: 'root',
9
+ type: 'root',
10
+ slots: {
11
+ content: [
12
+ {
13
+ type: CANVAS_LOCALIZATION_TYPE,
14
+ slots: {
15
+ [CANVAS_LOCALIZATION_SLOT]: [
16
+ {
17
+ type: 'hero',
18
+ parameters: {
19
+ [CANVAS_LOCALE_TAG_PARAM]: {
20
+ type: CANVAS_LOCALE_TAG_PARAM,
21
+ value: 'fr',
22
+ },
23
+ title: {
24
+ type: 'text',
25
+ value: 'This is the french title!',
26
+ },
27
+ },
28
+ },
29
+ {
30
+ type: 'hero',
31
+ parameters: {
32
+ [CANVAS_LOCALE_TAG_PARAM]: {
33
+ type: CANVAS_LOCALE_TAG_PARAM,
34
+ value: 'en',
35
+ },
36
+ title: {
37
+ type: 'text',
38
+ value: 'This is the english title!',
39
+ },
40
+ },
41
+ },
42
+ ],
43
+ },
44
+ },
45
+ ],
46
+ },
47
+ };
48
+ await evaluateComposition({
49
+ root,
50
+ dynamicInputs: {
51
+ lang: 'en',
52
+ },
53
+ cookies: {},
54
+ headers: new Headers(),
55
+ params: {
56
+ slug: 'whatever',
57
+ },
58
+ searchParams: {},
59
+ config: {
60
+ experimental: {
61
+ localeDynamicInputs: ['lang'],
62
+ },
63
+ },
64
+ manifest: {
65
+ project: {},
66
+ },
67
+ });
68
+ const localizedHero = (_a = root.slots) === null || _a === void 0 ? void 0 : _a['content'][0];
69
+ expect(localizedHero).toBeDefined();
70
+ expect((_b = localizedHero === null || localizedHero === void 0 ? void 0 : localizedHero.parameters) === null || _b === void 0 ? void 0 : _b[CANVAS_LOCALE_TAG_PARAM].value).toBe('en');
71
+ });
72
+ });
@@ -1,6 +1,8 @@
1
- import { RootComponentInstance } from '@uniformdev/canvas';
1
+ import { RootComponentInstance, RouteGetResponseComposition } from '@uniformdev/canvas';
2
+ import { ManifestV2 } from '@uniformdev/context';
3
+ import { UniformServerConfig } from '../config';
2
4
  import { SeenUniformComponent } from '../models';
3
- export declare const evaluateComposition: ({ root, params, searchParams, }: {
5
+ export declare const evaluateComposition: ({ root, params, searchParams, dynamicInputs, manifest, headers, cookies, config, }: {
4
6
  root: RootComponentInstance;
5
7
  params: {
6
8
  slug: string | string[];
@@ -8,6 +10,11 @@ export declare const evaluateComposition: ({ root, params, searchParams, }: {
8
10
  searchParams: {
9
11
  [key: string]: string | undefined;
10
12
  };
13
+ dynamicInputs: RouteGetResponseComposition['dynamicInputs'];
14
+ manifest: ManifestV2;
15
+ headers: Headers;
16
+ cookies: Record<string, string>;
17
+ config?: UniformServerConfig | undefined;
11
18
  }) => Promise<{
12
19
  composition: RootComponentInstance;
13
20
  cookieValue: string | undefined;
@@ -1,21 +1,13 @@
1
- import { CANVAS_PERSONALIZATION_PARAM, CANVAS_PERSONALIZE_TYPE, CANVAS_TEST_TYPE, mapSlotToPersonalizedVariations, mapSlotToTestVariations, walkComponentTree, } from '@uniformdev/canvas';
1
+ import { CANVAS_PERSONALIZATION_PARAM, CANVAS_PERSONALIZE_TYPE, CANVAS_TEST_TYPE, localize, mapSlotToPersonalizedVariations, mapSlotToTestVariations, walkComponentTree, } from '@uniformdev/canvas';
2
2
  import { Context, CookieTransitionDataStore, UNIFORM_DEFAULT_COOKIE_NAME, } from '@uniformdev/context';
3
- import { cookies, headers } from 'next/headers';
4
- import { getManifestFromApi } from '../client/manifestClient';
3
+ import serverConfig from '../config/uniform.server.config';
5
4
  import { serializeCookie } from '../score';
6
- import { applyDiff } from '../utils/apply';
7
- import { isDraftModeEnabled } from '../utils/draft';
8
5
  import { getBaseUrl } from '../utils/url';
9
6
  import { getEnrichmentTags } from './getEnrichmentTags';
10
7
  import { resolvePath } from './resolvePath';
11
- export const evaluateComposition = async ({ root, params, searchParams, }) => {
12
- var _a;
13
- // retrieve the manifest
14
- const manifest = await getManifestFromApi({
15
- searchParams,
16
- });
8
+ export const evaluateComposition = async ({ root, params, searchParams, dynamicInputs, manifest, headers, cookies, config = serverConfig, }) => {
17
9
  // retrieve the score from the cookie, if any
18
- const score = (_a = cookies().get(UNIFORM_DEFAULT_COOKIE_NAME)) === null || _a === void 0 ? void 0 : _a.value;
10
+ const score = cookies[UNIFORM_DEFAULT_COOKIE_NAME];
19
11
  // resolve the path from route params
20
12
  const path = resolvePath({
21
13
  params,
@@ -34,37 +26,30 @@ export const evaluateComposition = async ({ root, params, searchParams, }) => {
34
26
  url.searchParams.set(key, value);
35
27
  }
36
28
  }
37
- const headersInstance = headers();
38
29
  const missingQuirkValue = 'unknown';
39
30
  // update context to update scores
40
31
  await context.update({
41
- cookies: cookies()
42
- .getAll()
43
- .reduce((acc, cookie) => {
44
- acc[cookie.name] = cookie.value;
45
- return acc;
46
- }, {}),
32
+ cookies,
47
33
  url,
48
34
  quirks: {
49
- 'vc-country': headersInstance.get('x-vercel-ip-country') || missingQuirkValue,
50
- 'vc-region': headersInstance.get('x-vercel-ip-country-region') || missingQuirkValue,
51
- 'vc-city': headersInstance.get('x-vercel-ip-city') || missingQuirkValue,
35
+ 'vc-country': headers.get('x-vercel-ip-country') || missingQuirkValue,
36
+ 'vc-region': headers.get('x-vercel-ip-country-region') || missingQuirkValue,
37
+ 'vc-city': headers.get('x-vercel-ip-city') || missingQuirkValue,
52
38
  },
53
39
  });
54
- let composition = root;
55
- if (isDraftModeEnabled({ searchParams })) {
56
- const { vcdiff } = searchParams || {};
57
- if (vcdiff) {
58
- // remove bas64 encoding
59
- const diff = Buffer.from(vcdiff, 'base64').toString('utf-8');
60
- // parse the diff
61
- const parsedDiff = JSON.parse(diff);
62
- // apply the diff
63
- composition = applyDiff({
64
- composition,
65
- diff: parsedDiff,
66
- });
67
- }
40
+ const composition = root;
41
+ const resolvedLocaleKey = Object.keys(dynamicInputs || {}).find((key) => {
42
+ var _a, _b;
43
+ return (_b = (_a = config.experimental) === null || _a === void 0 ? void 0 : _a.localeDynamicInputs) === null || _b === void 0 ? void 0 : _b.includes(key);
44
+ });
45
+ const resolvedLocale = resolvedLocaleKey
46
+ ? dynamicInputs[resolvedLocaleKey]
47
+ : undefined;
48
+ if (typeof resolvedLocale !== 'undefined') {
49
+ localize({
50
+ composition,
51
+ locale: resolvedLocale,
52
+ });
68
53
  }
69
54
  const enrichmentTags = [];
70
55
  const seenComponents = [];
@@ -39,5 +39,9 @@ export type UniformServerConfig = {
39
39
  * @default false
40
40
  */
41
41
  edgeCompositions?: boolean;
42
+ /**
43
+ * Dynamic tokens names that should be used for localization.
44
+ */
45
+ localeDynamicInputs?: string[];
42
46
  };
43
47
  };
@@ -21,5 +21,5 @@ export const isOnVercelPreviewEnvironment = () => {
21
21
  return process.env.VERCEL_ENV === 'preview';
22
22
  };
23
23
  export const isDevelopmentEnvironment = () => {
24
- return process.env.NODE_ENV === 'development';
24
+ return process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test';
25
25
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniformdev/canvas-next-rsc",
3
- "version": "19.46.0",
3
+ "version": "19.47.0",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "scripts": {
6
6
  "dev": "pnpm build --watch",
@@ -56,13 +56,13 @@
56
56
  "typescript": "^5.0.4"
57
57
  },
58
58
  "dependencies": {
59
- "@uniformdev/canvas": "19.46.0",
60
- "@uniformdev/canvas-react": "19.46.0",
61
- "@uniformdev/context": "19.46.0",
62
- "@uniformdev/project-map": "19.46.0",
63
- "@uniformdev/redirect": "19.46.0",
64
- "@uniformdev/richtext": "19.46.0",
65
- "@uniformdev/webhooks": "19.46.0",
59
+ "@uniformdev/canvas": "19.47.0",
60
+ "@uniformdev/canvas-react": "19.47.0",
61
+ "@uniformdev/context": "19.47.0",
62
+ "@uniformdev/project-map": "19.47.0",
63
+ "@uniformdev/redirect": "19.47.0",
64
+ "@uniformdev/richtext": "19.47.0",
65
+ "@uniformdev/webhooks": "19.47.0",
66
66
  "@vercel/edge-config": "^0.2.0",
67
67
  "dequal": "^2.0.3",
68
68
  "js-cookie": "^3.0.5",
@@ -76,5 +76,5 @@
76
76
  "publishConfig": {
77
77
  "access": "public"
78
78
  },
79
- "gitHead": "6adedbd963772fdfe3c43584a89704d58527719d"
79
+ "gitHead": "66bb196919cc178fc94f4d0490d0ac517193304a"
80
80
  }