@amplifyup/sdk 0.1.4 → 0.1.6

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/README.md CHANGED
@@ -9,24 +9,62 @@ Configure a `trackingId`, and optionally a `target` for which environment to use
9
9
 
10
10
  ## Environment targets
11
11
 
12
- Two environments — `local` and `production`. Legacy `dev` maps to `local`.
12
+ **Default: production (live).** Hosting your site on `localhost` does not switch environments.
13
13
 
14
- | Target | Layouts / config (Neon) | Events + personalize |
15
- |--------|-------------------------|----------------------|
16
- | `local` | `NEXT_PUBLIC_ORCHESTRATOR_URL` (e.g. `http://localhost:5001`) | `https://api-dev.amplifyup.ai` (AWS development) |
17
- | `production` | `https://amplifyup.ai` | `https://api.amplifyup.ai` |
14
+ Opt into `local` only when developing inside the AmplifyUp monorepo:
15
+
16
+ | Target | When | Layouts / config | Events + personalize |
17
+ |--------|------|------------------|----------------------|
18
+ | `production` (default) | Customer sites, staging, production | `https://amplifyup.ai` | `https://api.amplifyup.ai` |
19
+ | `local` (explicit) | AmplifyUp monorepo dev | `NEXT_PUBLIC_ORCHESTRATOR_URL` | `https://api-dev.amplifyup.ai` |
18
20
 
19
21
  ```bash
20
- # Demo on http://localhost:5000 — pages from local Neon, events to AWS-dev
22
+ # AmplifyUp monorepo only (apps/demo, apps/orchestrator locally)
21
23
  NEXT_PUBLIC_ORCHESTRATOR_URL=http://localhost:5001
22
24
  NEXT_PUBLIC_AMPLIFYUP_TARGET=local
23
25
 
24
- # Optional: override AWS API base used by local (default api-dev)
25
- # NEXT_PUBLIC_AMPLIFYUP_API_URL=https://api-dev.amplifyup.ai
26
+ # Customer sites: omit NEXT_PUBLIC_AMPLIFYUP_TARGET — always live
26
27
  ```
27
28
 
28
29
  Check active URLs in the browser: `AmplifyUp.getStatus()`
29
30
 
31
+ ### Hybrid pages (native content + Composer)
32
+
33
+ For pages with native content that should only appear for real visitors:
34
+
35
+ ```tsx
36
+ // app/insights/page.tsx (server)
37
+ import { isPreviewRequest } from '@amplifyup/sdk';
38
+ import { AmplifyPageContent } from '@amplifyup/sdk/react';
39
+
40
+ export default async function InsightsPage({ searchParams }) {
41
+ const params = await searchParams;
42
+
43
+ if (isPreviewRequest(params)) {
44
+ // Composer: no blog fetch, no native fallback HTML
45
+ return (
46
+ <Layout>
47
+ <AmplifyPageContent renderComponent={renderAmplifyComponent} />
48
+ </Layout>
49
+ );
50
+ }
51
+
52
+ const posts = await getPosts();
53
+ return (
54
+ <Layout>
55
+ <PostsProvider value={posts}>
56
+ <AmplifyPageContent
57
+ fallback={<InsightsFallback />}
58
+ renderComponent={renderAmplifyComponent}
59
+ />
60
+ </PostsProvider>
61
+ </Layout>
62
+ );
63
+ }
64
+ ```
65
+
66
+ In Composer preview, `AmplifyPageContent` shows an empty layout canvas — never `fallback`.
67
+
30
68
  ## Installation
31
69
 
