@plasmicapp/loader-fetcher 1.0.16 → 1.0.18

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/dist/api.d.ts CHANGED
@@ -86,6 +86,12 @@ export interface LoaderBundleOutput {
86
86
  globalGroups: GlobalGroupMeta[];
87
87
  projects: ProjectMeta[];
88
88
  activeSplits: Split[];
89
+ /**
90
+ * ID used to identify LoaderBundleOutputs locally within a process.
91
+ *
92
+ * This property may be unset or `undefined` to indicate an "empty" LoaderBundleOutput.
93
+ */
94
+ localId?: number | undefined;
89
95
  }
90
96
  export interface LoaderHtmlOutput {
91
97
  html: string;
package/dist/fetcher.d.ts CHANGED
@@ -21,5 +21,7 @@ export declare class PlasmicModulesFetcher {
21
21
  private curFetch;
22
22
  constructor(opts: FetcherOptions);
23
23
  fetchAllData(): Promise<LoaderBundleOutput>;
24
+ private getCachedOrFetch;
24
25
  private doFetch;
26
+ private storeGlobally;
25
27
  }
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
6
 
7
- var fetch = _interopDefault(require('isomorphic-unfetch'));
7
+ var fetch = _interopDefault(require('@plasmicapp/isomorphic-unfetch'));
8
8
 
9
9
  const VERSION = '9';
10
10
  const isBrowser = typeof window !== 'undefined' && window != null && typeof window.document !== 'undefined';
@@ -78,6 +78,19 @@ class Api {
78
78
  }
79
79
  }
80
80
 
81
+ /**
82
+ * Returns an ID that is locally unique within a process.
83
+ *
84
+ * This function works even if this function's module is reloaded within the same process.
85
+ * It depends on setting state in the environment's `globalThis` variable.
86
+ */
87
+ function uniqueLocalId() {
88
+ const global = globalThis;
89
+ const localId = global.__PLASMIC_NEXT_LOCAL_ID ? global.__PLASMIC_NEXT_LOCAL_ID : 1;
90
+ global.__PLASMIC_NEXT_LOCAL_ID = localId + 1;
91
+ return localId;
92
+ }
93
+
81
94
  class PlasmicModulesFetcher {
82
95
  constructor(opts) {
83
96
  this.opts = opts;
@@ -88,6 +101,13 @@ class PlasmicModulesFetcher {
88
101
  });
89
102
  }
