Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wasm] Add support for baseUrl and mainScriptPath #70375

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/crypto-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function init_crypto(): void {
console.debug("MONO_WASM: Initializing Crypto WebWorker");

const chan = LibraryChannel.create(1024); // 1024 is the buffer size in char units.
const worker = new Worker("dotnet-crypto-worker.js");
const worker = new Worker(`${Module.baseUrl}dotnet-crypto-worker.js`);
kg marked this conversation as resolved.
Show resolved Hide resolved
mono_wasm_crypto = {
channel: chan,
worker: worker,
Expand Down
23 changes: 19 additions & 4 deletions src/mono/wasm/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,33 @@ export function configure_emscripten_startup(module: DotnetModule, exportedAPI:
(typeof (globalThis.document) === "object") &&
(typeof (globalThis.document.createElement) === "function")
) {
module.baseUrl = module.baseUrl || "./";
module.mainScriptPath = module.mainScriptPath || "dotnet.js";

// blazor injects a module preload link element for dotnet.[version].[sha].js
const blazorDotNetJS = Array.from(document.head.getElementsByTagName("link")).filter(elt => elt.rel !== undefined && elt.rel == "modulepreload" && elt.href !== undefined && elt.href.indexOf("dotnet") != -1 && elt.href.indexOf(".js") != -1);
if (blazorDotNetJS.length == 1) {
const hr = blazorDotNetJS[0].href;
console.log("determined url of main script to be " + hr);
const hr = blazorDotNetJS[0].href;

const slashIndex = hr.lastIndexOf("/");

if(slashIndex != -1){
const urlPathName = new URL(hr).pathname;

module.mainScriptPath = urlPathName.substring(slashIndex+1);
module.baseUrl = urlPathName.substring(0, slashIndex);
} else {
module.mainScriptPath = hr;
}

(<any>module)["mainScriptUrlOrBlob"] = hr;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while we're changing this, it might make sense to have a convention for how to tell the runtime what its baseUrl is so it doesn't have to guess. i'm guessing uno could pipe that through

Copy link
Member

@lambdageek lambdageek Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guessing should be removed - and blazor updated to provide a base URL (and possibly also the URL of the worker script - actually depending on how much stuff they mangle we might need some locate function where we can ask for normal names that we know and blazor can tell us what they mangled them to). the guessing was just a hack because I didn't want to coordinate a pair of aspnetcore and runtime PRs

(I don't mean this current PR should remove the guessing and wait until blazor is updated - that should be done separately)

const temp = globalThis.document.createElement("a");
temp.href = "dotnet.js";
console.log("determined url of main script to be " + temp.href);
temp.href = `${module.baseUrl}${module.mainScriptPath}`;
(<any>module)["mainScriptUrlOrBlob"] = temp.href;
}

console.debug("determined url of main script to be " + (<any>module)["mainScriptUrlOrBlob"]);
}

// these could be overriden on DotnetModuleConfig
Expand Down
11 changes: 11 additions & 0 deletions src/mono/wasm/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ export type DotnetModuleConfig = {

config?: MonoConfig | MonoConfigError,
configSrc?: string,

/**
* Base url for the app, defaults to `./` and must contain a trailing slash
*/
baseUrl?: string,

/**
* main script path from baseUrl, defaults to `dotnet.js`
*/
mainScriptPath?: string,

onConfigLoaded?: (config: MonoConfig) => Promise<void>;
onDotnetReady?: () => void;

Expand Down