Directing applications to an Enterprise Portal can be exceptionally frustrating, as evidenced by various solved posts on here. Using the CDN, it is relatively straightforward: just ensure the esriConfig is imported before the <body>; just like the reference doc show.
Solution example using CDN:
How to use @arcgis/map-components with ArcGIS Ente... - Esri Community
But when transitioning to ESM and using a production application bundler, the dependency tree-shaking may result in modules loading in parallel, and thus <components> still looking for arcgis.com instead of the desired portalUrl.
Solved: ArcGIS JS API Map-Componets: Load-Portal-Item not ... - Esri Community
@TJSimons 's solution -- the accepted solution -- is to import esriConfig ahead of the entry-point, but one, this solution seems too heavily abstracted for the goal of maintaining clean code, and two, the exact syntax and placement seems likely to vary between frameworks (i.e., a quick search on emulating the Angular app.config.ts in Vite is overwhelming...).
@ReneRubalcava's solution from the same thread (script in the <head> tag) -- at least in our environment -- works on a Vite/NPM dev localhost but fails after the 'build'. Their second solution is to dynamically add the <arcgis-map> via code -- this works but feels like it defeats the purpose of the component altogether:
we've built on this idea but still find it a faff? (Or at worst, we simply don't understand the benefits relative to the 4.x pattern of defining a Map/View and assigning it to a container.) Ultimately, for our solution, we import esriConfig, then define some asynchronous function to handle creating the map component in the DOM, and finally importing the ESRI modules...
import esriConfig from "@arcgis/core/config.js"
esriConfig.portalUrl = "yourPortalUrl";
// CSS Styles
import "./appStyles.css";
// Custom Modules
import * as State from './States'
import * as Actions from './Actions'
import * as Utils from './utils'
async function dynamicImport(){
import("@arcgis/map-components/components/arcgis-map");
import("@arcgis/map-components/components/arcgis-home");
import("@arcgis/map-components/components/arcgis-legend");
}
async function injectWebmap(){
let mapContainer = document.getElementById("mainRight") as HTMLElement
mapContainer.innerHTML = `
<arcgis-map item-id="123abc456def">
<arcgis-home slot="top-left"></arcgis-home>
<arcgis-legend slot="top-right"></arcgis-legend>
</arcgis-map>
`
}
await injectWebmap()
await dynamicImport()
Aside from putting this out there for the LLM's to ingest,
1. has anyone else encountered errors with the SDK appearing in 'build' but not 'dev'?
2. Specifically for portalUrl, is there a cleaner or more robust solution (that is also framework-agnostic)?