90
103
  async fetchAllData() {
104
+ const bundle = await this.getCachedOrFetch();
105
+ // Assign a local ID AFTER caching. If the local ID were cached, it may no longer be unique.
106
+ bundle.localId = uniqueLocalId();
107
+ this.storeGlobally(bundle);
108
+ return bundle;
109
+ }
110
+ async getCachedOrFetch() {
91
111
  if (this.opts.cache) {
92
112
  const cachedData = await this.opts.cache.get();
93
113
  if (cachedData) {
@@ -116,7 +136,23 @@ class PlasmicModulesFetcher {
116
136
  console.debug(`Plasmic: fetched designs for ${data.projects.map(p => `"${p.name}" (${p.id}@${p.version})`).join(', ')}`);
117
137
  return data;
118
138
  }
139
+ // For React Server Components (Next.js 13+),
140
+ // we need to pass server modules in LoaderBundleOutput from Server Components to Client Components.
141
+ // We don't want to pass them via normal page props because that will be serialized to the browser.
142
+ // Instead, we pass the bundle (including the server modules) via the Node `global` variable.
143
+ //
144
+ // This is the code that stores the bundle.
145
+ storeGlobally(bundle) {
146
+ if (bundle.localId === undefined) {
147
+ return;
148
+ }
149
+ if (global.__PLASMIC_BUNDLES === undefined) {
150
+ global.__PLASMIC_BUNDLES = {};
151
+ }
152
+ global.__PLASMIC_BUNDLES[bundle.localId] = bundle;
153
+ }
119
154
  }
155
+ const global = globalThis;
120
156
 
121
157
  exports.Api = Api;
122
158
  exports.PlasmicModulesFetcher = PlasmicModulesFetcher;
@@ -1 +1 @@
1
- {"version":3,"file":"loader-fetcher.cjs.development.js","sources":["../src/api.ts","../src/fetcher.ts"],"sourcesContent":["import fetch from 'isomorphic-unfetch';\n\nexport interface ComponentMeta {\n id: string;\n usedComponents: string[];\n projectId: string;\n name: string;\n displayName: string;\n cssFile: string;\n path: string | undefined;\n isPage: boolean;\n plumeType?: string;\n entry: string;\n isCode: boolean;\n isGlobalContextProvider: boolean;\n pageMetadata?: PageMetadata;\n metadata?: Record<string, string>;\n}\n\nexport interface PageMeta extends ComponentMeta {\n isPage: true;\n path: string;\n plumeType: never;\n pageMetadata: PageMetadata;\n}\n\nexport interface PageMetadata {\n path: string;\n title?: string | null;\n description?: string | null;\n openGraphImageUrl?: string | null;\n}\n\nexport interface GlobalGroupMeta {\n id: string;\n projectId: string;\n name: string;\n type: string;\n contextFile: string;\n useName: string;\n}\n\nexport interface ProjectMeta {\n id: string;\n teamId?: string;\n indirect?: boolean;\n name: string;\n version: string;\n remoteFonts: FontMeta[];\n globalContextsProviderFileName: string;\n}\n\nexport interface FontMeta {\n url: string;\n}\n\ninterface GlobalVariantSplitContent {\n type: 'global-variant';\n projectId: string;\n group: string;\n variant: string;\n}\n\ninterface Slice {\n id: string;\n contents: GlobalVariantSplitContent[];\n externalId?: string;\n}\n\nexport interface ExperimentSlice extends Slice {\n prob: number;\n}\n\nexport interface SegmentSlice extends Slice {\n cond: any;\n}\n\nexport interface ExperimentSplit {\n id: string;\n externalId?: string;\n type: 'experiment';\n slices: ExperimentSlice[];\n}\n\nexport interface SegmentSplit {\n id: string;\n externalId?: string;\n type: 'segment';\n slices: SegmentSlice[];\n}\n\nexport type Split = ExperimentSplit | SegmentSplit;\n\nexport interface LoaderBundleOutput {\n modules: {\n browser: (CodeModule | AssetModule)[];\n server: (CodeModule | AssetModule)[];\n };\n external: string[];\n components: ComponentMeta[];\n globalGroups: GlobalGroupMeta[];\n projects: ProjectMeta[];\n activeSplits: Split[];\n}\n\nexport interface LoaderHtmlOutput {\n html: string;\n}\n\nexport interface CodeModule {\n fileName: string;\n code: string;\n imports: string[];\n type: 'code';\n}\n\nexport interface AssetModule {\n fileName: string;\n source: string;\n type: 'asset';\n}\n\nconst VERSION = '9';\n\nexport const isBrowser =\n typeof window !== 'undefined' &&\n window != null &&\n typeof window.document !== 'undefined';\n\nexport class Api {\n private host: string;\n constructor(\n private opts: {\n projects: { id: string; token: string }[];\n host?: string;\n }\n ) {\n this.host = opts.host ?? 'https://codegen.plasmic.app';\n }\n\n async fetchLoaderData(\n projectIds: string[],\n opts: {\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n browserOnly?: boolean;\n i18nKeyScheme?: 'content' | 'hash';\n }\n ) {\n const { platform, preview } = opts;\n const query = new URLSearchParams([\n ['platform', platform ?? 'react'],\n ...projectIds.map((projectId) => ['projectId', projectId]),\n ...(opts.browserOnly ? [['browserOnly', 'true']] : []),\n ...(opts.i18nKeyScheme ? [['i18nKeyScheme', opts.i18nKeyScheme]] : []),\n ]).toString();\n\n const url = `${this.host}/api/v1/loader/code/${\n preview ? 'preview' : 'published'\n }?${query}`;\n const resp = await fetch(url, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n if (resp.status >= 400) {\n const error = await this.parseJsonResponse(resp);\n throw new Error(\n `Error fetching loader data: ${\n error?.error?.message ?? resp.statusText\n }`\n );\n }\n const json = await this.parseJsonResponse(resp);\n return json as LoaderBundleOutput;\n }\n\n private async parseJsonResponse(resp: Response) {\n const text = await resp.text();\n try {\n return JSON.parse(text);\n } catch (err) {\n throw new Error(`Error parsing JSON response: ${err}; response: ${text}`);\n }\n }\n\n async fetchHtmlData(opts: {\n projectId: string;\n component: string;\n hydrate?: boolean;\n embedHydrate?: boolean;\n }) {\n const { projectId, component, embedHydrate, hydrate } = opts;\n const query = new URLSearchParams([\n ['projectId', projectId],\n ['component', component],\n ['embedHydrate', embedHydrate ? '1' : '0'],\n ['hydrate', hydrate ? '1' : '0'],\n ]).toString();\n const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n const json = await resp.json();\n return json as LoaderHtmlOutput;\n }\n\n private makeGetHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n ...this.makeAuthHeaders(),\n };\n }\n\n // @ts-ignore\n private makePostHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n 'Content-Type': 'application/json',\n ...this.makeAuthHeaders(),\n };\n }\n\n private makeAuthHeaders() {\n const tokens = this.opts.projects\n .map((p) => `${p.id}:${p.token}`)\n .join(',');\n return {\n 'x-plasmic-api-project-tokens': tokens,\n };\n }\n}\n","import { Api, isBrowser, LoaderBundleOutput } from './api';\n\nexport interface FetcherOptions {\n projects: {\n id: string;\n version?: string;\n token: string;\n }[];\n cache?: LoaderBundleCache;\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n host?: string;\n i18nKeyScheme?: 'content' | 'hash';\n}\n\nexport interface LoaderBundleCache {\n set: (data: LoaderBundleOutput) => Promise<void>;\n get: () => Promise<LoaderBundleOutput>;\n}\n\nexport class PlasmicModulesFetcher {\n private api: Api;\n private curFetch: Promise<LoaderBundleOutput> | undefined = undefined;\n constructor(private opts: FetcherOptions) {\n this.api = new Api({\n projects: opts.projects,\n host: opts.host,\n });\n }\n\n async fetchAllData() {\n if (this.opts.cache) {\n const cachedData = await this.opts.cache.get();\n if (cachedData) {\n return cachedData;\n }\n }\n if (this.curFetch) {\n return await this.curFetch;\n }\n console.debug('Plasmic: doing a fresh fetch...');\n this.curFetch = this.doFetch();\n const data = await this.curFetch;\n this.curFetch = undefined;\n return data;\n }\n\n private async doFetch() {\n const data = await this.api.fetchLoaderData(\n this.opts.projects.map((p) =>\n p.version ? `${p.id}@${p.version}` : p.id\n ),\n {\n platform: this.opts.platform,\n preview: this.opts.preview,\n i18nKeyScheme: this.opts.i18nKeyScheme,\n browserOnly: isBrowser,\n }\n );\n if (this.opts.cache) {\n await this.opts.cache.set(data);\n }\n console.debug(\n `Plasmic: fetched designs for ${data.projects\n .map((p) => `\"${p.name}\" (${p.id}@${p.version})`)\n .join(', ')}`\n );\n return data;\n }\n}\n"],"names":["VERSION","isBrowser","window","document","Api","constructor","opts","host","fetchLoaderData","projectIds","platform","preview","query","URLSearchParams","map","projectId","browserOnly","i18nKeyScheme","toString","url","resp","fetch","method","headers","makeGetHeaders","status","error","parseJsonResponse","Error","message","statusText","json","text","JSON","parse","err","fetchHtmlData","component","embedHydrate","hydrate","makeAuthHeaders","makePostHeaders","tokens","projects","p","id","token","join","PlasmicModulesFetcher","undefined","api","fetchAllData","cache","cachedData","get","curFetch","console","debug","doFetch","data","version","set","name"],"mappings":";;;;;;;;AA0HA,MAAMA,OAAO,GAAG,GAAG;AAEZ,MAAMC,SAAS,GACpB,OAAOC,MAAM,KAAK,WAAW,IAC7BA,MAAM,IAAI,IAAI,IACd,OAAOA,MAAM,CAACC,QAAQ,KAAK,WAAW;MAE3BC,GAAG;EAEdC,YACUC,IAGP;;IAHO,SAAI,GAAJA,IAAI;IAKZ,IAAI,CAACC,IAAI,iBAAGD,IAAI,CAACC,IAAI,yBAAI,6BAA6B;;EAGxD,MAAMC,eAAe,CACnBC,UAAoB,EACpBH,IAKC;IAED,MAAM;MAAEI,QAAQ;MAAEC;KAAS,GAAGL,IAAI;IAClC,MAAMM,KAAK,GAAG,IAAIC,eAAe,CAAC,CAChC,CAAC,UAAU,EAAEH,QAAQ,WAARA,QAAQ,GAAI,OAAO,CAAC,EACjC,GAAGD,UAAU,CAACK,GAAG,CAAEC,SAAS,IAAK,CAAC,WAAW,EAAEA,SAAS,CAAC,CAAC,EAC1D,IAAIT,IAAI,CAACU,WAAW,GAAG,CAAC,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EACtD,IAAIV,IAAI,CAACW,aAAa,GAAG,CAAC,CAAC,eAAe,EAAEX,IAAI,CAACW,aAAa,CAAC,CAAC,GAAG,EAAE,CAAC,CACvE,CAAC,CAACC,QAAQ,EAAE;IAEb,MAAMC,GAAG,MAAM,IAAI,CAACZ,2BAClBI,OAAO,GAAG,SAAS,GAAG,eACpBC,OAAO;IACX,MAAMQ,IAAI,GAAG,MAAMC,KAAK,CAACF,GAAG,EAAE;MAC5BG,MAAM,EAAE,KAAK;MACbC,OAAO,EAAE,IAAI,CAACC,cAAc;KAC7B,CAAC;IACF,IAAIJ,IAAI,CAACK,MAAM,IAAI,GAAG,EAAE;MAA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8B,CAAC,IACvBA,CAAC,CAACgB,OAAO,MAAMhB,CAAC,CAACC,MAAMD,CAAC,CAACgB,SAAS,GAAGhB,CAAC,CAACC,EAAE,CAC1C,EACD;MACEnC,QAAQ,EAAE,IAAI,CAACJ,IAAI,CAACI,QAAQ;MAC5BC,OAAO,EAAE,IAAI,CAACL,IAAI,CAACK,OAAO;MAC1BM,aAAa,EAAE,IAAI,CAACX,IAAI,CAACW,aAAa;MACtCD,WAAW,EAAEf;KACd,CACF;IACD,IAAI,IAAI,CAACK,IAAI,CAAC8C,KAAK,EAAE;MACnB,MAAM,IAAI,CAAC9C,IAAI,CAAC8C,KAAK,CAACS,GAAG,CAACF,IAAI,CAAC;;IAEjCH,OAAO,CAACC,KAAK,iCACqBE,IAAI,CAAChB,QAAQ,CAC1C7B,GAAG,CAAE8B,CAAC,QAASA,CAAC,CAACkB,UAAUlB,CAAC,CAACC,MAAMD,CAAC,CAACgB,UAAU,CAAC,CAChDb,IAAI,CAAC,IAAI,GAAG,CAChB;IACD,OAAOY,IAAI;;;;;;;"}
1
+ {"version":3,"file":"loader-fetcher.cjs.development.js","sources":["../src/api.ts","../src/uniqueLocalId.ts","../src/fetcher.ts"],"sourcesContent":["import fetch from '@plasmicapp/isomorphic-unfetch';\n\nexport interface ComponentMeta {\n id: string;\n usedComponents: string[];\n projectId: string;\n name: string;\n displayName: string;\n cssFile: string;\n path: string | undefined;\n isPage: boolean;\n plumeType?: string;\n entry: string;\n isCode: boolean;\n isGlobalContextProvider: boolean;\n pageMetadata?: PageMetadata;\n metadata?: Record<string, string>;\n}\n\nexport interface PageMeta extends ComponentMeta {\n isPage: true;\n path: string;\n plumeType: never;\n pageMetadata: PageMetadata;\n}\n\nexport interface PageMetadata {\n path: string;\n title?: string | null;\n description?: string | null;\n openGraphImageUrl?: string | null;\n}\n\nexport interface GlobalGroupMeta {\n id: string;\n projectId: string;\n name: string;\n type: string;\n contextFile: string;\n useName: string;\n}\n\nexport interface ProjectMeta {\n id: string;\n teamId?: string;\n indirect?: boolean;\n name: string;\n version: string;\n remoteFonts: FontMeta[];\n globalContextsProviderFileName: string;\n}\n\nexport interface FontMeta {\n url: string;\n}\n\ninterface GlobalVariantSplitContent {\n type: 'global-variant';\n projectId: string;\n group: string;\n variant: string;\n}\n\ninterface Slice {\n id: string;\n contents: GlobalVariantSplitContent[];\n externalId?: string;\n}\n\nexport interface ExperimentSlice extends Slice {\n prob: number;\n}\n\nexport interface SegmentSlice extends Slice {\n cond: any;\n}\n\nexport interface ExperimentSplit {\n id: string;\n externalId?: string;\n type: 'experiment';\n slices: ExperimentSlice[];\n}\n\nexport interface SegmentSplit {\n id: string;\n externalId?: string;\n type: 'segment';\n slices: SegmentSlice[];\n}\n\nexport type Split = ExperimentSplit | SegmentSplit;\n\nexport interface LoaderBundleOutput {\n modules: {\n browser: (CodeModule | AssetModule)[];\n server: (CodeModule | AssetModule)[];\n };\n external: string[];\n components: ComponentMeta[];\n globalGroups: GlobalGroupMeta[];\n projects: ProjectMeta[];\n activeSplits: Split[];\n\n // Note this property doesn't come from the API but is generated locally by this module.\n /**\n * ID used to identify LoaderBundleOutputs locally within a process.\n *\n * This property may be unset or `undefined` to indicate an \"empty\" LoaderBundleOutput.\n */\n localId?: number | undefined;\n}\n\nexport interface LoaderHtmlOutput {\n html: string;\n}\n\nexport interface CodeModule {\n fileName: string;\n code: string;\n imports: string[];\n type: 'code';\n}\n\nexport interface AssetModule {\n fileName: string;\n source: string;\n type: 'asset';\n}\n\nconst VERSION = '9';\n\nexport const isBrowser =\n typeof window !== 'undefined' &&\n window != null &&\n typeof window.document !== 'undefined';\n\nexport class Api {\n private host: string;\n constructor(\n private opts: {\n projects: { id: string; token: string }[];\n host?: string;\n }\n ) {\n this.host = opts.host ?? 'https://codegen.plasmic.app';\n }\n\n async fetchLoaderData(\n projectIds: string[],\n opts: {\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n browserOnly?: boolean;\n i18nKeyScheme?: 'content' | 'hash';\n }\n ) {\n const { platform, preview } = opts;\n const query = new URLSearchParams([\n ['platform', platform ?? 'react'],\n ...projectIds.map((projectId) => ['projectId', projectId]),\n ...(opts.browserOnly ? [['browserOnly', 'true']] : []),\n ...(opts.i18nKeyScheme ? [['i18nKeyScheme', opts.i18nKeyScheme]] : []),\n ]).toString();\n\n const url = `${this.host}/api/v1/loader/code/${\n preview ? 'preview' : 'published'\n }?${query}`;\n const resp = await fetch(url, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n if (resp.status >= 400) {\n const error = await this.parseJsonResponse(resp);\n throw new Error(\n `Error fetching loader data: ${\n error?.error?.message ?? resp.statusText\n }`\n );\n }\n const json = await this.parseJsonResponse(resp);\n return json as LoaderBundleOutput;\n }\n\n private async parseJsonResponse(resp: Response) {\n const text = await resp.text();\n try {\n return JSON.parse(text);\n } catch (err) {\n throw new Error(`Error parsing JSON response: ${err}; response: ${text}`);\n }\n }\n\n async fetchHtmlData(opts: {\n projectId: string;\n component: string;\n hydrate?: boolean;\n embedHydrate?: boolean;\n }) {\n const { projectId, component, embedHydrate, hydrate } = opts;\n const query = new URLSearchParams([\n ['projectId', projectId],\n ['component', component],\n ['embedHydrate', embedHydrate ? '1' : '0'],\n ['hydrate', hydrate ? '1' : '0'],\n ]).toString();\n const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n const json = await resp.json();\n return json as LoaderHtmlOutput;\n }\n\n private makeGetHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n ...this.makeAuthHeaders(),\n };\n }\n\n // @ts-ignore\n private makePostHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n 'Content-Type': 'application/json',\n ...this.makeAuthHeaders(),\n };\n }\n\n private makeAuthHeaders() {\n const tokens = this.opts.projects\n .map((p) => `${p.id}:${p.token}`)\n .join(',');\n return {\n 'x-plasmic-api-project-tokens': tokens,\n };\n }\n}\n","interface GlobalWithNextLocalId {\n __PLASMIC_NEXT_LOCAL_ID?: number;\n}\n\n/**\n * Returns an ID that is locally unique within a process.\n *\n * This function works even if this function's module is reloaded within the same process.\n * It depends on setting state in the environment's `globalThis` variable.\n */\nexport function uniqueLocalId(): number {\n const global = globalThis as GlobalWithNextLocalId;\n const localId = global.__PLASMIC_NEXT_LOCAL_ID\n ? global.__PLASMIC_NEXT_LOCAL_ID\n : 1;\n global.__PLASMIC_NEXT_LOCAL_ID = localId + 1;\n return localId;\n}\n","import { Api, isBrowser, LoaderBundleOutput } from './api';\nimport { uniqueLocalId } from './uniqueLocalId';\n\nexport interface FetcherOptions {\n projects: {\n id: string;\n version?: string;\n token: string;\n }[];\n cache?: LoaderBundleCache;\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n host?: string;\n i18nKeyScheme?: 'content' | 'hash';\n}\n\nexport interface LoaderBundleCache {\n set: (data: LoaderBundleOutput) => Promise<void>;\n get: () => Promise<LoaderBundleOutput>;\n}\n\nexport class PlasmicModulesFetcher {\n private api: Api;\n private curFetch: Promise<LoaderBundleOutput> | undefined = undefined;\n constructor(private opts: FetcherOptions) {\n this.api = new Api({\n projects: opts.projects,\n host: opts.host,\n });\n }\n\n async fetchAllData() {\n const bundle = await this.getCachedOrFetch();\n\n // Assign a local ID AFTER caching. If the local ID were cached, it may no longer be unique.\n bundle.localId = uniqueLocalId();\n this.storeGlobally(bundle);\n\n return bundle;\n }\n\n private async getCachedOrFetch() {\n if (this.opts.cache) {\n const cachedData = await this.opts.cache.get();\n if (cachedData) {\n return cachedData;\n }\n }\n if (this.curFetch) {\n return await this.curFetch;\n }\n console.debug('Plasmic: doing a fresh fetch...');\n this.curFetch = this.doFetch();\n const data = await this.curFetch;\n this.curFetch = undefined;\n return data;\n }\n\n private async doFetch() {\n const data = await this.api.fetchLoaderData(\n this.opts.projects.map((p) =>\n p.version ? `${p.id}@${p.version}` : p.id\n ),\n {\n platform: this.opts.platform,\n preview: this.opts.preview,\n i18nKeyScheme: this.opts.i18nKeyScheme,\n browserOnly: isBrowser,\n }\n );\n if (this.opts.cache) {\n await this.opts.cache.set(data);\n }\n console.debug(\n `Plasmic: fetched designs for ${data.projects\n .map((p) => `\"${p.name}\" (${p.id}@${p.version})`)\n .join(', ')}`\n );\n return data;\n }\n\n // For React Server Components (Next.js 13+),\n // we need to pass server modules in LoaderBundleOutput from Server Components to Client Components.\n // We don't want to pass them via normal page props because that will be serialized to the browser.\n // Instead, we pass the bundle (including the server modules) via the Node `global` variable.\n //\n // This is the code that stores the bundle.\n private storeGlobally(bundle: LoaderBundleOutput) {\n if (bundle.localId === undefined) {\n return;\n }\n\n if (global.__PLASMIC_BUNDLES === undefined) {\n global.__PLASMIC_BUNDLES = {};\n }\n global.__PLASMIC_BUNDLES[bundle.localId] = bundle;\n }\n}\n\ninterface GlobalWithBundles {\n __PLASMIC_BUNDLES?: { [localId: number]: LoaderBundleOutput };\n}\nconst global = globalThis as GlobalWithBundles;\n"],"names":["VERSION","isBrowser","window","document","Api","constructor","opts","host","fetchLoaderData","projectIds","platform","preview","query","URLSearchParams","map","projectId","browserOnly","i18nKeyScheme","toString","url","resp","fetch","method","headers","makeGetHeaders","status","error","parseJsonResponse","Error","message","statusText","json","text","JSON","parse","err","fetchHtmlData","component","embedHydrate","hydrate","makeAuthHeaders","makePostHeaders","tokens","projects","p","id","token","join","uniqueLocalId","global","globalThis","localId","__PLASMIC_NEXT_LOCAL_ID","PlasmicModulesFetcher","undefined","api","fetchAllData","bundle","getCachedOrFetch","storeGlobally","cache","cachedData","get","curFetch","console","debug","doFetch","data","version","set","name","__PLASMIC_BUNDLES"],"mappings":";;;;;;;;AAkIA,MAAMA,OAAO,GAAG,GAAG;AAEZ,MAAMC,SAAS,GACpB,OAAOC,MAAM,KAAK,WAAW,IAC7BA,MAAM,IAAI,IAAI,IACd,OAAOA,MAAM,CAACC,QAAQ,KAAK,WAAW;MAE3BC,GAAG;EAEdC,YACUC,IAGP;;IAHO,SAAI,GAAJA,IAAI;IAKZ,IA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zC,IAAI,CAACF,IAAI,CAACqC,QAAQ,CAAC7B,GAAG,CAAE8B,CAAC,IACvBA,CAAC,CAACwB,OAAO,MAAMxB,CAAC,CAACC,MAAMD,CAAC,CAACwB,SAAS,GAAGxB,CAAC,CAACC,EAAE,CAC1C,EACD;MACEnC,QAAQ,EAAE,IAAI,CAACJ,IAAI,CAACI,QAAQ;MAC5BC,OAAO,EAAE,IAAI,CAACL,IAAI,CAACK,OAAO;MAC1BM,aAAa,EAAE,IAAI,CAACX,IAAI,CAACW,aAAa;MACtCD,WAAW,EAAEf;KACd,CACF;IACD,IAAI,IAAI,CAACK,IAAI,CAACsD,KAAK,EAAE;MACnB,MAAM,IAAI,CAACtD,IAAI,CAACsD,KAAK,CAACS,GAAG,CAACF,IAAI,CAAC;;IAEjCH,OAAO,CAACC,KAAK,iCACqBE,IAAI,CAACxB,QAAQ,CAC1C7B,GAAG,CAAE8B,CAAC,QAASA,CAAC,CAAC0B,UAAU1B,CAAC,CAACC,MAAMD,CAAC,CAACwB,UAAU,CAAC,CAChDrB,IAAI,CAAC,IAAI,GAAG,CAChB;IACD,OAAOoB,IAAI;;;;;;;;EASLR,aAAa,CAACF,MAA0B;IAC9C,IAAIA,MAAM,CAACN,OAAO,KAAKG,SAAS,EAAE;MAChC;;IAGF,IAAIL,MAAM,CAACsB,iBAAiB,KAAKjB,SAAS,EAAE;MAC1CL,MAAM,CAACsB,iBAAiB,GAAG,EAAE;;IAE/BtB,MAAM,CAACsB,iBAAiB,CAACd,MAAM,CAACN,OAAO,CAAC,GAAGM,MAAM;;;AAOrD,MAAMR,MAAM,GAAGC,UAA+B;;;;;"}
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("isomorphic-unfetch"))&&"object"==typeof e&&"default"in e?e.default:e;const s="undefined"!=typeof window&&null!=window&&void 0!==window.document;class r{constructor(e){var t;this.opts=e,this.host=null!=(t=e.host)?t:"https://codegen.plasmic.app"}async fetchLoaderData(e,s){const{platform:r,preview:o}=s,a=new URLSearchParams([["platform",null!=r?r:"react"],...e.map(e=>["projectId",e]),...s.browserOnly?[["browserOnly","true"]]:[],...s.i18nKeyScheme?[["i18nKeyScheme",s.i18nKeyScheme]]:[]]).toString(),i=`${this.host}/api/v1/loader/code/${o?"preview":"published"}?${a}`,n=await t(i,{method:"GET",headers:this.makeGetHeaders()});if(n.status>=400){var c,h;const e=await this.parseJsonResponse(n);throw new Error("Error fetching loader data: "+(null!=(c=null==e||null==(h=e.error)?void 0:h.message)?c:n.statusText))}return await this.parseJsonResponse(n)}async parseJsonResponse(e){const t=await e.text();try{return JSON.parse(t)}catch(e){throw new Error(`Error parsing JSON response: ${e}; response: ${t}`)}}async fetchHtmlData(e){const{projectId:s,component:r,embedHydrate:o,hydrate:a}=e,i=new URLSearchParams([["projectId",s],["component",r],["embedHydrate",o?"1":"0"],["hydrate",a?"1":"0"]]).toString(),n=await t(`${this.host}/api/v1/loader/html?${i}`,{method:"GET",headers:this.makeGetHeaders()});return await n.json()}makeGetHeaders(){return{"x-plasmic-loader-version":"9",...this.makeAuthHeaders()}}makePostHeaders(){return{"x-plasmic-loader-version":"9","Content-Type":"application/json",...this.makeAuthHeaders()}}makeAuthHeaders(){return{"x-plasmic-api-project-tokens":this.opts.projects.map(e=>`${e.id}:${e.token}`).join(",")}}}exports.Api=r,exports.PlasmicModulesFetcher=class{constructor(e){this.opts=e,this.curFetch=void 0,this.api=new r({projects:e.projects,host:e.host})}async fetchAllData(){if(this.opts.cache){const e=await this.opts.cache.get();if(e)return e}if(this.curFetch)return await this.curFetch;console.debug("Plasmic: doing a fresh fetch..."),this.curFetch=this.doFetch();const e=await this.curFetch;return this.curFetch=void 0,e}async doFetch(){const e=await this.api.fetchLoaderData(this.opts.projects.map(e=>e.version?`${e.id}@${e.version}`:e.id),{platform:this.opts.platform,preview:this.opts.preview,i18nKeyScheme:this.opts.i18nKeyScheme,browserOnly:s});return this.opts.cache&&await this.opts.cache.set(e),console.debug("Plasmic: fetched designs for "+e.projects.map(e=>`"${e.name}" (${e.id}@${e.version})`).join(", ")),e}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=(e=require("@plasmicapp/isomorphic-unfetch"))&&"object"==typeof e&&"default"in e?e.default:e;const s="undefined"!=typeof window&&null!=window&&void 0!==window.document;class r{constructor(e){var t;this.opts=e,this.host=null!=(t=e.host)?t:"https://codegen.plasmic.app"}async fetchLoaderData(e,s){const{platform:r,preview:o}=s,a=new URLSearchParams([["platform",null!=r?r:"react"],...e.map(e=>["projectId",e]),...s.browserOnly?[["browserOnly","true"]]:[],...s.i18nKeyScheme?[["i18nKeyScheme",s.i18nKeyScheme]]:[]]).toString(),i=`${this.host}/api/v1/loader/code/${o?"preview":"published"}?${a}`,c=await t(i,{method:"GET",headers:this.makeGetHeaders()});if(c.status>=400){var n,h;const e=await this.parseJsonResponse(c);throw new Error("Error fetching loader data: "+(null!=(n=null==e||null==(h=e.error)?void 0:h.message)?n:c.statusText))}return await this.parseJsonResponse(c)}async parseJsonResponse(e){const t=await e.text();try{return JSON.parse(t)}catch(e){throw new Error(`Error parsing JSON response: ${e}; response: ${t}`)}}async fetchHtmlData(e){const{projectId:s,component:r,embedHydrate:o,hydrate:a}=e,i=new URLSearchParams([["projectId",s],["component",r],["embedHydrate",o?"1":"0"],["hydrate",a?"1":"0"]]).toString(),c=await t(`${this.host}/api/v1/loader/html?${i}`,{method:"GET",headers:this.makeGetHeaders()});return await c.json()}makeGetHeaders(){return{"x-plasmic-loader-version":"9",...this.makeAuthHeaders()}}makePostHeaders(){return{"x-plasmic-loader-version":"9","Content-Type":"application/json",...this.makeAuthHeaders()}}makeAuthHeaders(){return{"x-plasmic-api-project-tokens":this.opts.projects.map(e=>`${e.id}:${e.token}`).join(",")}}}const o=globalThis;exports.Api=r,exports.PlasmicModulesFetcher=class{constructor(e){this.opts=e,this.curFetch=void 0,this.api=new r({projects:e.projects,host:e.host})}async fetchAllData(){const e=await this.getCachedOrFetch();return e.localId=function(){const e=globalThis,t=e.__PLASMIC_NEXT_LOCAL_ID?e.__PLASMIC_NEXT_LOCAL_ID:1;return e.__PLASMIC_NEXT_LOCAL_ID=t+1,t}(),this.storeGlobally(e),e}async getCachedOrFetch(){if(this.opts.cache){const e=await this.opts.cache.get();if(e)return e}if(this.curFetch)return await this.curFetch;console.debug("Plasmic: doing a fresh fetch..."),this.curFetch=this.doFetch();const e=await this.curFetch;return this.curFetch=void 0,e}async doFetch(){const e=await this.api.fetchLoaderData(this.opts.projects.map(e=>e.version?`${e.id}@${e.version}`:e.id),{platform:this.opts.platform,preview:this.opts.preview,i18nKeyScheme:this.opts.i18nKeyScheme,browserOnly:s});return this.opts.cache&&await this.opts.cache.set(e),console.debug("Plasmic: fetched designs for "+e.projects.map(e=>`"${e.name}" (${e.id}@${e.version})`).join(", ")),e}storeGlobally(e){void 0!==e.localId&&(void 0===o.__PLASMIC_BUNDLES&&(o.__PLASMIC_BUNDLES={}),o.__PLASMIC_BUNDLES[e.localId]=e)}};
2
2
  //# sourceMappingURL=loader-fetcher.cjs.production.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"loader-fetcher.cjs.production.min.js","sources":["../src/api.ts","../src/fetcher.ts"],"sourcesContent":["import fetch from 'isomorphic-unfetch';\n\nexport interface ComponentMeta {\n id: string;\n usedComponents: string[];\n projectId: string;\n name: string;\n displayName: string;\n cssFile: string;\n path: string | undefined;\n isPage: boolean;\n plumeType?: string;\n entry: string;\n isCode: boolean;\n isGlobalContextProvider: boolean;\n pageMetadata?: PageMetadata;\n metadata?: Record<string, string>;\n}\n\nexport interface PageMeta extends ComponentMeta {\n isPage: true;\n path: string;\n plumeType: never;\n pageMetadata: PageMetadata;\n}\n\nexport interface PageMetadata {\n path: string;\n title?: string | null;\n description?: string | null;\n openGraphImageUrl?: string | null;\n}\n\nexport interface GlobalGroupMeta {\n id: string;\n projectId: string;\n name: string;\n type: string;\n contextFile: string;\n useName: string;\n}\n\nexport interface ProjectMeta {\n id: string;\n teamId?: string;\n indirect?: boolean;\n name: string;\n version: string;\n remoteFonts: FontMeta[];\n globalContextsProviderFileName: string;\n}\n\nexport interface FontMeta {\n url: string;\n}\n\ninterface GlobalVariantSplitContent {\n type: 'global-variant';\n projectId: string;\n group: string;\n variant: string;\n}\n\ninterface Slice {\n id: string;\n contents: GlobalVariantSplitContent[];\n externalId?: string;\n}\n\nexport interface ExperimentSlice extends Slice {\n prob: number;\n}\n\nexport interface SegmentSlice extends Slice {\n cond: any;\n}\n\nexport interface ExperimentSplit {\n id: string;\n externalId?: string;\n type: 'experiment';\n slices: ExperimentSlice[];\n}\n\nexport interface SegmentSplit {\n id: string;\n externalId?: string;\n type: 'segment';\n slices: SegmentSlice[];\n}\n\nexport type Split = ExperimentSplit | SegmentSplit;\n\nexport interface LoaderBundleOutput {\n modules: {\n browser: (CodeModule | AssetModule)[];\n server: (CodeModule | AssetModule)[];\n };\n external: string[];\n components: ComponentMeta[];\n globalGroups: GlobalGroupMeta[];\n projects: ProjectMeta[];\n activeSplits: Split[];\n}\n\nexport interface LoaderHtmlOutput {\n html: string;\n}\n\nexport interface CodeModule {\n fileName: string;\n code: string;\n imports: string[];\n type: 'code';\n}\n\nexport interface AssetModule {\n fileName: string;\n source: string;\n type: 'asset';\n}\n\nconst VERSION = '9';\n\nexport const isBrowser =\n typeof window !== 'undefined' &&\n window != null &&\n typeof window.document !== 'undefined';\n\nexport class Api {\n private host: string;\n constructor(\n private opts: {\n projects: { id: string; token: string }[];\n host?: string;\n }\n ) {\n this.host = opts.host ?? 'https://codegen.plasmic.app';\n }\n\n async fetchLoaderData(\n projectIds: string[],\n opts: {\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n browserOnly?: boolean;\n i18nKeyScheme?: 'content' | 'hash';\n }\n ) {\n const { platform, preview } = opts;\n const query = new URLSearchParams([\n ['platform', platform ?? 'react'],\n ...projectIds.map((projectId) => ['projectId', projectId]),\n ...(opts.browserOnly ? [['browserOnly', 'true']] : []),\n ...(opts.i18nKeyScheme ? [['i18nKeyScheme', opts.i18nKeyScheme]] : []),\n ]).toString();\n\n const url = `${this.host}/api/v1/loader/code/${\n preview ? 'preview' : 'published'\n }?${query}`;\n const resp = await fetch(url, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n if (resp.status >= 400) {\n const error = await this.parseJsonResponse(resp);\n throw new Error(\n `Error fetching loader data: ${\n error?.error?.message ?? resp.statusText\n }`\n );\n }\n const json = await this.parseJsonResponse(resp);\n return json as LoaderBundleOutput;\n }\n\n private async parseJsonResponse(resp: Response) {\n const text = await resp.text();\n try {\n return JSON.parse(text);\n } catch (err) {\n throw new Error(`Error parsing JSON response: ${err}; response: ${text}`);\n }\n }\n\n async fetchHtmlData(opts: {\n projectId: string;\n component: string;\n hydrate?: boolean;\n embedHydrate?: boolean;\n }) {\n const { projectId, component, embedHydrate, hydrate } = opts;\n const query = new URLSearchParams([\n ['projectId', projectId],\n ['component', component],\n ['embedHydrate', embedHydrate ? '1' : '0'],\n ['hydrate', hydrate ? '1' : '0'],\n ]).toString();\n const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n const json = await resp.json();\n return json as LoaderHtmlOutput;\n }\n\n private makeGetHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n ...this.makeAuthHeaders(),\n };\n }\n\n // @ts-ignore\n private makePostHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n 'Content-Type': 'application/json',\n ...this.makeAuthHeaders(),\n };\n }\n\n private makeAuthHeaders() {\n const tokens = this.opts.projects\n .map((p) => `${p.id}:${p.token}`)\n .join(',');\n return {\n 'x-plasmic-api-project-tokens': tokens,\n };\n }\n}\n","import { Api, isBrowser, LoaderBundleOutput } from './api';\n\nexport interface FetcherOptions {\n projects: {\n id: string;\n version?: string;\n token: string;\n }[];\n cache?: LoaderBundleCache;\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n host?: string;\n i18nKeyScheme?: 'content' | 'hash';\n}\n\nexport interface LoaderBundleCache {\n set: (data: LoaderBundleOutput) => Promise<void>;\n get: () => Promise<LoaderBundleOutput>;\n}\n\nexport class PlasmicModulesFetcher {\n private api: Api;\n private curFetch: Promise<LoaderBundleOutput> | undefined = undefined;\n constructor(private opts: FetcherOptions) {\n this.api = new Api({\n projects: opts.projects,\n host: opts.host,\n });\n }\n\n async fetchAllData() {\n if (this.opts.cache) {\n const cachedData = await this.opts.cache.get();\n if (cachedData) {\n return cachedData;\n }\n }\n if (this.curFetch) {\n return await this.curFetch;\n }\n console.debug('Plasmic: doing a fresh fetch...');\n this.curFetch = this.doFetch();\n const data = await this.curFetch;\n this.curFetch = undefined;\n return data;\n }\n\n private async doFetch() {\n const data = await this.api.fetchLoaderData(\n this.opts.projects.map((p) =>\n p.version ? `${p.id}@${p.version}` : p.id\n ),\n {\n platform: this.opts.platform,\n preview: this.opts.preview,\n i18nKeyScheme: this.opts.i18nKeyScheme,\n browserOnly: isBrowser,\n }\n );\n if (this.opts.cache) {\n await this.opts.cache.set(data);\n }\n console.debug(\n `Plasmic: fetched designs for ${data.projects\n .map((p) => `\"${p.name}\" (${p.id}@${p.version})`)\n .join(', ')}`\n );\n return data;\n }\n}\n"],"names":["isBrowser","window","document","Api","constructor","opts","this","host","[object Object]","projectIds","platform","preview","query","URLSearchParams","map","projectId","browserOnly","i18nKeyScheme","toString","url","resp","fetch","method","headers","makeGetHeaders","status","error","parseJsonResponse","Error","_error$error","message","statusText","text","JSON","parse","err","component","embedHydrate","hydrate","json","x-plasmic-loader-version","makeAuthHeaders","makePostHeaders","Content-Type","x-plasmic-api-project-tokens","projects","p","id","token","join","undefined","api","cache","cachedData","get","curFetch","console","debug","doFetch","data","fetchLoaderData","version","set","name"],"mappings":"6JA0HA,MAEaA,EACO,oBAAXC,QACG,MAAVA,aAC2B,IAApBA,OAAOC,eAEHC,EAEXC,YACUC,SAAAC,UAAAD,EAKRC,KAAK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,KAAKD,KAAKwC,SACtB/B,IAAKgC,MAASA,EAAEC,MAAMD,EAAEE,SACxBC,KAAK,yDC1MV7C,YAAoBC,GAAAC,UAAAD,EADZC,mBAAoD4C,EAE1D5C,KAAK6C,IAAM,IAAIhD,EAAI,CACjB0C,SAAUxC,EAAKwC,SACftC,KAAMF,EAAKE,OAIfC,qBACE,GAAIF,KAAKD,KAAK+C,MAAO,CACnB,MAAMC,QAAmB/C,KAAKD,KAAK+C,MAAME,MACzC,GAAID,EACF,OAAOA,EAGX,GAAI/C,KAAKiD,SACP,aAAajD,KAAKiD,SAEpBC,QAAQC,MAAM,mCACdnD,KAAKiD,SAAWjD,KAAKoD,UACrB,MAAMC,QAAarD,KAAKiD,SAExB,OADAjD,KAAKiD,cAAWL,EACTS,EAGDnD,gBACN,MAAMmD,QAAarD,KAAK6C,IAAIS,gBAC1BtD,KAAKD,KAAKwC,SAAS/B,IAAKgC,GACtBA,EAAEe,WAAaf,EAAEC,MAAMD,EAAEe,UAAYf,EAAEC,IAEzC,CACErC,SAAUJ,KAAKD,KAAKK,SACpBC,QAASL,KAAKD,KAAKM,QACnBM,cAAeX,KAAKD,KAAKY,cACzBD,YAAahB,IAWjB,OARIM,KAAKD,KAAK+C,aACN9C,KAAKD,KAAK+C,MAAMU,IAAIH,GAE5BH,QAAQC,sCAC0BE,EAAKd,SAClC/B,IAAKgC,OAAUA,EAAEiB,UAAUjB,EAAEC,MAAMD,EAAEe,YACrCZ,KAAK,OAEHU"}
1
+ {"version":3,"file":"loader-fetcher.cjs.production.min.js","sources":["../src/api.ts","../src/fetcher.ts","../src/uniqueLocalId.ts"],"sourcesContent":["import fetch from '@plasmicapp/isomorphic-unfetch';\n\nexport interface ComponentMeta {\n id: string;\n usedComponents: string[];\n projectId: string;\n name: string;\n displayName: string;\n cssFile: string;\n path: string | undefined;\n isPage: boolean;\n plumeType?: string;\n entry: string;\n isCode: boolean;\n isGlobalContextProvider: boolean;\n pageMetadata?: PageMetadata;\n metadata?: Record<string, string>;\n}\n\nexport interface PageMeta extends ComponentMeta {\n isPage: true;\n path: string;\n plumeType: never;\n pageMetadata: PageMetadata;\n}\n\nexport interface PageMetadata {\n path: string;\n title?: string | null;\n description?: string | null;\n openGraphImageUrl?: string | null;\n}\n\nexport interface GlobalGroupMeta {\n id: string;\n projectId: string;\n name: string;\n type: string;\n contextFile: string;\n useName: string;\n}\n\nexport interface ProjectMeta {\n id: string;\n teamId?: string;\n indirect?: boolean;\n name: string;\n version: string;\n remoteFonts: FontMeta[];\n globalContextsProviderFileName: string;\n}\n\nexport interface FontMeta {\n url: string;\n}\n\ninterface GlobalVariantSplitContent {\n type: 'global-variant';\n projectId: string;\n group: string;\n variant: string;\n}\n\ninterface Slice {\n id: string;\n contents: GlobalVariantSplitContent[];\n externalId?: string;\n}\n\nexport interface ExperimentSlice extends Slice {\n prob: number;\n}\n\nexport interface SegmentSlice extends Slice {\n cond: any;\n}\n\nexport interface ExperimentSplit {\n id: string;\n externalId?: string;\n type: 'experiment';\n slices: ExperimentSlice[];\n}\n\nexport interface SegmentSplit {\n id: string;\n externalId?: string;\n type: 'segment';\n slices: SegmentSlice[];\n}\n\nexport type Split = ExperimentSplit | SegmentSplit;\n\nexport interface LoaderBundleOutput {\n modules: {\n browser: (CodeModule | AssetModule)[];\n server: (CodeModule | AssetModule)[];\n };\n external: string[];\n components: ComponentMeta[];\n globalGroups: GlobalGroupMeta[];\n projects: ProjectMeta[];\n activeSplits: Split[];\n\n // Note this property doesn't come from the API but is generated locally by this module.\n /**\n * ID used to identify LoaderBundleOutputs locally within a process.\n *\n * This property may be unset or `undefined` to indicate an \"empty\" LoaderBundleOutput.\n */\n localId?: number | undefined;\n}\n\nexport interface LoaderHtmlOutput {\n html: string;\n}\n\nexport interface CodeModule {\n fileName: string;\n code: string;\n imports: string[];\n type: 'code';\n}\n\nexport interface AssetModule {\n fileName: string;\n source: string;\n type: 'asset';\n}\n\nconst VERSION = '9';\n\nexport const isBrowser =\n typeof window !== 'undefined' &&\n window != null &&\n typeof window.document !== 'undefined';\n\nexport class Api {\n private host: string;\n constructor(\n private opts: {\n projects: { id: string; token: string }[];\n host?: string;\n }\n ) {\n this.host = opts.host ?? 'https://codegen.plasmic.app';\n }\n\n async fetchLoaderData(\n projectIds: string[],\n opts: {\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n browserOnly?: boolean;\n i18nKeyScheme?: 'content' | 'hash';\n }\n ) {\n const { platform, preview } = opts;\n const query = new URLSearchParams([\n ['platform', platform ?? 'react'],\n ...projectIds.map((projectId) => ['projectId', projectId]),\n ...(opts.browserOnly ? [['browserOnly', 'true']] : []),\n ...(opts.i18nKeyScheme ? [['i18nKeyScheme', opts.i18nKeyScheme]] : []),\n ]).toString();\n\n const url = `${this.host}/api/v1/loader/code/${\n preview ? 'preview' : 'published'\n }?${query}`;\n const resp = await fetch(url, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n if (resp.status >= 400) {\n const error = await this.parseJsonResponse(resp);\n throw new Error(\n `Error fetching loader data: ${\n error?.error?.message ?? resp.statusText\n }`\n );\n }\n const json = await this.parseJsonResponse(resp);\n return json as LoaderBundleOutput;\n }\n\n private async parseJsonResponse(resp: Response) {\n const text = await resp.text();\n try {\n return JSON.parse(text);\n } catch (err) {\n throw new Error(`Error parsing JSON response: ${err}; response: ${text}`);\n }\n }\n\n async fetchHtmlData(opts: {\n projectId: string;\n component: string;\n hydrate?: boolean;\n embedHydrate?: boolean;\n }) {\n const { projectId, component, embedHydrate, hydrate } = opts;\n const query = new URLSearchParams([\n ['projectId', projectId],\n ['component', component],\n ['embedHydrate', embedHydrate ? '1' : '0'],\n ['hydrate', hydrate ? '1' : '0'],\n ]).toString();\n const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n const json = await resp.json();\n return json as LoaderHtmlOutput;\n }\n\n private makeGetHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n ...this.makeAuthHeaders(),\n };\n }\n\n // @ts-ignore\n private makePostHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n 'Content-Type': 'application/json',\n ...this.makeAuthHeaders(),\n };\n }\n\n private makeAuthHeaders() {\n const tokens = this.opts.projects\n .map((p) => `${p.id}:${p.token}`)\n .join(',');\n return {\n 'x-plasmic-api-project-tokens': tokens,\n };\n }\n}\n","import { Api, isBrowser, LoaderBundleOutput } from './api';\nimport { uniqueLocalId } from './uniqueLocalId';\n\nexport interface FetcherOptions {\n projects: {\n id: string;\n version?: string;\n token: string;\n }[];\n cache?: LoaderBundleCache;\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n host?: string;\n i18nKeyScheme?: 'content' | 'hash';\n}\n\nexport interface LoaderBundleCache {\n set: (data: LoaderBundleOutput) => Promise<void>;\n get: () => Promise<LoaderBundleOutput>;\n}\n\nexport class PlasmicModulesFetcher {\n private api: Api;\n private curFetch: Promise<LoaderBundleOutput> | undefined = undefined;\n constructor(private opts: FetcherOptions) {\n this.api = new Api({\n projects: opts.projects,\n host: opts.host,\n });\n }\n\n async fetchAllData() {\n const bundle = await this.getCachedOrFetch();\n\n // Assign a local ID AFTER caching. If the local ID were cached, it may no longer be unique.\n bundle.localId = uniqueLocalId();\n this.storeGlobally(bundle);\n\n return bundle;\n }\n\n private async getCachedOrFetch() {\n if (this.opts.cache) {\n const cachedData = await this.opts.cache.get();\n if (cachedData) {\n return cachedData;\n }\n }\n if (this.curFetch) {\n return await this.curFetch;\n }\n console.debug('Plasmic: doing a fresh fetch...');\n this.curFetch = this.doFetch();\n const data = await this.curFetch;\n this.curFetch = undefined;\n return data;\n }\n\n private async doFetch() {\n const data = await this.api.fetchLoaderData(\n this.opts.projects.map((p) =>\n p.version ? `${p.id}@${p.version}` : p.id\n ),\n {\n platform: this.opts.platform,\n preview: this.opts.preview,\n i18nKeyScheme: this.opts.i18nKeyScheme,\n browserOnly: isBrowser,\n }\n );\n if (this.opts.cache) {\n await this.opts.cache.set(data);\n }\n console.debug(\n `Plasmic: fetched designs for ${data.projects\n .map((p) => `\"${p.name}\" (${p.id}@${p.version})`)\n .join(', ')}`\n );\n return data;\n }\n\n // For React Server Components (Next.js 13+),\n // we need to pass server modules in LoaderBundleOutput from Server Components to Client Components.\n // We don't want to pass them via normal page props because that will be serialized to the browser.\n // Instead, we pass the bundle (including the server modules) via the Node `global` variable.\n //\n // This is the code that stores the bundle.\n private storeGlobally(bundle: LoaderBundleOutput) {\n if (bundle.localId === undefined) {\n return;\n }\n\n if (global.__PLASMIC_BUNDLES === undefined) {\n global.__PLASMIC_BUNDLES = {};\n }\n global.__PLASMIC_BUNDLES[bundle.localId] = bundle;\n }\n}\n\ninterface GlobalWithBundles {\n __PLASMIC_BUNDLES?: { [localId: number]: LoaderBundleOutput };\n}\nconst global = globalThis as GlobalWithBundles;\n","interface GlobalWithNextLocalId {\n __PLASMIC_NEXT_LOCAL_ID?: number;\n}\n\n/**\n * Returns an ID that is locally unique within a process.\n *\n * This function works even if this function's module is reloaded within the same process.\n * It depends on setting state in the environment's `globalThis` variable.\n */\nexport function uniqueLocalId(): number {\n const global = globalThis as GlobalWithNextLocalId;\n const localId = global.__PLASMIC_NEXT_LOCAL_ID\n ? global.__PLASMIC_NEXT_LOCAL_ID\n : 1;\n global.__PLASMIC_NEXT_LOCAL_ID = localId + 1;\n return localId;\n}\n"],"names":["isBrowser","window","document","Api","constructor","opts","this","host","[object Object]","projectIds","platform","preview","query","URLSearchParams","map","projectId","browserOnly","i18nKeyScheme","toString","url","resp","fetch","method","headers","makeGetHeaders","status","error","parseJsonResponse","Error","_error$error","message","statusText","text","JSON","parse","err","component","embedHydrate","hydrate","json","x-plasmic-loader-version","makeAuthHeaders","makePostHeaders","Content-Type","x-plasmic-api-project-tokens","projects","p","id","token","join","global","globalThis","undefined","api","bundle","getCachedOrFetch","localId","__PLASMIC_NEXT_LOCAL_ID","uniqueLocalId","storeGlobally","cache","cachedData","get","curFetch","console","debug","doFetch","data","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AClC/B,IAAKgC,OAAUA,EAAEyB,UAAUzB,EAAEC,MAAMD,EAAEuB,YACrCpB,KAAK,OAEHkB,EASDR,cAAcL,QACGF,IAAnBE,EAAOE,eAIsBJ,IAA7BF,EAAOsB,oBACTtB,EAAOsB,kBAAoB,IAE7BtB,EAAOsB,kBAAkBlB,EAAOE,SAAWF"}
@@ -1,4 +1,4 @@
1
- import fetch from 'isomorphic-unfetch';
1
+ import fetch from '@plasmicapp/isomorphic-unfetch';
2
2
 
3
3
  const VERSION = '9';
4
4
  const isBrowser = typeof window !== 'undefined' && window != null && typeof window.document !== 'undefined';
@@ -72,6 +72,19 @@ class Api {
72
72
  }
73
73
  }
74
74
 
75
+ /**
76
+ * Returns an ID that is locally unique within a process.
77
+ *
78
+ * This function works even if this function's module is reloaded within the same process.
79
+ * It depends on setting state in the environment's `globalThis` variable.
80
+ */
81
+ function uniqueLocalId() {
82
+ const global = globalThis;
83
+ const localId = global.__PLASMIC_NEXT_LOCAL_ID ? global.__PLASMIC_NEXT_LOCAL_ID : 1;
84
+ global.__PLASMIC_NEXT_LOCAL_ID = localId + 1;
85
+ return localId;
86
+ }
87
+
75
88
  class PlasmicModulesFetcher {
76
89
  constructor(opts) {
77
90
  this.opts = opts;
@@ -82,6 +95,13 @@ class PlasmicModulesFetcher {
82
95
  });
83
96
  }
84
97
  async fetchAllData() {
98
+ const bundle = await this.getCachedOrFetch();
99
+ // Assign a local ID AFTER caching. If the local ID were cached, it may no longer be unique.
100
+ bundle.localId = uniqueLocalId();
101
+ this.storeGlobally(bundle);
102
+ return bundle;
103
+ }
104
+ async getCachedOrFetch() {
85
105
  if (this.opts.cache) {
86
106
  const cachedData = await this.opts.cache.get();
87
107
  if (cachedData) {
@@ -110,7 +130,23 @@ class PlasmicModulesFetcher {
110
130
  console.debug(`Plasmic: fetched designs for ${data.projects.map(p => `"${p.name}" (${p.id}@${p.version})`).join(', ')}`);
111
131
  return data;
112
132
  }
133
+ // For React Server Components (Next.js 13+),
134
+ // we need to pass server modules in LoaderBundleOutput from Server Components to Client Components.
135
+ // We don't want to pass them via normal page props because that will be serialized to the browser.
136
+ // Instead, we pass the bundle (including the server modules) via the Node `global` variable.
137
+ //
138
+ // This is the code that stores the bundle.
139
+ storeGlobally(bundle) {
140
+ if (bundle.localId === undefined) {
141
+ return;
142
+ }
143
+ if (global.__PLASMIC_BUNDLES === undefined) {
144
+ global.__PLASMIC_BUNDLES = {};
145
+ }
146
+ global.__PLASMIC_BUNDLES[bundle.localId] = bundle;
147
+ }
113
148
  }
149
+ const global = globalThis;
114
150
 
115
151
  export { Api, PlasmicModulesFetcher };
116
152
  //# sourceMappingURL=loader-fetcher.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"loader-fetcher.esm.js","sources":["../src/api.ts","../src/fetcher.ts"],"sourcesContent":["import fetch from 'isomorphic-unfetch';\n\nexport interface ComponentMeta {\n id: string;\n usedComponents: string[];\n projectId: string;\n name: string;\n displayName: string;\n cssFile: string;\n path: string | undefined;\n isPage: boolean;\n plumeType?: string;\n entry: string;\n isCode: boolean;\n isGlobalContextProvider: boolean;\n pageMetadata?: PageMetadata;\n metadata?: Record<string, string>;\n}\n\nexport interface PageMeta extends ComponentMeta {\n isPage: true;\n path: string;\n plumeType: never;\n pageMetadata: PageMetadata;\n}\n\nexport interface PageMetadata {\n path: string;\n title?: string | null;\n description?: string | null;\n openGraphImageUrl?: string | null;\n}\n\nexport interface GlobalGroupMeta {\n id: string;\n projectId: string;\n name: string;\n type: string;\n contextFile: string;\n useName: string;\n}\n\nexport interface ProjectMeta {\n id: string;\n teamId?: string;\n indirect?: boolean;\n name: string;\n version: string;\n remoteFonts: FontMeta[];\n globalContextsProviderFileName: string;\n}\n\nexport interface FontMeta {\n url: string;\n}\n\ninterface GlobalVariantSplitContent {\n type: 'global-variant';\n projectId: string;\n group: string;\n variant: string;\n}\n\ninterface Slice {\n id: string;\n contents: GlobalVariantSplitContent[];\n externalId?: string;\n}\n\nexport interface ExperimentSlice extends Slice {\n prob: number;\n}\n\nexport interface SegmentSlice extends Slice {\n cond: any;\n}\n\nexport interface ExperimentSplit {\n id: string;\n externalId?: string;\n type: 'experiment';\n slices: ExperimentSlice[];\n}\n\nexport interface SegmentSplit {\n id: string;\n externalId?: string;\n type: 'segment';\n slices: SegmentSlice[];\n}\n\nexport type Split = ExperimentSplit | SegmentSplit;\n\nexport interface LoaderBundleOutput {\n modules: {\n browser: (CodeModule | AssetModule)[];\n server: (CodeModule | AssetModule)[];\n };\n external: string[];\n components: ComponentMeta[];\n globalGroups: GlobalGroupMeta[];\n projects: ProjectMeta[];\n activeSplits: Split[];\n}\n\nexport interface LoaderHtmlOutput {\n html: string;\n}\n\nexport interface CodeModule {\n fileName: string;\n code: string;\n imports: string[];\n type: 'code';\n}\n\nexport interface AssetModule {\n fileName: string;\n source: string;\n type: 'asset';\n}\n\nconst VERSION = '9';\n\nexport const isBrowser =\n typeof window !== 'undefined' &&\n window != null &&\n typeof window.document !== 'undefined';\n\nexport class Api {\n private host: string;\n constructor(\n private opts: {\n projects: { id: string; token: string }[];\n host?: string;\n }\n ) {\n this.host = opts.host ?? 'https://codegen.plasmic.app';\n }\n\n async fetchLoaderData(\n projectIds: string[],\n opts: {\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n browserOnly?: boolean;\n i18nKeyScheme?: 'content' | 'hash';\n }\n ) {\n const { platform, preview } = opts;\n const query = new URLSearchParams([\n ['platform', platform ?? 'react'],\n ...projectIds.map((projectId) => ['projectId', projectId]),\n ...(opts.browserOnly ? [['browserOnly', 'true']] : []),\n ...(opts.i18nKeyScheme ? [['i18nKeyScheme', opts.i18nKeyScheme]] : []),\n ]).toString();\n\n const url = `${this.host}/api/v1/loader/code/${\n preview ? 'preview' : 'published'\n }?${query}`;\n const resp = await fetch(url, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n if (resp.status >= 400) {\n const error = await this.parseJsonResponse(resp);\n throw new Error(\n `Error fetching loader data: ${\n error?.error?.message ?? resp.statusText\n }`\n );\n }\n const json = await this.parseJsonResponse(resp);\n return json as LoaderBundleOutput;\n }\n\n private async parseJsonResponse(resp: Response) {\n const text = await resp.text();\n try {\n return JSON.parse(text);\n } catch (err) {\n throw new Error(`Error parsing JSON response: ${err}; response: ${text}`);\n }\n }\n\n async fetchHtmlData(opts: {\n projectId: string;\n component: string;\n hydrate?: boolean;\n embedHydrate?: boolean;\n }) {\n const { projectId, component, embedHydrate, hydrate } = opts;\n const query = new URLSearchParams([\n ['projectId', projectId],\n ['component', component],\n ['embedHydrate', embedHydrate ? '1' : '0'],\n ['hydrate', hydrate ? '1' : '0'],\n ]).toString();\n const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n const json = await resp.json();\n return json as LoaderHtmlOutput;\n }\n\n private makeGetHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n ...this.makeAuthHeaders(),\n };\n }\n\n // @ts-ignore\n private makePostHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n 'Content-Type': 'application/json',\n ...this.makeAuthHeaders(),\n };\n }\n\n private makeAuthHeaders() {\n const tokens = this.opts.projects\n .map((p) => `${p.id}:${p.token}`)\n .join(',');\n return {\n 'x-plasmic-api-project-tokens': tokens,\n };\n }\n}\n","import { Api, isBrowser, LoaderBundleOutput } from './api';\n\nexport interface FetcherOptions {\n projects: {\n id: string;\n version?: string;\n token: string;\n }[];\n cache?: LoaderBundleCache;\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n host?: string;\n i18nKeyScheme?: 'content' | 'hash';\n}\n\nexport interface LoaderBundleCache {\n set: (data: LoaderBundleOutput) => Promise<void>;\n get: () => Promise<LoaderBundleOutput>;\n}\n\nexport class PlasmicModulesFetcher {\n private api: Api;\n private curFetch: Promise<LoaderBundleOutput> | undefined = undefined;\n constructor(private opts: FetcherOptions) {\n this.api = new Api({\n projects: opts.projects,\n host: opts.host,\n });\n }\n\n async fetchAllData() {\n if (this.opts.cache) {\n const cachedData = await this.opts.cache.get();\n if (cachedData) {\n return cachedData;\n }\n }\n if (this.curFetch) {\n return await this.curFetch;\n }\n console.debug('Plasmic: doing a fresh fetch...');\n this.curFetch = this.doFetch();\n const data = await this.curFetch;\n this.curFetch = undefined;\n return data;\n }\n\n private async doFetch() {\n const data = await this.api.fetchLoaderData(\n this.opts.projects.map((p) =>\n p.version ? `${p.id}@${p.version}` : p.id\n ),\n {\n platform: this.opts.platform,\n preview: this.opts.preview,\n i18nKeyScheme: this.opts.i18nKeyScheme,\n browserOnly: isBrowser,\n }\n );\n if (this.opts.cache) {\n await this.opts.cache.set(data);\n }\n console.debug(\n `Plasmic: fetched designs for ${data.projects\n .map((p) => `\"${p.name}\" (${p.id}@${p.version})`)\n .join(', ')}`\n );\n return data;\n }\n}\n"],"names":["VERSION","isBrowser","window","document","Api","constructor","opts","host","fetchLoaderData","projectIds","platform","preview","query","URLSearchParams","map","projectId","browserOnly","i18nKeyScheme","toString","url","resp","fetch","method","headers","makeGetHeaders","status","error","parseJsonResponse","Error","message","statusText","json","text","JSON","parse","err","fetchHtmlData","component","embedHydrate","hydrate","makeAuthHeaders","makePostHeaders","tokens","projects","p","id","token","join","PlasmicModulesFetcher","undefined","api","fetchAllData","cache","cachedData","get","curFetch","console","debug","doFetch","data","version","set","name"],"mappings":";;AA0HA,MAAMA,OAAO,GAAG,GAAG;AAEZ,MAAMC,SAAS,GACpB,OAAOC,MAAM,KAAK,WAAW,IAC7BA,MAAM,IAAI,IAAI,IACd,OAAOA,MAAM,CAACC,QAAQ,KAAK,WAAW;MAE3BC,GAAG;EAEdC,YACUC,IAGP;;IAHO,SAAI,GAAJA,IAAI;IAKZ,IAAI,CAACC,IAAI,iBAAGD,IAAI,CAACC,IAAI,yBAAI,6BAA6B;;EAGxD,MAAMC,eAAe,CACnBC,UAAoB,EACpBH,IAKC;IAED,MAAM;MAAEI,QAAQ;MAAEC;KAAS,GAAGL,IAAI;IAClC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CAACkB,UAAUlB,CAAC,CAACC,MAAMD,CAAC,CAACgB,UAAU,CAAC,CAChDb,IAAI,CAAC,IAAI,GAAG,CAChB;IACD,OAAOY,IAAI;;;;;;"}
1
+ {"version":3,"file":"loader-fetcher.esm.js","sources":["../src/api.ts","../src/uniqueLocalId.ts","../src/fetcher.ts"],"sourcesContent":["import fetch from '@plasmicapp/isomorphic-unfetch';\n\nexport interface ComponentMeta {\n id: string;\n usedComponents: string[];\n projectId: string;\n name: string;\n displayName: string;\n cssFile: string;\n path: string | undefined;\n isPage: boolean;\n plumeType?: string;\n entry: string;\n isCode: boolean;\n isGlobalContextProvider: boolean;\n pageMetadata?: PageMetadata;\n metadata?: Record<string, string>;\n}\n\nexport interface PageMeta extends ComponentMeta {\n isPage: true;\n path: string;\n plumeType: never;\n pageMetadata: PageMetadata;\n}\n\nexport interface PageMetadata {\n path: string;\n title?: string | null;\n description?: string | null;\n openGraphImageUrl?: string | null;\n}\n\nexport interface GlobalGroupMeta {\n id: string;\n projectId: string;\n name: string;\n type: string;\n contextFile: string;\n useName: string;\n}\n\nexport interface ProjectMeta {\n id: string;\n teamId?: string;\n indirect?: boolean;\n name: string;\n version: string;\n remoteFonts: FontMeta[];\n globalContextsProviderFileName: string;\n}\n\nexport interface FontMeta {\n url: string;\n}\n\ninterface GlobalVariantSplitContent {\n type: 'global-variant';\n projectId: string;\n group: string;\n variant: string;\n}\n\ninterface Slice {\n id: string;\n contents: GlobalVariantSplitContent[];\n externalId?: string;\n}\n\nexport interface ExperimentSlice extends Slice {\n prob: number;\n}\n\nexport interface SegmentSlice extends Slice {\n cond: any;\n}\n\nexport interface ExperimentSplit {\n id: string;\n externalId?: string;\n type: 'experiment';\n slices: ExperimentSlice[];\n}\n\nexport interface SegmentSplit {\n id: string;\n externalId?: string;\n type: 'segment';\n slices: SegmentSlice[];\n}\n\nexport type Split = ExperimentSplit | SegmentSplit;\n\nexport interface LoaderBundleOutput {\n modules: {\n browser: (CodeModule | AssetModule)[];\n server: (CodeModule | AssetModule)[];\n };\n external: string[];\n components: ComponentMeta[];\n globalGroups: GlobalGroupMeta[];\n projects: ProjectMeta[];\n activeSplits: Split[];\n\n // Note this property doesn't come from the API but is generated locally by this module.\n /**\n * ID used to identify LoaderBundleOutputs locally within a process.\n *\n * This property may be unset or `undefined` to indicate an \"empty\" LoaderBundleOutput.\n */\n localId?: number | undefined;\n}\n\nexport interface LoaderHtmlOutput {\n html: string;\n}\n\nexport interface CodeModule {\n fileName: string;\n code: string;\n imports: string[];\n type: 'code';\n}\n\nexport interface AssetModule {\n fileName: string;\n source: string;\n type: 'asset';\n}\n\nconst VERSION = '9';\n\nexport const isBrowser =\n typeof window !== 'undefined' &&\n window != null &&\n typeof window.document !== 'undefined';\n\nexport class Api {\n private host: string;\n constructor(\n private opts: {\n projects: { id: string; token: string }[];\n host?: string;\n }\n ) {\n this.host = opts.host ?? 'https://codegen.plasmic.app';\n }\n\n async fetchLoaderData(\n projectIds: string[],\n opts: {\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n browserOnly?: boolean;\n i18nKeyScheme?: 'content' | 'hash';\n }\n ) {\n const { platform, preview } = opts;\n const query = new URLSearchParams([\n ['platform', platform ?? 'react'],\n ...projectIds.map((projectId) => ['projectId', projectId]),\n ...(opts.browserOnly ? [['browserOnly', 'true']] : []),\n ...(opts.i18nKeyScheme ? [['i18nKeyScheme', opts.i18nKeyScheme]] : []),\n ]).toString();\n\n const url = `${this.host}/api/v1/loader/code/${\n preview ? 'preview' : 'published'\n }?${query}`;\n const resp = await fetch(url, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n if (resp.status >= 400) {\n const error = await this.parseJsonResponse(resp);\n throw new Error(\n `Error fetching loader data: ${\n error?.error?.message ?? resp.statusText\n }`\n );\n }\n const json = await this.parseJsonResponse(resp);\n return json as LoaderBundleOutput;\n }\n\n private async parseJsonResponse(resp: Response) {\n const text = await resp.text();\n try {\n return JSON.parse(text);\n } catch (err) {\n throw new Error(`Error parsing JSON response: ${err}; response: ${text}`);\n }\n }\n\n async fetchHtmlData(opts: {\n projectId: string;\n component: string;\n hydrate?: boolean;\n embedHydrate?: boolean;\n }) {\n const { projectId, component, embedHydrate, hydrate } = opts;\n const query = new URLSearchParams([\n ['projectId', projectId],\n ['component', component],\n ['embedHydrate', embedHydrate ? '1' : '0'],\n ['hydrate', hydrate ? '1' : '0'],\n ]).toString();\n const resp = await fetch(`${this.host}/api/v1/loader/html?${query}`, {\n method: 'GET',\n headers: this.makeGetHeaders(),\n });\n const json = await resp.json();\n return json as LoaderHtmlOutput;\n }\n\n private makeGetHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n ...this.makeAuthHeaders(),\n };\n }\n\n // @ts-ignore\n private makePostHeaders() {\n return {\n 'x-plasmic-loader-version': VERSION,\n 'Content-Type': 'application/json',\n ...this.makeAuthHeaders(),\n };\n }\n\n private makeAuthHeaders() {\n const tokens = this.opts.projects\n .map((p) => `${p.id}:${p.token}`)\n .join(',');\n return {\n 'x-plasmic-api-project-tokens': tokens,\n };\n }\n}\n","interface GlobalWithNextLocalId {\n __PLASMIC_NEXT_LOCAL_ID?: number;\n}\n\n/**\n * Returns an ID that is locally unique within a process.\n *\n * This function works even if this function's module is reloaded within the same process.\n * It depends on setting state in the environment's `globalThis` variable.\n */\nexport function uniqueLocalId(): number {\n const global = globalThis as GlobalWithNextLocalId;\n const localId = global.__PLASMIC_NEXT_LOCAL_ID\n ? global.__PLASMIC_NEXT_LOCAL_ID\n : 1;\n global.__PLASMIC_NEXT_LOCAL_ID = localId + 1;\n return localId;\n}\n","import { Api, isBrowser, LoaderBundleOutput } from './api';\nimport { uniqueLocalId } from './uniqueLocalId';\n\nexport interface FetcherOptions {\n projects: {\n id: string;\n version?: string;\n token: string;\n }[];\n cache?: LoaderBundleCache;\n platform?: 'react' | 'nextjs' | 'gatsby';\n preview?: boolean;\n host?: string;\n i18nKeyScheme?: 'content' | 'hash';\n}\n\nexport interface LoaderBundleCache {\n set: (data: LoaderBundleOutput) => Promise<void>;\n get: () => Promise<LoaderBundleOutput>;\n}\n\nexport class PlasmicModulesFetcher {\n private api: Api;\n private curFetch: Promise<LoaderBundleOutput> | undefined = undefined;\n constructor(private opts: FetcherOptions) {\n this.api = new Api({\n projects: opts.projects,\n host: opts.host,\n });\n }\n\n async fetchAllData() {\n const bundle = await this.getCachedOrFetch();\n\n // Assign a local ID AFTER caching. If the local ID were cached, it may no longer be unique.\n bundle.localId = uniqueLocalId();\n this.storeGlobally(bundle);\n\n return bundle;\n }\n\n private async getCachedOrFetch() {\n if (this.opts.cache) {\n const cachedData = await this.opts.cache.get();\n if (cachedData) {\n return cachedData;\n }\n }\n if (this.curFetch) {\n return await this.curFetch;\n }\n console.debug('Plasmic: doing a fresh fetch...');\n this.curFetch = this.doFetch();\n const data = await this.curFetch;\n this.curFetch = undefined;\n return data;\n }\n\n private async doFetch() {\n const data = await this.api.fetchLoaderData(\n this.opts.projects.map((p) =>\n p.version ? `${p.id}@${p.version}` : p.id\n ),\n {\n platform: this.opts.platform,\n preview: this.opts.preview,\n i18nKeyScheme: this.opts.i18nKeyScheme,\n browserOnly: isBrowser,\n }\n );\n if (this.opts.cache) {\n await this.opts.cache.set(data);\n }\n console.debug(\n `Plasmic: fetched designs for ${data.projects\n .map((p) => `\"${p.name}\" (${p.id}@${p.version})`)\n .join(', ')}`\n );\n return data;\n }\n\n // For React Server Components (Next.js 13+),\n // we need to pass server modules in LoaderBundleOutput from Server Components to Client Components.\n // We don't want to pass them via normal page props because that will be serialized to the browser.\n // Instead, we pass the bundle (including the server modules) via the Node `global` variable.\n //\n // This is the code that stores the bundle.\n private storeGlobally(bundle: LoaderBundleOutput) {\n if (bundle.localId === undefined) {\n return;\n }\n\n if (global.__PLASMIC_BUNDLES === undefined) {\n global.__PLASMIC_BUNDLES = {};\n }\n global.__PLASMIC_BUNDLES[bundle.localId] = bundle;\n }\n}\n\ninterface GlobalWithBundles {\n __PLASMIC_BUNDLES?: { [localId: number]: LoaderBundleOutput };\n}\nconst global = globalThis as GlobalWithBundles;\n"],"names":["VERSION","isBrowser","window","document","Api","constructor","opts","host","fetchLoaderData","projectIds","platform","preview","query","URLSearchParams","map","projectId","browserOnly","i18nKeyScheme","toString","url","resp","fetch","method","headers","makeGetHeaders","status","error","parseJsonResponse","Error","message","statusText","json","text","JSON","parse","err","fetchHtmlData","component","embedHydrate","hydrate","makeAuthHeaders","makePostHeaders","tokens","projects","p","id","token","join","uniqueLocalId","global","globalThis","localId","__PLASMIC_NEXT_LOCAL_ID","PlasmicModulesFetcher","undefined","api","fetchAllData","bundle","getCachedOrFetch","storeGlobally","cache","cachedData","get","curFetch","console","debug","doFetch","data","version","set","name","__PLASMIC_BUNDLES"],"mappings":";;AAkIA,MAAMA,OAAO,GAAG,GAAG;AAEZ,MAAMC,SAAS,GACpB,OAAOC,MAAM,KAAK,WAAW,IAC7BA,MAAM,IAAI,IAAI,IACd,OAAOA,MAAM,CAACC,QAAQ,KAAK,WAAW;MAE3BC,GAAG;EAEdC,YACUC,IAGP;;IAHO,SAAI,GAAJA,IAAI;IAKZ,IAAI,CAACC,IAAI,iBAAGD,IAAI,CAACC,IAAI,yBAAI,6BAA6B;;EAGxD,MAAMC,eAAe,CACnBC,UAAoB,EACpBH,IAKC;IAED,MAAM;MAAEI,QAAQ;MAAEC;KAAS,GAAGL,IAAI;IAClC,MAAMM,KAAK,GAAG,IAAIC,eAAe,CAAC,CAChC,CAAC,UAAU,EAAEH,QAAQ,WAARA,QAAQ,GAAI,OAAO,CAAC,EACjC,GAAGD,UAAU,CAACK,GAAG,CAAEC,SAAS,IAAK,CAAC,WAAW,EAAEA,SAAS,CAAC,CAAC,EAC1D,IAAIT,IAAI,CAACU,WAAW,GAAG,CAAC,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EACtD,IAAIV,IAAI,CAACW,aAAa,GAAG,CAAC,CAAC,eAAe,EAAEX,IAAI,CAACW,aAAa,CAAC,CA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C1C7B,GAAG,CAAE8B,CAAC,QAASA,CAAC,CAAC0B,UAAU1B,CAAC,CAACC,MAAMD,CAAC,CAACwB,UAAU,CAAC,CAChDrB,IAAI,CAAC,IAAI,GAAG,CAChB;IACD,OAAOoB,IAAI;;;;;;;;EASLR,aAAa,CAACF,MAA0B;IAC9C,IAAIA,MAAM,CAACN,OAAO,KAAKG,SAAS,EAAE;MAChC;;IAGF,IAAIL,MAAM,CAACsB,iBAAiB,KAAKjB,SAAS,EAAE;MAC1CL,MAAM,CAACsB,iBAAiB,GAAG,EAAE;;IAE/BtB,MAAM,CAACsB,iBAAiB,CAACd,MAAM,CAACN,OAAO,CAAC,GAAGM,MAAM;;;AAOrD,MAAMR,MAAM,GAAGC,UAA+B;;;;"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Returns an ID that is locally unique within a process.
3
+ *
4
+ * This function works even if this function's module is reloaded within the same process.
5
+ * It depends on setting state in the environment's `globalThis` variable.
6
+ */
7
+ export declare function uniqueLocalId(): number;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.16",
2
+ "version": "1.0.18",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -46,7 +46,6 @@
46
46
  ],
47
47
  "devDependencies": {
48
48
  "@size-limit/preset-small-lib": "^4.11.0",
49
- "@types/isomorphic-fetch": "^0.0.35",
50
49
  "husky": "^6.0.0",
51
50
  "size-limit": "^4.11.0",
52
51
  "tsdx": "^0.14.1",
@@ -54,10 +53,10 @@
54
53
  "typescript": "^4.3.2"
55
54
  },
56
55
  "dependencies": {
57
- "isomorphic-unfetch": "^3.1.0"
56
+ "@plasmicapp/isomorphic-unfetch": "^1.0.1"
58
57
  },
59
58
  "publishConfig": {
60
59
  "access": "public"
61
60
  },
62
- "gitHead": "29e95851972acf14bf3ff15462022d322f4c6b57"
61
+ "gitHead": "efbeab79fc47f510321095be368cc2d25781b9ba"
63
62
  }