3

I'm trying to get this to work as per Microsoft's documentation.

The below script code works perfectly in an existing React app and I'm trying to port the app to Blazor. The script loads and inits - and I can see the Iframe being correctly loaded into the browser DOM. However the script (Apple's mapkit.js script) errors out with Attempted to assign to readonly property in the migrateStateTo function, trying to set t.tint = this.node.tint.

MapkitHelper.js

import "https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js";

export function initialiseMapkit(token, element) {
  // this call seems to work
  mapkit.init({
    authorizationCallback: function(done) {
      done(token)
    }  
  })
  // from here on things seem to go south no matter what I do
  var map = new mapkit.Map(element)
}

MapkitRenderer.razor

@implements IAsyncDisposable
@inject IJSRuntime JS

<div @ref="mapkitElement"></div>

@code {
  public string? token = Environment.GetEnvironmentVariable("MAPKIT_JS_TOKEN");

  private ElementReference mapkitElement;
  private IJSObjectReference? mapModule;

  protected override async Task OnAfterRenderAsync(bool firstRender) {
    if (firstRender) {
      mapModule = await JS.InvokeAsync<IJSObjectReference>("import", "./MapkitHelper.js");
      await mapModule.InvokeVoidAsync("initialiseMapkit", token, mapkitElement);
    }
  }

  async ValueTask IAsyncDisposable.DisposeAsync() {
    if (mapModule is not null) {
      await mapModule.DisposeAsync();
    }
  }
}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
robuk
  • 85
  • 1
  • 5
  • Did you figure this one out? Seeing the same error with similar setup – thomasstephn Jan 09 '22 at 17:46
  • Nevermind, I did, in my case I was importing my library twice, redefining the same name for it twice – thomasstephn Jan 09 '22 at 18:02
  • @robuk did you ever get this working. if yes, please provide sample code. – Barry MSIH Jun 29 '22 at 13:01
  • @thomasstephn could you share a working example? – Barry MSIH Jun 29 '22 at 13:02
  • @BarryMSIH to be honest I abandoned this and Blazor completely - the project relied on a number of external modules and trying to get these (or equivalents/replacements) to work with Blazor just turned into an exercise in futility, especially as C#/JS/TS is not really my day job. – robuk Jun 30 '22 at 14:07

1 Answers1

1

I encountered this problem because I loaded mapkit.js as a module.

import "https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js";

Loading mapkit.js as a module results in an exception because mapkit.js is not compatible with JavaScript's strict mode. You must instead load mapkit.js in sloppy mode.

<script src="https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js"></script>

Based on https://developer.apple.com/forums/thread/706389

Barry MSIH
  • 3,525
  • 5
  • 32
  • 53