32
70
  ```bash
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { ReactNode } from 'react';
3
- import { P as PageConfig } from './types-Xv9P55Dj.mjs';
3
+ import { P as PageConfig } from './types-B2Swk8BW.mjs';
4
4
 
5
5
  interface AmplifyRendererProps {
6
6
  /**
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { ReactNode } from 'react';
3
- import { P as PageConfig } from './types-Xv9P55Dj.js';
3
+ import { P as PageConfig } from './types-B2Swk8BW.js';
4
4
 
5
5
  interface AmplifyRendererProps {
6
6
  /**
@@ -242,14 +242,16 @@ function resolveAmplifyUpTarget(options) {
242
242
  if (isAmplifyUpTarget(envTarget)) {
243
243
  return normalizeAmplifyUpTarget(envTarget);
244
244
  }
245
- if (typeof window !== "undefined") {
246
- const host = window.location.hostname;
247
- if (host === "localhost" || host === "127.0.0.1") {
248
- return "local";
249
- }
250
- }
251
245
  return "production";
252
246
  }
247
+ function isRemoteOrchestratorUrl(orchestratorUrl) {
248
+ try {
249
+ const host = new URL(orchestratorUrl).hostname.toLowerCase();
250
+ return host !== "localhost" && host !== "127.0.0.1" && host !== "::1";
251
+ } catch {
252
+ return false;
253
+ }
254
+ }
253
255
  function localOrchestratorUrl() {
254
256
  const fromEnv = typeof process !== "undefined" ? process.env.NEXT_PUBLIC_ORCHESTRATOR_URL : void 0;
255
257
  return (fromEnv || "http://localhost:5001").replace(/\/$/, "");
@@ -405,6 +407,7 @@ var AmplifyUpSDK = class {
405
407
  */
406
408
  async init(config) {
407
409
  if (this.initialized && this.adapter?.isInitialized()) {
410
+ this.ensureWindowBridge();
408
411
  return;
409
412
  }
410
413
  if (this.initPromise) {
@@ -417,6 +420,32 @@ var AmplifyUpSDK = class {
417
420
  this.initPromise = null;
418
421
  }
419
422
  }
423
+ /** Attach console helpers to window (idempotent; survives Fast Refresh). */
424
+ ensureWindowBridge() {
425
+ if (typeof window === "undefined") return;
426
+ window.AmplifyUp = {
427
+ getInstanceId: () => this.getInstanceId(),
428
+ getUserId: () => this.getUserId(),
429
+ getTrackingId: () => this.getTrackingId(),
430
+ getTarget: () => this.getTarget(),
431
+ getDiagnostics: () => this.getDiagnostics(),
432
+ getStatus: () => {
433
+ console.log(this.getStatus());
434
+ return this.getDiagnostics();
435
+ },
436
+ testTracking: () => this.testTracking(),
437
+ verifyAWSConnection: () => {
438
+ const result = this.verifyAWSConnection();
439
+ console.log("AWS Connection Verification:", result.message);
440
+ console.log("Full details:", result);
441
+ return result;
442
+ },
443
+ isInitialized: () => this.isInitialized(),
444
+ page: (url, properties) => this.page(url, properties),
445
+ track: (eventName, properties) => this.track(eventName, properties),
446
+ identify: (userId, traits) => this.identify(userId, traits)
447
+ };
448
+ }
420
449
  async performInit(config) {
421
450
  if (!this.adapter) {
422
451
  this.adapter = new AWSAdapter();
@@ -432,6 +461,12 @@ var AmplifyUpSDK = class {
432
461
  const previewOverrides = readPreviewQueryOverrides();
433
462
  if (previewOverrides.orchestratorUrl) {
434
463
  this.orchestratorUrl = previewOverrides.orchestratorUrl;
464
+ if (isRemoteOrchestratorUrl(previewOverrides.orchestratorUrl)) {
465
+ const prodUrls = resolveAmplifyUpUrls("production");
466
+ this.target = "production";
467
+ this.apiGatewayUrl = prodUrls.apiGatewayUrl;
468
+ this.decisionApiUrl = prodUrls.decisionApiUrl;
469
+ }
435
470
  }
436
471
  this.config = {
437
472
  trackingId: previewOverrides.trackingId || config.trackingId,
@@ -449,34 +484,10 @@ var AmplifyUpSDK = class {
449
484
  };
450
485
  await this.adapter.init(adapterConfig);
451
486
  this.initialized = this.adapter.isInitialized();
452
- if (typeof window !== "undefined") {
453
- window.AmplifyUp = {
454
- getInstanceId: () => this.getInstanceId(),
455
- getUserId: () => this.getUserId(),
456
- getTrackingId: () => this.getTrackingId(),
457
- getTarget: () => this.getTarget(),
458
- getDiagnostics: () => this.getDiagnostics(),
459
- getStatus: () => {
460
- console.log(this.getStatus());
461
- return this.getDiagnostics();
462
- },
463
- testTracking: () => this.testTracking(),
464
- verifyAWSConnection: () => {
465
- const result = this.verifyAWSConnection();
466
- console.log("AWS Connection Verification:", result.message);
467
- console.log("Full details:", result);
468
- return result;
469
- },
470
- isInitialized: () => this.isInitialized(),
471
- // Re-export main tracking methods for convenience
472
- page: (url, properties) => this.page(url, properties),
473
- track: (eventName, properties) => this.track(eventName, properties),
474
- identify: (userId, traits) => this.identify(userId, traits)
475
- };
476
- if (this.config?.debug) {
477
- console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
478
- console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
479
- }
487
+ this.ensureWindowBridge();
488
+ if (this.config?.debug) {
489
+ console.log("AmplifyUp SDK: Diagnostic methods available on window.AmplifyUp");
490
+ console.log("Try: AmplifyUp.getStatus() or AmplifyUp.testTracking()");
480
491
  }
481
492
  this.clearTrackingIntervals();
482
493
  if (this.isPreviewMode()) {
@@ -1487,10 +1498,17 @@ Available Console Commands:
1487
1498
  }
1488
1499
  };
1489
1500
  var sdkInstance = null;
1501
+ if (typeof window !== "undefined" && window.__AMPLIFYUP_SDK__) {
1502
+ sdkInstance = window.__AMPLIFYUP_SDK__;
1503
+ }
1490
1504
  function getSDK(adapter) {
1491
1505
  if (!sdkInstance) {
1492
1506
  sdkInstance = new AmplifyUpSDK(adapter);
1507
+ if (typeof window !== "undefined") {
1508
+ window.__AMPLIFYUP_SDK__ = sdkInstance;
1509
+ }
1493
1510
  }
1511
+ sdkInstance.ensureWindowBridge();
1494
1512
  return sdkInstance;
1495
1513
  }
1496
1514
  var trackContentView = (contentId, contentType, properties) => getSDK().trackContentView(contentId, contentType, properties);