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