@decocms/blocks 8.0.0 → 8.1.0-next.1
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.
- package/bin/deco.js +13 -0
- package/package.json +32 -82
- package/src/index.ts +35 -8
- package/src/protocol/assets.ts +124 -0
- package/src/protocol/canonical.ts +7 -0
- package/src/protocol/ciphertext.ts +6 -0
- package/src/protocol/client.ts +170 -0
- package/src/protocol/conformance/cases/assets.ts +81 -0
- package/src/protocol/conformance/cases/guards.ts +314 -0
- package/src/protocol/conformance/cases/reads.ts +168 -0
- package/src/protocol/conformance/cases/types.ts +9 -0
- package/src/protocol/conformance/cases/wire.ts +138 -0
- package/src/protocol/conformance/cases/writes.ts +345 -0
- package/src/protocol/conformance/contentHashFixtures.ts +79 -0
- package/src/protocol/conformance/context.ts +222 -0
- package/src/protocol/conformance/index.ts +140 -0
- package/src/protocol/errors.ts +142 -0
- package/src/protocol/index.ts +39 -0
- package/src/protocol/keys.ts +279 -0
- package/src/protocol/params.ts +67 -0
- package/src/protocol/secrets.ts +231 -0
- package/src/protocol/server/assets.ts +119 -0
- package/src/protocol/server/auth.ts +49 -0
- package/src/protocol/server/bodyCache.ts +84 -0
- package/src/protocol/server/content.ts +175 -0
- package/src/protocol/server/core.ts +112 -0
- package/src/protocol/server/handler.ts +102 -0
- package/src/protocol/server/http.ts +139 -0
- package/src/protocol/server/index.ts +11 -0
- package/src/protocol/server/methods/apply.ts +341 -0
- package/src/protocol/server/methods/read.ts +156 -0
- package/src/protocol/server/rpc.ts +136 -0
- package/src/protocol/storage/fs/hash.ts +21 -0
- package/src/protocol/storage/fs/index.ts +338 -0
- package/src/protocol/storage/fs/lock.ts +68 -0
- package/src/protocol/storage/fs/transaction.ts +183 -0
- package/src/protocol/storage/fs/unsupported.ts +15 -0
- package/src/protocol/storage/memory.ts +238 -0
- package/src/protocol/storage.ts +182 -0
- package/src/protocol/types.ts +172 -0
- package/src/v8/analytics.ts +50 -0
- package/src/v8/background.ts +107 -0
- package/src/v8/builtins/data.ts +23 -0
- package/src/v8/builtins/index.ts +26 -0
- package/src/v8/builtins/lazy.ts +23 -0
- package/src/v8/builtins/legacy.ts +65 -0
- package/src/v8/builtins/matchers.ts +31 -0
- package/src/v8/builtins/multivariate.ts +25 -0
- package/src/v8/builtins/secret.ts +82 -0
- package/src/v8/canonical.ts +117 -0
- package/src/v8/ciphertext.ts +158 -0
- package/src/v8/cli/builtins.ts +20 -0
- package/src/v8/cli/check/index.ts +774 -0
- package/src/v8/cli/check/messages.ts +168 -0
- package/src/v8/cli/content.ts +287 -0
- package/src/v8/cli/index.ts +17 -0
- package/src/v8/cli/log.ts +18 -0
- package/src/v8/cli/main.ts +12 -0
- package/src/v8/cli/root.ts +66 -0
- package/src/v8/cli/run.ts +236 -0
- package/src/v8/cli/schema/builtinSchemas.ts +236 -0
- package/src/v8/cli/schema/generate.ts +507 -0
- package/src/v8/cli/schema/index.ts +63 -0
- package/src/v8/cli/schema/tsProgram.ts +497 -0
- package/src/v8/cli/schema/typeToSchema.ts +689 -0
- package/src/v8/cli/serve/server.ts +303 -0
- package/src/v8/cli/watch.ts +86 -0
- package/src/v8/client.ts +147 -0
- package/src/v8/cms.ts +237 -0
- package/src/v8/content.ts +195 -0
- package/src/v8/draft.ts +136 -0
- package/src/v8/errors.ts +59 -0
- package/src/v8/fetch.ts +133 -0
- package/src/v8/identity.ts +65 -0
- package/src/v8/index.ts +39 -0
- package/src/v8/json.ts +74 -0
- package/src/v8/matchRoute.ts +204 -0
- package/src/v8/remoteLoader.ts +282 -0
- package/src/v8/resolver.ts +278 -0
- package/src/v8/secrets.ts +17 -0
- package/src/v8/telemetry.ts +533 -0
- package/src/v8/types.ts +240 -0
- package/src/cms/applySectionConventions.ts +0 -124
- package/src/cms/blockSource.test.ts +0 -76
- package/src/cms/blockSource.ts +0 -191
- package/src/cms/client.browserBundle.test.ts +0 -44
- package/src/cms/client.ts +0 -80
- package/src/cms/draftSource.test.ts +0 -343
- package/src/cms/draftSource.ts +0 -362
- package/src/cms/index.ts +0 -151
- package/src/cms/layoutCacheRace.test.ts +0 -181
- package/src/cms/loadDecofileDirectory.test.ts +0 -109
- package/src/cms/loadDecofileDirectory.ts +0 -89
- package/src/cms/loader.test.ts +0 -200
- package/src/cms/loader.ts +0 -304
- package/src/cms/registry.test.ts +0 -110
- package/src/cms/registry.ts +0 -242
- package/src/cms/resolve.test.ts +0 -980
- package/src/cms/resolve.ts +0 -2282
- package/src/cms/schema.test.ts +0 -356
- package/src/cms/schema.ts +0 -1215
- package/src/cms/sectionLoaderContext.test.ts +0 -111
- package/src/cms/sectionLoaderContext.ts +0 -113
- package/src/cms/sectionLoaders.test.ts +0 -533
- package/src/cms/sectionLoaders.ts +0 -636
- package/src/cms/sectionMixins.test.ts +0 -167
- package/src/cms/sectionMixins.ts +0 -153
- package/src/cms/stickyFlags.test.ts +0 -117
- package/src/flags/audience.ts +0 -56
- package/src/flags/everyone.ts +0 -29
- package/src/flags/flag.ts +0 -15
- package/src/flags/flags.test.ts +0 -148
- package/src/flags/multivariate/image.ts +0 -11
- package/src/flags/multivariate/message.ts +0 -11
- package/src/flags/multivariate/page.ts +0 -16
- package/src/flags/multivariate/section.ts +0 -16
- package/src/flags/multivariate.ts +0 -1
- package/src/flags/types.ts +0 -59
- package/src/flags/utils/multivariate.ts +0 -20
- package/src/hooks/Image.test.ts +0 -100
- package/src/hooks/Image.tsx +0 -250
- package/src/hooks/JsonLd.tsx +0 -353
- package/src/hooks/LazySection.tsx +0 -121
- package/src/hooks/LiveControls.tsx +0 -122
- package/src/hooks/Picture.tsx +0 -127
- package/src/hooks/RenderSection.test.tsx +0 -81
- package/src/hooks/RenderSection.tsx +0 -91
- package/src/hooks/SectionErrorFallback.tsx +0 -97
- package/src/hooks/index.ts +0 -30
- package/src/matchers/builtins.test.ts +0 -318
- package/src/matchers/builtins.ts +0 -459
- package/src/matchers/countryNames.ts +0 -104
- package/src/matchers/override.test.ts +0 -205
- package/src/matchers/override.ts +0 -136
- package/src/matchers/posthog.ts +0 -154
- package/src/middleware/decoState.ts +0 -55
- package/src/middleware/healthMetrics.ts +0 -133
- package/src/middleware/hydrationContext.test.ts +0 -61
- package/src/middleware/hydrationContext.ts +0 -79
- package/src/middleware/index.ts +0 -88
- package/src/middleware/liveness.ts +0 -21
- package/src/middleware/observability.test.ts +0 -242
- package/src/middleware/observability.ts +0 -643
- package/src/middleware/validateSection.test.ts +0 -147
- package/src/middleware/validateSection.ts +0 -100
- package/src/sdk/abTesting.test.ts +0 -326
- package/src/sdk/abTesting.ts +0 -499
- package/src/sdk/analytics.ts +0 -77
- package/src/sdk/cacheHeaders.test.ts +0 -115
- package/src/sdk/cacheHeaders.ts +0 -433
- package/src/sdk/cachedLoader.test.ts +0 -122
- package/src/sdk/cachedLoader.ts +0 -503
- package/src/sdk/cachedLoaderModule.test.ts +0 -209
- package/src/sdk/clx.ts +0 -5
- package/src/sdk/cn.test.ts +0 -34
- package/src/sdk/cn.ts +0 -28
- package/src/sdk/composite.test.ts +0 -121
- package/src/sdk/composite.ts +0 -114
- package/src/sdk/cookie.test.ts +0 -108
- package/src/sdk/cookie.ts +0 -129
- package/src/sdk/crypto.ts +0 -185
- package/src/sdk/csp.ts +0 -59
- package/src/sdk/detectDevice.ts +0 -45
- package/src/sdk/djb2.ts +0 -20
- package/src/sdk/encoding.test.ts +0 -71
- package/src/sdk/encoding.ts +0 -47
- package/src/sdk/env.test.ts +0 -33
- package/src/sdk/env.ts +0 -54
- package/src/sdk/flags.test.ts +0 -91
- package/src/sdk/flags.ts +0 -108
- package/src/sdk/http.test.ts +0 -71
- package/src/sdk/http.ts +0 -124
- package/src/sdk/index.ts +0 -90
- package/src/sdk/inflightTimeout.test.ts +0 -35
- package/src/sdk/inflightTimeout.ts +0 -53
- package/src/sdk/instrumentedFetch.test.ts +0 -559
- package/src/sdk/instrumentedFetch.ts +0 -339
- package/src/sdk/invoke.test.ts +0 -115
- package/src/sdk/invoke.ts +0 -298
- package/src/sdk/logger.test.ts +0 -432
- package/src/sdk/logger.ts +0 -304
- package/src/sdk/mergeCacheControl.ts +0 -150
- package/src/sdk/normalizeUrls.ts +0 -91
- package/src/sdk/observability.ts +0 -109
- package/src/sdk/otel.test.ts +0 -526
- package/src/sdk/otel.ts +0 -1012
- package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
- package/src/sdk/otelAdapters.test.ts +0 -89
- package/src/sdk/otelAdapters.ts +0 -144
- package/src/sdk/otelHttpLog.test.ts +0 -457
- package/src/sdk/otelHttpLog.ts +0 -419
- package/src/sdk/otelHttpMeter.test.ts +0 -292
- package/src/sdk/otelHttpMeter.ts +0 -506
- package/src/sdk/otelHttpTracer.test.ts +0 -474
- package/src/sdk/otelHttpTracer.ts +0 -543
- package/src/sdk/redirects.ts +0 -228
- package/src/sdk/requestContext.ts +0 -281
- package/src/sdk/requestContextStorage.browser.test.ts +0 -29
- package/src/sdk/requestContextStorage.browser.ts +0 -43
- package/src/sdk/requestContextStorage.ts +0 -35
- package/src/sdk/retry.ts +0 -45
- package/src/sdk/serverTimings.ts +0 -68
- package/src/sdk/signal.ts +0 -42
- package/src/sdk/sitemap.ts +0 -160
- package/src/sdk/urlRedaction.test.ts +0 -73
- package/src/sdk/urlRedaction.ts +0 -82
- package/src/sdk/urlUtils.ts +0 -134
- package/src/sdk/useDevice.test.ts +0 -130
- package/src/sdk/useDevice.ts +0 -89
- package/src/sdk/useDeviceContext.tsx +0 -108
- package/src/sdk/useId.ts +0 -7
- package/src/sdk/useScript.test.ts +0 -128
- package/src/sdk/useScript.ts +0 -210
- package/src/sdk/useSuggestions.test.ts +0 -230
- package/src/sdk/useSuggestions.ts +0 -188
- package/src/sdk/wrapCaughtErrors.ts +0 -107
- package/src/setup.ts +0 -120
- package/src/types/index.ts +0 -53
- package/src/types/widgets.ts +0 -14
- package/tsconfig.json +0 -7
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { ImageWidget, MultivariateFlag } from "../types";
|
|
2
|
-
import multivariate, { type MultivariateProps } from "../utils/multivariate";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @title Image Variants
|
|
6
|
-
*/
|
|
7
|
-
export default function Image(
|
|
8
|
-
props: MultivariateProps<ImageWidget>,
|
|
9
|
-
): MultivariateFlag<ImageWidget> {
|
|
10
|
-
return multivariate(props);
|
|
11
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { MultivariateFlag } from "../types";
|
|
2
|
-
import multivariate, { type MultivariateProps } from "../utils/multivariate";
|
|
3
|
-
|
|
4
|
-
export type Message = string;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @title Message Variants
|
|
8
|
-
*/
|
|
9
|
-
export default function Message(props: MultivariateProps<Message>): MultivariateFlag<Message> {
|
|
10
|
-
return multivariate(props);
|
|
11
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { MultivariateFlag } from "../types";
|
|
2
|
-
import multivariate, { type MultivariateProps } from "../utils/multivariate";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Section type placeholder — the actual Section type is defined by the framework.
|
|
6
|
-
*/
|
|
7
|
-
type Section = unknown;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @title Page Variants
|
|
11
|
-
*/
|
|
12
|
-
export default function PageVariants(
|
|
13
|
-
props: MultivariateProps<Section[]>,
|
|
14
|
-
): MultivariateFlag<Section[]> {
|
|
15
|
-
return multivariate(props);
|
|
16
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { MultivariateFlag } from "../types";
|
|
2
|
-
import multivariate, { type MultivariateProps } from "../utils/multivariate";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Section type placeholder — the actual Section type is defined by the framework.
|
|
6
|
-
*/
|
|
7
|
-
type Section = unknown;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @title Section Variants
|
|
11
|
-
*/
|
|
12
|
-
export default function SectionVariants(
|
|
13
|
-
props: MultivariateProps<Section>,
|
|
14
|
-
): MultivariateFlag<Section> {
|
|
15
|
-
return multivariate(props);
|
|
16
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default } from "./multivariate/page";
|
package/src/flags/types.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Types for the feature-flag primitives in this subpath.
|
|
3
|
-
*
|
|
4
|
-
* Ported from apps-start's `website/types.ts` (the flag/matcher slice only).
|
|
5
|
-
* These are intentionally self-contained: `Matcher`/`MatchContext` here are
|
|
6
|
-
* a *function-composition* matcher — a plain `(ctx) => boolean` imported and
|
|
7
|
-
* invoked directly by TS code — which is a different mechanism from
|
|
8
|
-
* `@decocms/blocks/matchers`'s CMS rule engine (`registerMatcher`/
|
|
9
|
-
* `evaluateMatcher` dispatch by name over a `Record<string, unknown>` rule
|
|
10
|
-
* against `MatcherContext` from `cms/resolve.ts`). The two don't share a
|
|
11
|
-
* context shape (this `MatchContext` has `device`/`siteId`; the CMS one
|
|
12
|
-
* doesn't), so they are not merged.
|
|
13
|
-
*/
|
|
14
|
-
import type { ImageWidget } from "../types/widgets";
|
|
15
|
-
|
|
16
|
-
export type { ImageWidget };
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Context passed to matchers at request time.
|
|
20
|
-
* The framework populates this from the incoming request.
|
|
21
|
-
*/
|
|
22
|
-
export interface MatchContext {
|
|
23
|
-
request: Request;
|
|
24
|
-
device: "mobile" | "tablet" | "desktop";
|
|
25
|
-
siteId: number;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* A matcher is a function that evaluates request context and returns a boolean.
|
|
30
|
-
*/
|
|
31
|
-
export type Matcher = (ctx: MatchContext) => boolean;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* A feature flag with a matcher and two branches.
|
|
35
|
-
* The framework evaluates the matcher at request time and selects the
|
|
36
|
-
* appropriate branch value.
|
|
37
|
-
*/
|
|
38
|
-
export interface FlagObj<T> {
|
|
39
|
-
matcher: Matcher;
|
|
40
|
-
true: T;
|
|
41
|
-
false: T;
|
|
42
|
-
name: string;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* A single variant in a multivariate flag.
|
|
47
|
-
*/
|
|
48
|
-
export interface Variant<T> {
|
|
49
|
-
matcher?: Matcher;
|
|
50
|
-
value: T;
|
|
51
|
-
weight?: number;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* A multivariate flag with multiple variants, each with its own matcher.
|
|
56
|
-
*/
|
|
57
|
-
export interface MultivariateFlag<T> {
|
|
58
|
-
variants: Variant<T>[];
|
|
59
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { MultivariateFlag, Variant } from "../types";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @title Multivariate
|
|
5
|
-
*/
|
|
6
|
-
export interface MultivariateProps<T> {
|
|
7
|
-
/**
|
|
8
|
-
* @minItems 1
|
|
9
|
-
* @addBehavior 1
|
|
10
|
-
*/
|
|
11
|
-
variants: Variant<T>[];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @title Variant
|
|
16
|
-
* @label hidden
|
|
17
|
-
*/
|
|
18
|
-
export default function multivariate<T>(props: MultivariateProps<T>): MultivariateFlag<T> {
|
|
19
|
-
return props;
|
|
20
|
-
}
|
package/src/hooks/Image.test.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import { getOptimizedMediaUrl, getSrcSet } from "./Image";
|
|
3
|
-
|
|
4
|
-
describe("getOptimizedMediaUrl", () => {
|
|
5
|
-
let warnSpy: ReturnType<typeof vi.spyOn>;
|
|
6
|
-
let prevNodeEnv: string | undefined;
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
prevNodeEnv = process.env.NODE_ENV;
|
|
10
|
-
process.env.NODE_ENV = "development";
|
|
11
|
-
warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
afterEach(() => {
|
|
15
|
-
warnSpy.mockRestore();
|
|
16
|
-
if (prevNodeEnv === undefined) delete process.env.NODE_ENV;
|
|
17
|
-
else process.env.NODE_ENV = prevNodeEnv;
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("returns empty string and warns when src is undefined", () => {
|
|
21
|
-
const result = getOptimizedMediaUrl({
|
|
22
|
-
originalSrc: undefined as unknown as string,
|
|
23
|
-
width: 100,
|
|
24
|
-
fit: "cover",
|
|
25
|
-
});
|
|
26
|
-
expect(result).toBe("");
|
|
27
|
-
expect(warnSpy).toHaveBeenCalledTimes(1);
|
|
28
|
-
expect(warnSpy.mock.calls[0][0]).toMatch(/empty\/undefined src/);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("returns empty string when src is empty", () => {
|
|
32
|
-
const result = getOptimizedMediaUrl({
|
|
33
|
-
originalSrc: "",
|
|
34
|
-
width: 100,
|
|
35
|
-
fit: "cover",
|
|
36
|
-
});
|
|
37
|
-
expect(result).toBe("");
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it("returns empty string when src is null", () => {
|
|
41
|
-
const result = getOptimizedMediaUrl({
|
|
42
|
-
originalSrc: null as unknown as string,
|
|
43
|
-
width: 100,
|
|
44
|
-
fit: "cover",
|
|
45
|
-
});
|
|
46
|
-
expect(result).toBe("");
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it("does NOT warn in production for missing src", () => {
|
|
50
|
-
process.env.NODE_ENV = "production";
|
|
51
|
-
const result = getOptimizedMediaUrl({
|
|
52
|
-
originalSrc: undefined as unknown as string,
|
|
53
|
-
width: 100,
|
|
54
|
-
fit: "cover",
|
|
55
|
-
});
|
|
56
|
-
expect(result).toBe("");
|
|
57
|
-
expect(warnSpy).not.toHaveBeenCalled();
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("returns data URI as-is", () => {
|
|
61
|
-
const dataUri = "data:image/png;base64,iVBORw0KGgo=";
|
|
62
|
-
expect(
|
|
63
|
-
getOptimizedMediaUrl({
|
|
64
|
-
originalSrc: dataUri,
|
|
65
|
-
width: 100,
|
|
66
|
-
fit: "cover",
|
|
67
|
-
}),
|
|
68
|
-
).toBe(dataUri);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it("routes through Deco image CDN for arbitrary src", () => {
|
|
72
|
-
const result = getOptimizedMediaUrl({
|
|
73
|
-
originalSrc: "https://cdn.example.com/foo.jpg",
|
|
74
|
-
width: 200,
|
|
75
|
-
fit: "cover",
|
|
76
|
-
});
|
|
77
|
-
expect(result).toContain("/image?");
|
|
78
|
-
expect(result).toContain("width=200");
|
|
79
|
-
expect(result).toContain("fit=cover");
|
|
80
|
-
expect(result).toContain("src=https://cdn.example.com/foo.jpg");
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
describe("getSrcSet", () => {
|
|
85
|
-
it("returns undefined when src is undefined", () => {
|
|
86
|
-
expect(getSrcSet(undefined as unknown as string, 100)).toBeUndefined();
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it("returns undefined when src is empty", () => {
|
|
90
|
-
expect(getSrcSet("", 100)).toBeUndefined();
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it("produces a srcset string for valid src", () => {
|
|
94
|
-
const result = getSrcSet("https://cdn.example.com/foo.jpg", 100);
|
|
95
|
-
expect(result).toBeDefined();
|
|
96
|
-
// Each factor entry is "<url> <width>w".
|
|
97
|
-
expect(result).toMatch(/\d+w/);
|
|
98
|
-
expect(result).toContain("foo.jpg");
|
|
99
|
-
});
|
|
100
|
-
});
|
package/src/hooks/Image.tsx
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
import type { ImgHTMLAttributes } from "react";
|
|
2
|
-
import { forwardRef } from "react";
|
|
3
|
-
|
|
4
|
-
// -------------------------------------------------------------------------
|
|
5
|
-
// Known asset prefixes that get stripped to produce a relative src path
|
|
6
|
-
// -------------------------------------------------------------------------
|
|
7
|
-
|
|
8
|
-
const DECO_CACHE_URL = "https://assets.decocache.com/";
|
|
9
|
-
const S3_URL = "https://deco-sites-assets.s3.sa-east-1.amazonaws.com/";
|
|
10
|
-
|
|
11
|
-
// -------------------------------------------------------------------------
|
|
12
|
-
// Configurable CDN domain
|
|
13
|
-
// -------------------------------------------------------------------------
|
|
14
|
-
|
|
15
|
-
let imageCdnDomain = "decoims.com";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Register the image CDN domain used by `getOptimizedMediaUrl`.
|
|
19
|
-
* Call once in your site's setup.ts before any page loads.
|
|
20
|
-
*
|
|
21
|
-
* Available domains:
|
|
22
|
-
* - `decoims.com` (Cloudflare, default — best compression, same edge as Workers)
|
|
23
|
-
* - `deco-assets.edgedeco.com` (Azion IMS)
|
|
24
|
-
* - `deco-assets.decoazn.com` (Azion IMS, legacy)
|
|
25
|
-
*/
|
|
26
|
-
export function registerImageCdnDomain(domain: string) {
|
|
27
|
-
imageCdnDomain = domain.replace(/^https?:\/\//, "").replace(/\/+$/, "");
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function getImageCdnDomain(): string {
|
|
31
|
-
return imageCdnDomain;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// -------------------------------------------------------------------------
|
|
35
|
-
// Fit options & optimization types
|
|
36
|
-
// -------------------------------------------------------------------------
|
|
37
|
-
|
|
38
|
-
export type FitOptions = "contain" | "cover" | "fill";
|
|
39
|
-
|
|
40
|
-
export const FACTORS = [1, 2];
|
|
41
|
-
|
|
42
|
-
interface OptimizationOptions {
|
|
43
|
-
originalSrc: string;
|
|
44
|
-
width: number;
|
|
45
|
-
height?: number;
|
|
46
|
-
fit: FitOptions;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// -------------------------------------------------------------------------
|
|
50
|
-
// Platform-specific URL optimizers (fallbacks when the CDN can handle
|
|
51
|
-
// the native platform's resize syntax directly)
|
|
52
|
-
// -------------------------------------------------------------------------
|
|
53
|
-
|
|
54
|
-
function optimizeVTEX(originalSrc: string, width: number, height?: number): string {
|
|
55
|
-
const src = new URL(originalSrc);
|
|
56
|
-
const [slash, arquivos, ids, rawId, ...rest] = src.pathname.split("/");
|
|
57
|
-
const [trueId] = rawId.split("-");
|
|
58
|
-
|
|
59
|
-
src.pathname = [slash, arquivos, ids, `${trueId}-${width}-${height ?? width}`, ...rest].join("/");
|
|
60
|
-
|
|
61
|
-
return src.href;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function optimizeShopify(originalSrc: string, width: number, height?: number): string {
|
|
65
|
-
const url = new URL(originalSrc);
|
|
66
|
-
url.searchParams.set("width", `${width}`);
|
|
67
|
-
if (height) url.searchParams.set("height", `${height}`);
|
|
68
|
-
url.searchParams.set("crop", "center");
|
|
69
|
-
return url.href;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// -------------------------------------------------------------------------
|
|
73
|
-
// Core optimization function
|
|
74
|
-
// Ported from deco-cx/apps website/components/Image.tsx
|
|
75
|
-
// -------------------------------------------------------------------------
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Builds an optimized image URL.
|
|
79
|
-
*
|
|
80
|
-
* For Deco-hosted images (decocache / S3), strips the known prefix and
|
|
81
|
-
* routes through the Deco image CDN for edge resize + format conversion.
|
|
82
|
-
*
|
|
83
|
-
* For platform-specific images (VTEX, Shopify), rewrites the URL using
|
|
84
|
-
* the platform's native resize params — no CDN proxy needed.
|
|
85
|
-
*
|
|
86
|
-
* Data URIs are returned as-is.
|
|
87
|
-
*/
|
|
88
|
-
export function getOptimizedMediaUrl(opts: OptimizationOptions): string {
|
|
89
|
-
const { originalSrc, width, height, fit } = opts;
|
|
90
|
-
|
|
91
|
-
// Defensive: an upstream CMS payload occasionally has missing/null image
|
|
92
|
-
// fields. Crashing the entire React tree on `undefined.startsWith` would
|
|
93
|
-
// take down the whole page. Return an empty string so the resulting
|
|
94
|
-
// `<img>` is rendered with no src — the browser shows the broken-image
|
|
95
|
-
// placeholder and SSR completes cleanly.
|
|
96
|
-
if (typeof originalSrc !== "string" || originalSrc.length === 0) {
|
|
97
|
-
if (typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
|
|
98
|
-
console.warn(
|
|
99
|
-
`[Image] getOptimizedMediaUrl called with empty/undefined src — rendering empty src instead of crashing.`,
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
return "";
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (originalSrc.startsWith("data:")) {
|
|
106
|
-
return originalSrc;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (/(vteximg\.com\.br|vtexassets\.com|myvtex\.com)\/arquivos\/ids\/\d+/.test(originalSrc)) {
|
|
110
|
-
return optimizeVTEX(originalSrc, width, height);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (originalSrc.startsWith("https://cdn.shopify.com")) {
|
|
114
|
-
return optimizeShopify(originalSrc, width, height);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
let imageSource = originalSrc.replace(DECO_CACHE_URL, "").replace(S3_URL, "").split("?")[0];
|
|
118
|
-
|
|
119
|
-
// Already on the image CDN — strip the host so we don't proxy through ourselves.
|
|
120
|
-
const cdnPrefix = `https://${imageCdnDomain}/`;
|
|
121
|
-
if (imageSource.startsWith(cdnPrefix)) {
|
|
122
|
-
imageSource = imageSource.slice(cdnPrefix.length);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const params = new URLSearchParams();
|
|
126
|
-
params.set("fit", fit);
|
|
127
|
-
params.set("width", `${width}`);
|
|
128
|
-
if (height) params.set("height", `${height}`);
|
|
129
|
-
|
|
130
|
-
return `https://${imageCdnDomain}/image?${params}&src=${imageSource}`;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Generates a srcset string with responsive multipliers.
|
|
135
|
-
*/
|
|
136
|
-
export function getSrcSet(
|
|
137
|
-
originalSrc: string,
|
|
138
|
-
width: number,
|
|
139
|
-
height?: number,
|
|
140
|
-
fit?: FitOptions,
|
|
141
|
-
factors: number[] = FACTORS,
|
|
142
|
-
): string | undefined {
|
|
143
|
-
if (typeof originalSrc !== "string" || originalSrc.length === 0) {
|
|
144
|
-
return undefined;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const entries: string[] = [];
|
|
148
|
-
|
|
149
|
-
for (const factor of factors) {
|
|
150
|
-
const w = Math.trunc(factor * width);
|
|
151
|
-
const h = height ? Math.trunc(factor * height) : undefined;
|
|
152
|
-
|
|
153
|
-
const src = getOptimizedMediaUrl({
|
|
154
|
-
originalSrc,
|
|
155
|
-
width: w,
|
|
156
|
-
height: h,
|
|
157
|
-
fit: fit ?? "cover",
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
if (src) {
|
|
161
|
-
entries.push(`${src} ${w}w`);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return entries.length > 0 ? entries.join(", ") : undefined;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// -------------------------------------------------------------------------
|
|
169
|
-
// Image component
|
|
170
|
-
// -------------------------------------------------------------------------
|
|
171
|
-
|
|
172
|
-
export interface ImageProps
|
|
173
|
-
extends Omit<ImgHTMLAttributes<HTMLImageElement>, "src" | "width" | "height"> {
|
|
174
|
-
src: string;
|
|
175
|
-
/** @description Improves Web Vitals (CLS/LCP) */
|
|
176
|
-
width: number;
|
|
177
|
-
/** @description Improves Web Vitals (CLS/LCP) */
|
|
178
|
-
height?: number;
|
|
179
|
-
/** @description Object-fit */
|
|
180
|
-
fit?: FitOptions;
|
|
181
|
-
/**
|
|
182
|
-
* @description Web Vitals (LCP). Injects a `<link rel="preload">` tag
|
|
183
|
-
* alongside the `<img>`, sets `fetchPriority="high"` and `loading="eager"`.
|
|
184
|
-
* Use once per page for the LCP image.
|
|
185
|
-
*/
|
|
186
|
-
preload?: boolean;
|
|
187
|
-
/** @description Media query for responsive preloading (e.g. "(min-width: 768px)") */
|
|
188
|
-
media?: string;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export const Image = forwardRef<HTMLImageElement, ImageProps>(function Image(
|
|
192
|
-
{
|
|
193
|
-
src,
|
|
194
|
-
width,
|
|
195
|
-
height,
|
|
196
|
-
fit = "cover",
|
|
197
|
-
preload,
|
|
198
|
-
media,
|
|
199
|
-
loading,
|
|
200
|
-
decoding,
|
|
201
|
-
srcSet: srcSetProp,
|
|
202
|
-
sizes,
|
|
203
|
-
fetchPriority,
|
|
204
|
-
...rest
|
|
205
|
-
},
|
|
206
|
-
ref,
|
|
207
|
-
) {
|
|
208
|
-
if (!height && typeof process !== "undefined") {
|
|
209
|
-
console.warn(`Missing height. This image will NOT be optimized: ${src}`);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const optimizedSrc = getOptimizedMediaUrl({
|
|
213
|
-
originalSrc: src,
|
|
214
|
-
width,
|
|
215
|
-
height,
|
|
216
|
-
fit,
|
|
217
|
-
});
|
|
218
|
-
const srcSet = srcSetProp ?? getSrcSet(src, width, height, fit);
|
|
219
|
-
const resolvedSizes = srcSet ? (sizes ?? "(max-width: 768px) 100vw, 50vw") : undefined;
|
|
220
|
-
|
|
221
|
-
return (
|
|
222
|
-
<>
|
|
223
|
-
{preload && (
|
|
224
|
-
<link
|
|
225
|
-
as="image"
|
|
226
|
-
rel="preload"
|
|
227
|
-
href={optimizedSrc}
|
|
228
|
-
imageSrcSet={srcSet}
|
|
229
|
-
imageSizes={resolvedSizes}
|
|
230
|
-
fetchPriority={fetchPriority ?? "high"}
|
|
231
|
-
media={media}
|
|
232
|
-
/>
|
|
233
|
-
)}
|
|
234
|
-
<img
|
|
235
|
-
{...rest}
|
|
236
|
-
src={optimizedSrc}
|
|
237
|
-
srcSet={srcSet}
|
|
238
|
-
sizes={resolvedSizes}
|
|
239
|
-
width={width}
|
|
240
|
-
height={height}
|
|
241
|
-
loading={loading ?? (preload ? "eager" : "lazy")}
|
|
242
|
-
decoding={decoding ?? "async"}
|
|
243
|
-
fetchPriority={preload ? "high" : fetchPriority}
|
|
244
|
-
ref={ref}
|
|
245
|
-
/>
|
|
246
|
-
</>
|
|
247
|
-
);
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
export default Image;
|