Skip to content

Commit 5129791

Browse files
authored
[3.2-RC] Document Blazor loadBootResource (#18049)
1 parent 8aa7e33 commit 5129791

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

aspnetcore/host-and-deploy/blazor/webassembly.md

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ description: Learn how to host and deploy a Blazor app using ASP.NET Core, Conte
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 04/23/2020
8+
ms.date: 04/30/2020
99
no-loc: [Blazor, SignalR]
1010
uid: host-and-deploy/blazor/webassembly
1111
---
1212
# Host and deploy ASP.NET Core Blazor WebAssembly
1313

14-
By [Luke Latham](https://github.com/guardrex), [Rainer Stropek](https://www.timecockpit.com), [Daniel Roth](https://github.com/danroth27), and [Ben Adams](https://twitter.com/ben_a_adams).
14+
By [Luke Latham](https://github.com/guardrex), [Rainer Stropek](https://www.timecockpit.com), [Daniel Roth](https://github.com/danroth27), [Ben Adams](https://twitter.com/ben_a_adams), and [Safia Abdalla](https://safia.rocks)
1515

1616
[!INCLUDE[](~/includes/blazorwasm-preview-notice.md)]
1717

@@ -325,3 +325,72 @@ The `--urls` argument sets the IP addresses or host addresses with ports and pro
325325
## Configure the Linker
326326

327327
Blazor performs Intermediate Language (IL) linking on each Release build to remove unnecessary IL from the output assemblies. For more information, see <xref:host-and-deploy/blazor/configure-linker>.
328+
329+
## Custom boot resource loading
330+
331+
A Blazor WebAssembly app can be initialized with the `loadBootResource` function to override the built-in boot resource loading mechanism. Use `loadBootResource` for the following scenarios:
332+
333+
* Allow users to load static resources, such as timezone data or *dotnet.wasm* from a CDN.
334+
* Load compressed assemblies using an HTTP request and decompress them on the client for hosts that don't support fetching compressed contents from the server.
335+
* Alias resources to a different name by redirecting each `fetch` request to a new name.
336+
337+
`loadBootResource` parameters appear in the following table.
338+
339+
| Parameter | Description |
340+
| ------------ | ----------- |
341+
| `type` | The type of the resource. Permissable types: `assembly`, `pdb`, `dotnetjs`, `dotnetwasm`, `timezonedata` |
342+
| `name` | The name of the resource. |
343+
| `defaultUri` | The relative or absolute URI of the resource. |
344+
| `integrity` | The integrity string representing the expected content in the response. |
345+
346+
`loadBootResource` returns any of the following to override the loading process:
347+
348+
* URI string. In the following example (*wwwroot/index.html*), the following files are served from a CDN at `https://my-awesome-cdn.com/`:
349+
350+
* *dotnet.\*.js*
351+
* *dotnet.wasm*
352+
* Timezone data
353+
354+
```html
355+
...
356+
357+
<script src="_framework/blazor.webassembly.js" autostart="false"></script>
358+
<script>
359+
Blazor.start({
360+
loadBootResource: function (type, name, defaultUri, integrity) {
361+
console.log(`Loading: '${type}', '${name}', '${defaultUri}', '${integrity}'`);
362+
switch (type) {
363+
case 'dotnetjs':
364+
case 'dotnetwasm':
365+
case 'timezonedata':
366+
return `https://my-awesome-cdn.com/blazorwebassembly/3.2.0/${name}`;
367+
}
368+
}
369+
});
370+
</script>
371+
```
372+
373+
* `Promise<Response>`. Pass the `integrity` parameter in a header to retain the default integrity-checking behavior.
374+
375+
The following example (*wwwroot/index.html*) adds a custom HTTP header to the outbound requests and passes the `integrity` parameter through to the `fetch` call:
376+
377+
```html
378+
<script src="_framework/blazor.webassembly.js" autostart="false"></script>
379+
<script>
380+
Blazor.start({
381+
loadBootResource: function (type, name, defaultUri, integrity) {
382+
return fetch(defaultUri, {
383+
cache: 'no-cache',
384+
integrity: integrity,
385+
headers: { 'MyCustomHeader': 'My custom value' }
386+
});
387+
}
388+
});
389+
</script>
390+
```
391+
392+
* `null`/`undefined`, which results in the default loading behavior.
393+
394+
External sources must return the required CORS headers for browsers to allow the cross-origin resource loading. CDNs usually provide the required headers by default.
395+
396+
You only need to specify types for custom behaviors. Types not specified to `loadBootResource` are loaded by the framework per their default loading behaviors.

0 commit comments

Comments
 (0)