UNPKG

@vercel/microfrontends

Version:

Defines configuration and utilities for microfrontends development

3 lines 6.57 kB
"use client"; "use strict";var z=Object.create;var m=Object.defineProperty;var J=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var V=Object.getPrototypeOf,U=Object.prototype.hasOwnProperty;var X=(e,t)=>{for(var n in t)m(e,n,{get:t[n],enumerable:!0})},M=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of B(t))!U.call(e,r)&&r!==n&&m(e,r,{get:()=>t[r],enumerable:!(o=J(t,r))||o.enumerable});return e};var _=(e,t,n)=>(n=e!=null?z(V(e)):{},M(t||!e||!e.__esModule?m(n,"default",{value:e,enumerable:!0}):n,e)),W=e=>M(m({},"__esModule",{value:!0}),e);var ee={};X(ee,{Link:()=>Z,PrefetchCrossZoneLinks:()=>Q,PrefetchCrossZoneLinksContext:()=>P,PrefetchCrossZoneLinksProvider:()=>G,useZoneForHref:()=>H});module.exports=W(ee);var g=require("react"),A=_(require("next/link.js"),1);var u=require("react");var N=require("path-to-regexp"),O=new Map,$=e=>{let t=O.get(e);if(t)return t;let n=(0,N.pathToRegexp)(e);return O.set(e,n),n},f=class{constructor(t,n){this.pathCache={};if(this.serialized=t,n?.removeFlaggedPaths)for(let o of Object.values(t.applications))o.routing&&(o.routing=o.routing.filter(r=>!r.flag));this.applications=t.applications}static fromEnv(t,n){if(!t)throw new Error("Could not construct MicrofrontendConfigClient: configuration is empty or undefined. Did you set up your application with `withMicrofrontends`?");return new f(JSON.parse(t),n)}isEqual(t){return this===t||JSON.stringify(this.applications)===JSON.stringify(t.applications)}getApplicationNameForPath(t){if(!t.startsWith("/"))throw new Error("Path must start with a /");if(this.pathCache[t])return this.pathCache[t];let n=new URL(t,"https://example.com").pathname;for(let[r,a]of Object.entries(this.applications))if(a.routing){for(let i of a.routing)for(let s of i.paths)if($(s).test(n))return this.pathCache[t]=r,r}let o=Object.entries(this.applications).find(([,r])=>r.default);return o?(this.pathCache[t]=o[0],o[0]):null}serialize(){return this.serialized}};var R=new Map,b=new Map,K=e=>JSON.stringify({removeFlaggedPaths:e?.removeFlaggedPaths||!1}),k=(e,t)=>{let n=K(t),o=R.get(e||"");if(!o)o=new Map,R.set(e||"",o);else{let a=o.get(n);if(a)return a}let r=f.fromEnv(e,t);return o.set(n,r),r},y=null,L=null;async function q(){try{let e=await fetch("/.well-known/vercel/microfrontends/client-config");if(e.status!==200)return null;let t=await e.json(),n=new f(t.config);return L=n,n}catch{return null}}function E(e,{removeFlaggedPathsFromDefault:t}={}){let[n,o]=(0,u.useState)(()=>L??k(e,{removeFlaggedPaths:t})),r=(0,u.useMemo)(()=>{if(process.env.NODE_ENV==="test"&&process.env.MFE_FORCE_CLIENT_CONFIG_FROM_SERVER!=="1"||L)return!1;let s=b.get(e||"");if(s!==void 0)return s;let d=k(e),C=Object.values(d.applications).some(I=>I.routing?.some(D=>D.flag));return b.set(e||"",C),!!C},[e]),[a,i]=(0,u.useState)(r);return(0,u.useEffect)(()=>{r&&(y||(y=q()),y.then(s=>{s&&o(d=>d.isEqual(s)?d:s)}).finally(()=>{i(!1)}))},[e,n.applications,r]),{clientConfig:n,isLoading:a}}var c=require("react"),p=require("react/jsx-runtime"),P=(0,c.createContext)({prefetchHref:()=>{}});function G({children:e}){let[t,n]=(0,c.useState)(new Set),o=(0,c.useRef)(typeof navigator<"u"&&(navigator.userAgent.includes("Firefox")||navigator.userAgent.includes("Safari")&&!navigator.userAgent.includes("Chrome"))),r=(0,c.useCallback)(i=>{(0,c.startTransition)(()=>{n(s=>s.has(i)?s:new Set(s).add(i))})},[]),a=(0,c.useMemo)(()=>({prefetchHref:r}),[r]);return o.current?(0,p.jsxs)(P.Provider,{value:a,children:[e,[...t].map(i=>(0,p.jsx)("link",{as:"fetch",href:i,rel:"preload"},i))]}):(0,p.jsx)(p.Fragment,{children:e})}var h=require("react"),S=_(require("next/script.js"),1);var T=require("react/jsx-runtime"),v="data-prefetch",l={anyZone:"[data-zone]",external:'[data-zone="null"]',sameZone:'[data-zone="same"]',prefetch:`[${v}]`},w={and:[{href_matches:"/*"},{selector_matches:l.anyZone},{not:{selector_matches:l.sameZone}},{not:{selector_matches:l.external}}]},j={and:[{href_matches:"/*"},{selector_matches:l.anyZone},{not:{selector_matches:l.sameZone}},{not:{selector_matches:l.external}},{selector_matches:l.prefetch}]};function F(e){if(!e)return!0;if("checkVisibility"in e)return e.checkVisibility({opacityProperty:!0});let t=e,n=window.getComputedStyle(t);return n.display==="none"||n.visibility==="hidden"||n.opacity==="0"?!1:F(t.parentElement)}function Q(){let{isLoading:e}=E(process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG),[t,n]=(0,h.useState)([]);return(0,h.useEffect)(()=>{if(e)return;let r=new IntersectionObserver(a=>{a.forEach(i=>{i.isIntersecting&&!i.target.hasAttribute(v)&&F(i.target)&&i.target.setAttribute(v,"true")})},{root:null,rootMargin:"0px",threshold:.1});return t.forEach(a=>r.observe(a)),()=>{r.disconnect()}},[e,t]),(0,h.useEffect)(()=>{if(e)return;let r=new MutationObserver(a=>{a.some(s=>s.type==="childList"&&s.addedNodes.length>0||s.type==="attributes"&&s.attributeName==="href")&&n(Array.from(document.querySelectorAll(`a${l.anyZone}:not(${l.prefetch}):not(${l.sameZone}):not(${l.external})`)))});return r.observe(document.body,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["href"]}),()=>{r.disconnect()}},[e]),e?null:(0,T.jsx)(S.default,{dangerouslySetInnerHTML:{__html:JSON.stringify({prefetch:[{eagerness:"moderate",where:w},{eagerness:"immediate",where:j}],prerender:[{eagerness:"conservative",where:w}]})},id:"prefetch-zones-links",type:"speculationrules"})}var x=require("react/jsx-runtime"),Y=process.env.NEXT_PUBLIC_MFE_CURRENT_APPLICATION;function H(e){let{clientConfig:t,isLoading:n}=E(process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG,{removeFlaggedPathsFromDefault:!0}),{isRelativePath:o,zoneOfHref:r}=(0,g.useMemo)(()=>{let i=typeof e=="string"&&e.startsWith("/");return{isRelativePath:i,zoneOfHref:i?t.getApplicationNameForPath(e):null}},[t,e]);return typeof e=="string"&&!e.length?{zoneOfHref:null,isDifferentZone:!1,isLoading:!1}:{zoneOfHref:r,isDifferentZone:!o||(r?Y!==r:!1),isLoading:n}}var Z=(0,g.forwardRef)(({children:e,...t},n)=>{let{prefetchHref:o}=(0,g.useContext)(P),{zoneOfHref:r,isDifferentZone:a,isLoading:i}=H(t.href);function s(){t.href&&o(t.href)}if(a&&r!==null){let{prefetch:d,...C}=t;return(0,x.jsx)("a",{...C,"data-zone":r,onFocus:t.prefetch!==!1?s:void 0,onMouseOver:t.prefetch!==!1?s:void 0,children:e})}return(0,x.jsx)(A.default,{...t,"data-zone":r?"same":"null",prefetch:t.prefetch??(i?!1:void 0),ref:n,children:e})});Z.displayName="MicrofrontendsLink";0&&(module.exports={Link,PrefetchCrossZoneLinks,PrefetchCrossZoneLinksContext,PrefetchCrossZoneLinksProvider,useZoneForHref}); //# sourceMappingURL=client.cjs.map