1
votes

TL;DR: is there a Target during build/publish (in asp.net core Blazor app) for JS files compression which I can use in csproj to run my script before this Target?

Background:
I have Blazor frontend application which is loaded to different web application (different domains). So: main application loads many other applications and one of these applications is Blazor app (hosted at different URL).

What I did: I load manually _framework/blazor.webassembly.js with autostart property set to false and start Blazor manually:

  Blazor.start({
    loadBootResource: (
      type: string,
      name: string,
      defaultUri: string,
      integrity: string
    ) => {
      const newUrl = ...;// here I can make some URL replacements for defaultUri
      // so my `newUrl` points to place where Blazor app is hosted
      return newUrl;
    },
  })

similar as described here: https://docs.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/webassembly?view=aspnetcore-3.1#custom-boot-resource-loading

It works correctly but one file is NOT loaded through loadBootResource. It is blazor.boot.json. Code which loads this file is located in blazor.webassembly.js (fetch("_framework/blazor.boot.json"...): https://github.com/dotnet/aspnetcore/blob/master/src/Components/Web.JS/src/Platform/BootConfig.ts#L6

Problem is described also in this issue https://github.com/dotnet/aspnetcore/issues/22220

There is also possible solutions suggested by me: https://github.com/dotnet/aspnetcore/issues/22220#issuecomment-683783971

I decided to replace content of blazor.webassembly.js (replace fetch("_framework/blazor.boot.json" with fetch(${someGlobalVariableForSpecificDomainURL}/_framework/blazor.boot.json) but there are also compressed files (GZ and BR). How to run my script for replacement before compression is started but after JS file is generated? Is it possible? Is there any Target which I can use in csproj? I do not want to disable dotnet files compression and I do not want to overwrite compressed files (compress by my own).

My current csproj contains something like this (script is started after compression so too late):

  <Target Name="ReplacementDuringBuild" BeforeTargets="Build">
    <Exec WorkingDirectory="$(MyDirWithScripts)" Command="node replace.js --output=$(MSBuildThisFileDirectory)$(OutDir)" />
  </Target>

  <Target Name="ReplacementDuringPublish" AfterTargets="AfterPublish">
    <Exec WorkingDirectory="$(MyDirWithScripts)" Command="node replace.js --output=$(MSBuildThisFileDirectory)$(PublishDir)" />
  </Target>

Thanks for a help or any suggestion! If there is also another workaround to solve main issue, then I will be glad to see it (base tag does not work; replacement of fetch also is not so good).

1

1 Answers

0
votes

I didn't find any fitting Target for my purpose. Code from question worked correctly but only with my own compression. So I reverted this and finished with overriding window.fetch to resolve main issue. If URL contains blazor.boot.json then I modify URL and pass it to original fetch. After all files are loaded, I restore original fetch. Similar to code suggested here: https://github.com/dotnet/aspnetcore/discussions/25447

    const originalFetch = window.fetch;
    window.fetch = function(requestInfo, options) {
        if (requestInfo === '_framework/blazor.boot.json') {
            return originalFetch('https://example.com/myCustomUrl/blazor.boot.json', options);
        } else {
            // Use default logic
            return originalFetch.apply(this, arguments);
        }
    };