0

I have a pretty basic Javascript function stored in a separate JS file that sets the page title for a new Blazor app that looks like this:

window.setTitle = (title, icon) => {

    //title
    document.title = title;
    $("#page_title").html(title);

    //page icon
    $("#page_icon").removeClass().addClass('fad fa-' + icon + ' fa-2x');
}

It all works great, except on a cold startup of the app when hitting it through a browser. When I try to hit the site and it hasn't been hit in a little while, I'll end up with the following in the console:

Microsoft.JSInterop.JSException: Could not find 'setTitle' ('setTitle' was undefined).
Error: Could not find 'setTitle' ('setTitle' was undefined).
    at https://example.com/_framework/blazor.webassembly.js:1:1287
    at Array.forEach (<anonymous>)
    at e.findFunction (https://example.com/_framework/blazor.webassembly.js:1:1247)
    at b (https://example.com/_framework/blazor.webassembly.js:1:2989)
    at https://example.com/_framework/blazor.webassembly.js:1:3935
    at new Promise (<anonymous>)
    at Object.beginInvokeJSFromDotNet (https://example.com/_framework/blazor.webassembly.js:1:3908)
    at Object.w [as invokeJSFromDotNet] (https://example.com/_framework/blazor.webassembly.js:1:64218)
    at _mono_wasm_invoke_js_blazor (https://example.com/_framework/dotnet.5.0.6.js:1:190800)
    at do_icall (<anonymous>:wasm-function[10596]:0x194e4e)
   at Microsoft.JSInterop.JSRuntime.<InvokeAsync>d__15`1[[System.Object, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)
   at App.Web.Portal.Client.Shared.PageTitle.OnAfterRenderAsync(Boolean firstRender)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
d.printErr @ blazor.webassembly.js:1

The part that confuses me is, if I just do a hard refresh (CTRL + Shift + R), then everything loads up perfectly fine and runs great without any changes or deployments. Given that, I don't really know what to do or where to look on how to correct this.

Has anyone else seen this before or have any tips on how to prevent it? I've been doing .Net since ~2003, but I'm new to Blazor as of 2021, so I've been learning a LOT, but this one has me stumped.

Thanks in advance!

Scott Salyer
  • 2,165
  • 7
  • 45
  • 82
  • Where do you load the js file containing setTitle? –  Jun 15 '21 at 19:36
  • It's inside "wwwroot\index.html" and is linked as a standard JS file: ``, right above the closing `

    ` tag - it's the last script file linked.

    – Scott Salyer Jun 15 '21 at 19:40
  • Hi, where are you calling `setTitle` through JsInterop in your Blazor SPA? And define how you `cold restart` your application. – MrC aka Shaun Curtis Jun 15 '21 at 22:02
  • 1
    The whole app is secured (meaning no public pages except the login screen), so the login is where the error is at, but I'll have to wait/see if it does it again to see if a stack trace will tell me where it's firing from. The login page itself doesn't use that function, so when you asked that a light bulb clicked and now I'm curious :-) regarding a cold start, that just means it's hosted via IIS and no one has been to it for at least a few days, so IIS is spinning it up for the first time. More to come as soon as I can reproduce it again! – Scott Salyer Jun 16 '21 at 16:04

1 Answers1

1

Been having similar issues getting my Blazor Project to find the .js files

The below link fixed all issues. However then with no code changes, it started producing errors again; 'methods.saveAsFile was undefined'.

Control Shift R on the browser for a full reload fixed the issue. It wasn't the code in this case.

Could not find property of window when doing JS interop with Blazor

Zittlejohn
  • 11
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 22 '21 at 01:49