You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
AspNetStatic now works with Blazor websites, thanks to the new Blazor SSR capability in ASP.NET Core 8.
27
27
28
-
> Blazor pages must not rely on any client-side (JS, WASM) behavior for rendering, or behaviors like showing a placeholder (e.g. a spinner) before rendering the actual content. The rule-of-thumb (for any technology you use with AspNetStatic) is that as long as the content has completed rendering by the time AspNetStatic receives it (via its HttpClient request), it will work fine.
28
+
> :bulb:Blazor pages must not rely on any client-side (JS, WASM) behavior for rendering, or behaviors like showing a placeholder (e.g. a spinner) before rendering the actual content. The rule-of-thumb (for any technology you use with AspNetStatic) is that as long as the content has completed rendering by the time AspNetStatic receives it (via its HttpClient request), it will work fine.
@@ -104,16 +104,16 @@ Keep the following in mind when specifying routes in the `IStaticResourcesInfoPr
104
104
- You can directly specify the pathname of the file to be generated for routes you add to the `PageResources` collection (see `OutFile` property). The only requirement is that the specified path be relative to the destination root folder. If you do not specify a value for `OutFile`, the pathname for the generated file will be determined as demonstrated below.
105
105
- You can specify route parameters for routes you add to the `PageResources` collection. The route parameters are treated as part of the route, and are used in constructing the output file pathname.
106
106
- You can specify a query string for routes you add to the `PageResources` collection (see `Query` property). You can specify the same `Route` with different `Query` values, but you will need to specify a unique `OutFile` value for each instance of that route.
107
-
- You can skip content optimization<sup>1</sup> or choose a specific optimizer type for routes you add to the `PageResources` collection (see `OptimizerType` property). The default optimizer type setting, `OptimizerType.Auto`, automatically selects the appropriate optimizer.
107
+
- You can skip content optimization<sup>1</sup> or choose a specific optimizer type for routes you add to the `PageResources` collection (see `OptimizationType` property). The default optimizer type setting, `OptimizationType.Auto`, automatically selects the appropriate optimizer.
108
108
- You can set the encoding for content written to output files for routes you add to the `PageResources` collection (see `OutputEncoding` property). Default is UTF8.
109
109
110
-
> NOTE: All of the above also applies to routes for CSS, JavaScript, and binary (e.g. image) files specified in the `OtherResources` collection property.
110
+
> :bulb: All of the above also applies to routes for CSS, JavaScript, and binary (e.g. image) files specified in the `OtherResources` collection property.
111
111
112
112
> 1: Content optimization options apply only when content optimization is enabled. Please see the __Content Optimization__ section below for details.
113
113
114
114
### Routes vs. Generated Static Files (page resources)
> ####The same rules apply when links in static files are updated to refer to other generated static pages.
183
+
> :bulb: The same rules apply when links in static files are updated to refer to other generated static pages.
184
184
185
185
186
186
__IMPORTANT NOTE__: In ASP.NET Core, UrlHelper (and the asp-* tag helpers) generate link URIs based on the routing configuration of your app, so if you're using them, be sure to specify an appropriate value for `alwaysDefaultFile`, as shown below. (NOTE: Specify the same value if/when configuring the fallback middleware).
@@ -232,7 +232,7 @@ app.GenerateStaticContent(
232
232
233
233
## Scenarios
234
234
235
-
> ####In all scenarios, ensure that routes for static content are unincumbered by authentication or authorization requirements.
235
+
> :bulb: In all scenarios, ensure that routes for static content are unincumbered by authentication or authorization requirements.
236
236
237
237
### Static Site Generation (Standalone SSG)
238
238
@@ -296,7 +296,7 @@ Now you can use the SSG profile to launch your app in SSG mode (to generate stat
296
296
297
297
In this scenario, you want some of the pages in your ASP.NET Core app to be static, but still want other routes to be served as dynamic content per request (e.g. pages/views, JSON API's, etc.). When the app runs, static (.html) files will be generated for routes you specify. The website will then serve these static files for the specified routes, and dynamic content (as usual) for others.
298
298
299
-
> While static files are being generated, requests to routes for which a static file has not yet been generated will be served as dynamicly generated content (using the source .cshtml page). Once the static file for that route has been generated, it will be used to satisfy subsequent requests.
299
+
> :bulb:While static files are being generated, requests to routes for which a static file has not yet been generated will be served as dynamicly generated content (using the source .cshtml page). Once the static file for that route has been generated, it will be used to satisfy subsequent requests.
300
300
301
301
The configuration options are generally the same as for a standalone static site, except the following differences:
302
302
- The destination root folder must be `app.Environment.WebRoot` (i.e. wwwroot).
@@ -326,7 +326,7 @@ app.GenerateStaticContent(
326
326
app.Run();
327
327
```
328
328
329
-
> #### NOTE: The fallback middleware only re-routes requests for routes that match entries in the `PageResources` collection, and only if a generated static file exists for that route.
329
+
> :bulb: The fallback middleware only re-routes requests for routes that match entries in the `PageResources` collection, and only if a generated static file exists for that route.
330
330
331
331
332
332
#### Periodic Regeneration
@@ -345,79 +345,157 @@ app.GenerateStaticContent(
345
345
346
346
## Content Optimization
347
347
348
-
AspNetStatic automatically minifies HTML content (and any embedded CSS or Javascript) in generated static files; configuration is not required.
349
-
To disable this feature, however, you can specify `true` for the `dontOptimizeContent` parameter.
348
+
Before proceeding, let's clarify what an "_optimizer_" is in AspNetStatic. An optimizer is simply a component that performs some sort of processing on the content retrieved for a static resource (_i.e. page, css, js, image, etc._) An optimizer is called by AspNetStatic after the content of a resource is retrieved, but just before that content is written to the destination file.
349
+
350
+
351
+
### Enabling Optimization
352
+
353
+
The "optimizer" feature in AspNetStatic is enabled by default and requires no configuration to use.
354
+
To disable the feature, pass `true` as the argument for the `dontOptimizeContent` parameter in the `GenerateStaticContent` call.
350
355
```c#
351
356
app.GenerateStaticContent(
352
357
...
353
358
dontOptimizeContent: true);
354
359
```
360
+
This will prevent the `IOptimizerSelector` and any optimizers from being called.
355
361
356
-
> Content optimization does not apply to binary resource types (`BinResource` entries), but is enabled by default (`OptimizerType.Auto`) for all other resource types (`PageResource`, `CssResource`, and `JsResource` entries).
357
362
358
-
### Configuration
363
+
### Optimizer Selector
359
364
360
-
To override the default minification settings used by AspNetStatic, register the appropriate objects in the DI container, as shown below.
365
+
The optimizer to be executed by AspNetStatic for a given resource (_page, css, etc._) is determined by the registered `IOptimizerSelector` component, which by default is `DefaultOptimizerSelector`.
361
366
362
-
> AspNetStatic uses the excellent WebMarkupMin package to implement the minification feature. For details about the configuration settings, please consult the WebMarkupMin [documentation](https://github.com/Taritsyn/WebMarkupMin/wiki/).
367
+
An `IOptimizerSelector` implementation can select an optimizer based on the attributes of the resource (e.g. the resource type, its stated `OptimizationType`, the source or destination file extension or path). `DefaultOptimizerSelector`, for instance, uses the resource type and `OptimizationType` information to select an optimizer.
363
368
364
-
Content optimization can be customized in one of two ways:
365
-
1. Create and register an object that implements `IOptimizerSelector`. In addition to specifying custom optimizer configurations, this option allows you to implement your own custom logic for selecting the optimizer to use for a resource.
To use your own custom selector, implement the `IOptimizerSelector` interface and register it in the DI container. You can derive from `DefaultOptimizerSelector` and override it's methods, to expedite this task.
builder.Services.AddDefaultOptimizers(); // do this if MyCustomOptimizerSelector uses them
379
+
```
380
+
> :bulb: If your custom `IOptimizerSelector` implementation injects one or more of the default optimizers provided by AspNetStatic, you must register them by calling `AddDefaultOptimizers()`.
381
+
382
+
383
+
### Optimizers
384
+
385
+
AspNetStatic supports the following optimizer types:
386
+
-`IMarkupOptimizer`: Called when processing PageResource objects
387
+
-`ICssOptimizer`: Called when processing CssResource objects
388
+
-`IJsOptimizer`: Called when processing JsResource objects
389
+
-`IBinOptimizer`: Called when processing BinResource objects
390
+
391
+
AspNetStatic provides the following "default" implementations for these interfaces: `DefaultMarkupOptimizer`, `DefaultCssOptimizer`, and `DefaultJsOptimizer`. There is no default `IBinOptimizer`.
392
+
393
+
To use a custom optimizer, implement (_and register in DI_) the relevant interface. You can derive and extend the "default" implementation, if you wish.
394
+
For instance, if you want to perform some pre and post processing operations on CSS resources (_in addition to the built-in minification_), derive and extend the DefaultCssOptimizer, like so:
// Call following method if you want DefaultCssOptimizer to use
445
+
// the AspNetStatic defaults for these services, otherwise it will
446
+
// use the WebMarkupMin internal defaults.
447
+
builder.Services.AddDefaultMinifiers();
448
+
```
449
+
450
+
451
+
### Configuring Default Optimizers
452
+
453
+
The default optimizers provided by AspNetStatic (`DefaultMarkupOptimizer`, `DefaultCssOptimizer`, and `DefaultJsOptimizer`) use the `WebMarkupMin` package to minify HTML, CSS and JS content.
454
+
To override the default minification settings used by AspNetStatic, register the appropriate objects as described below.
455
+
456
+
> :flashlight: For details about WebMarkupMin configuration settings, please consult the [WebMarkupMin documentation](https://github.com/Taritsyn/WebMarkupMin/wiki/).
457
+
458
+
AspNetStatic uses the default `WebMarkupMin` configuration settings (_determined internally by WebMarkupMin_) for minifying HTML, XHTML, and XML content. To override this behavior, register one or more of the following configuration objects:
AspNetStatic uses `KristensenCssMinifier` for `ICssMinifier`, and `CrockfordJsMinifier` for `IJsMinifier` by default. To override this behavior, register alternative implementations:
0 commit comments