Closed nakigoe closed 1 year ago
@nakigoe
I guessed your Program.cs
maybe like below:
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
...
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddGoogleAnalytics("UA-111742878-2");
...
await builder.Build().RunAsync();
If that is true, you must rewrite your Program.cs
to be like the below:
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
...
// 👇 Please extract the service registration code into a static method named "ConfigureServices".
// (Be careful that the static method name must be "ConfigureServices" exactly. Don't miss-spelling.)
ConfigureServices(builder.Services, builder.HostEnvironment);
...
await builder.Build().RunAsync();
static void ConfigureServices(IServiceCollection services, IWebAssemblyHostEnvironment hostEnvironment)
{
services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(hostEnvironment.BaseAddress) });
services.AddGoogleAnalytics("UA-111742878-2");
}
Could you try that out?
Or, if my guessing is false, please disclose your Program.cs
code to me to reproduce and investigate this problem.
@nakigoe Refer Program.cs of ilovedotnet. I use use the Blazor.Analytics package in my project and it works fine. If you still face an issue please share your Program.cs
or feel free to close this if your issue is resolved as this is in open sate for long time awaiting your response.
@jsakamoto 様 いつもお世話になっております。 Nakigoeです、凄く感謝いたします。
What I loved about Your project, is that the index.html
files that are generated are claimed to be hidden from the user.
Are they?
The NuGet is working for the basic project, but
1) the initial loading time with VPN enabled seems to be slower with this NuGet, it stucks for a long time on 95% and then after about 5 seconds more finally loads. That's odd for the basic template. Any recommendations? The initial loading is unacceptably slow when the VPN connection is enabled. I know that's the way Blazor WebAssembly operates, but is there a smart workaround?
2) 日本語が話せますけれど、Could You please utilize the LanguageTool
or Grammarly
or an alternative to correct your English to have clearer instructions in all of your articles
My code for a basic template: https://github.com/nakigoe/blazorwasm
The website that is generated: https://blazorwasm.nakigoe.org/
Does Your app work with Cookies, Globalization, and Localization? (the standard, a bit simplified Microsoft Globalization and Localization via the .resx files)
I do have a serious Blazor WebAssembly website for Your NuGet: https://nakigoe.org
Might I have your email to send the program.cs
Please cast an eye on my website:
Now I have an error in GitHub Actions when trying to utilize Your NuGet on that larger website. It seems to be about JSinterop
Compressing Blazor WebAssembly publish artifacts. This may take a while...
angelblaze -> /home/runner/work/angelblaze/angelblaze/bin/Release/net7.0/publish/
Start fetching...[http://127.0.0.1:5050/]
Getting http://127.0.0.1:5050/...
Error: ROR] The HTTP status code was not OK. (it was (500)InternalServerError.)
System.InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.
at Toolbelt.Blazor.WebAssembly.PreRendering.Build.WebHost.Services.ServerSideRenderingJSRuntime.InvokeAsync[TValue](String identifier, Object[] args) in C:\Projects\My\Blazor\PreRendering\BlazorWasmPreRendering.Build.WebHost\Services\ServerSideRenderingJSRuntime.cs:line 39
at Microsoft.JSInterop.JSRuntimeExtensions.InvokeAsync[TValue](IJSRuntime jsRuntime, String identifier, Object[] args)
INFORMATION
=============================
The crawler encountered errors and/or warnings.
Error: X [ERROR] JavaScript interop calls cannot be issued at this time
-----------------------------
if you are calling JavaScript code in "OnInitializedAsync()" like this:
@inject IJSRuntime JS
...
protected async Task OnInitializedAsync() {
await this.JS.InvokeVoidAsync("...", ...);
...
Please consider changing the above code to like below.
@inject IJSRuntime JS
...
protected async Task OnAfterRenderAsync(bool firstRender) {
if (firstRender) {
await this.JS.InvokeVoidAsync("...", ...);
...
For more detail, see also: https://docs.microsoft.com/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet#prerendering
TIPS
-----------------------------
If you want to keep running the pre-rendering server process for debugging it on live, you can do that by setting the "BlazorWasmPrerenderingKeepServer" MSBuild property to "true".
ex) dotnet publish -p:BlazorWasmPrerenderingKeepServer=true
Fetching complete.
/home/runner/.nuget/packages/blazorwasmprerendering.build/2.0.0-preview.8/build/BlazorWasmPreRendering.Build.targets(62,5): error MSB3073: The command "dotnet "/home/runner/.nuget/packages/blazorwasmprerendering.build/2.0.0-preview.8/build/../tools/net7.0/BlazorWasmPreRendering.Build.dll" -a "angelblaze" -t "angelblaze.App" --selectorofrootcomponent "#app,app" --selectorofheadoutletcomponent "head::after" -p "/home/runner/work/angelblaze/angelblaze/bin/Release/net7.0/publish" -i "/home/runner/work/angelblaze/angelblaze/obj/Release/net7.0/." -m "" -f "net7.0" --serviceworkerassetsmanifest "" --environment "Prerendering" --emulateauthme "true" --locale "en" -o "IndexHtmlInSubFolders" -u "" -r "Static" --serverport "5050-5[99](https://github.com/nakigoe/angelblaze/actions/runs/5104513967/jobs/9175488795#step:5:100)9" --bwapoptionsdllext "bin"" exited with code 1. [/home/runner/work/angelblaze/angelblaze/angelblaze.csproj]
Error: Process completed with exit code 1.
@nakigoe
Now I have an error in GitHub Actions when trying to utilize Your NuGet on that larger website. It seems to be about JSinterop
This error will happen if you use the JavaScript interop feature of Blazor in the OnInitializedAsync
life cycle. You can only call JavaScript code after OnAfterRenderAsync
is invoked. That is a limitation of the BlazorWasmPreRendering.Build
package.
Please understand that the pre-rendering process runs outside the web browser, inside any PC locally, like a usual console app. Therefore, there is no JavaScript engine during the publishing process. This is the reason for the limitation that the JavaScript interop can not use in the OnInitializedAsync
life cycle on pre-rendering.
日本語
このエラーは、「OnInitializedAsync」ライフ サイクルで Blazor の JavaScript 相互運用機能を使用する場合に発生します。 JavaScript コードは、「OnAfterRenderAsync」が呼び出された後にのみ呼び出すことができます。 これは、BlazorWasmPreRendering.Build
パッケージの制限です。
プリレンダリング プロセスは、通常のコンソール アプリと同様に、Web ブラウザーの外部、ローカルの PC 内で実行されることを理解してください。 したがって、公開プロセス中に JavaScript エンジンは存在しません。 これが、プリレンダリング時の OnInitializedAsync
ライフサイクルで JavaScript 相互運用機能が使用できない制限の理由です。
@nakigoe
is that the index.html files that are generated are claimed to be hidden from the user. Are they?
No. I've not intended to tell users that the generated HTML files are hidden from users. It might be your misunderstanding due to my English is terrible. I'll appreciate it if you pointed out where a paragraph of this package's document (README) lets you feel so.
日本語
生成された HTML ファイルがユーザーに対して非表示になることをユーザーに伝えるつもりはありません。 私の英語がひどいので誤解があるかもしれません。 このパッケージのドキュメント (README) のどの段落でそう感じられるかを指摘していただければ幸いです。
@nakigoe
the initial loading time with VPN enabled seems to be slower with this NuGet, it stucks for a long time on 95% and then after about 5 seconds more finally loads.
I can't imagine that the loading time of a pre-rendered Blazor application will be significantly affected by whether it is connected to a VPN or not.
Could you verify the following link, Microsoft's demo site for the QuickGrid component, is also slow or not in your VPN connection environment? This site also utilizes the BlazorWasmPreRendering.Build
package.
https://aspnet.github.io/quickgridsamples/
日本語
プリレンダリングされた Blazor アプリケーションの読み込み時間が、VPN に接続されているかどうかによって大きな影響を受けるとは想像できません。
次のリンク (QuickGrid コンポーネントの Microsoft のデモ サイト) も、あなたの VPN 接続環境で遅いかどうかを確認していただけますか? このサイトも、BlazorWasmPreRendering.Build
パッケージを利用しています。
@nakigoe
Does Your app work with Cookies, Globalization, and Localization? (the standard, a bit simplified Microsoft Globalization and Localization via the .resx files)
If you mean the "app" is this "BlazorWasmPreRendering.Build" package, the answer might be "Yes", but it depends on what you expect.
Please understand the working principle of the BlazorWasmPreRendering.Build
package. The BlazorWasmPreRendering.Build
package just makes your Blazor WebAssembly app run as a Blazor Server app with some tricks. Therefore, there is no difference in the pre-rendering process with the usual Blazor Server pre-rendering. It might be hard to understand if you are unfamiliar with the server-side pre-rendering on Blazor Server apps, but The BlazorWasmPreRendering.Build
package isn't doing something special over the usual Blazor Server apps doing.
From that perspective, the BlazorWasmPreRendering.Build
package supports all features as usual Blazor Server apps with server-side pre-rendering, such as Cookies, Globalization, and Localization.
(However, the Cookie support doesn't make sense during the pre-rendering process because the pre-rendering process doesn't involve any HTTP request. Cookies are always empty during the pre-rendering process.)
日本語
アプリは Cookie、グローバリゼーション、ローカリゼーションに対応していますか? (.resx ファイルを使用した、標準の少し簡略化された Microsoft グローバリゼーションおよびローカリゼーション)
「アプリ」がこの「BlazorWasmPreRendering.Build」パッケージであるということであれば、答えは「はい」かもしれませんが、それはあなたが何を期待しているかによって異なります。
BlazorWasmPreRendering.Build
パッケージの動作原理を理解してください。 BlazorWasmPreRendering.Build
パッケージは、いくつかのトリックを使用して Blazor WebAssembly アプリを Blazor Server アプリとして実行するだけです。 したがって、プリレンダリング プロセスには、通常の Blazor Server のプリレンダリングと違いはありません。 Blazor Server アプリでのサーバー側のプリレンダリングに慣れていない場合は、理解するのが難しいかもしれませんが、BlazorWasmPreRendering.Build
パッケージはそれ以上の特別なことをしているわけではありません。
その観点から、BlazorWasmPreRendering.Build
パッケージは、サーバー側の事前レンダリングを備えた通常の Blazor サーバー アプリと同様に、Cookie、グローバリゼーション、ローカリゼーションなどのすべての機能をサポートします。
(ただし、プリレンダリング プロセスには HTTP リクエストが含まれないため、プリレンダリング プロセス中は Cookie サポートは意味を持ちません。プリレンダリング プロセス中、Cookie は常に空です。)
Your English is great、英語はも大丈夫でいらっしゃるのです。
@nakigoe
Could You please utilize the LanguageTool or Grammarly or an alternative to correct your English to have clearer instructions in all of your articles
Thank you for your feedback! Actually, I'm a paid user of the grammarly.com premium subscription for now, so I'll refine the README document of the "BlazorWasmPreRendering.Build" package by using those tools later. And of course, I will always welcome it if you correct the README of the "BlazorWasmPreRendering.Build" and send pull requests for me.
@nakigoe
Might I have your email to send the
program.cs
You can send your files to me from the following link.
@nakigoe If you or your app is hard to make pre-rendered by using "BlazorWasmPreRendering.Build" due to its architecture, I can propose another method based on another architecture to pre-render it.
That method uses the react-snap
. This approach runs your Blazor WebAssembly app as is, browses it with a built-in Chromium browser, collects rendered HTML in a browser, and saves them to static pre-rendered HTML files during publishing.
For more details about this method, please follow the article written by Niels Swimburger.NET 🍔 below. https://dev.to/swimburger/pre-render-blazor-webassembly-at-build-time-to-optimize-for-search-engines-359c
Generally, this method is more complex than adding a single NuGet package because you have to write a few script codes. However, in some cases, making the app to be server-side renderable to correspond to the "BlazorWasmPreRendering.Build" package will be more complex than using react-snap
. So it will be better to know the using react-snap
way to the second option.
HTML that is generated has errors according to https://validator.w3.org/nu/
Trailing slash on void elements has no effect and interacts badly with unquoted attribute values.
The automatically generated trailing slash inside HTML tags prevents images from displaying when sharing the website on social networks (LinkedIn)
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0"/>
<title>Blazor WebAssembly .NET 7 Starting Template</title>
<meta name="description" content="Blazor WebAssembly .NET 7 production-ready starting template, coming from GitHub Actions and Vercel. Compression, linking, pre-rendering and analytics enabled."/>
<base href="/"/>
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet"/>
<link href="css/app.css" rel="stylesheet"/>
<link href="nakigoe.styles.css" rel="stylesheet"/>
<link rel="image_src" href="https://nakigoe.org/_SRC/nakigoe-academy-night-2048.jpg" type="image/jpeg"/>
<meta name="image_width" content="2048"/>
<meta name="image_height" content="2048"/>
<meta property="og:image:secure_url" content="https://nakigoe.org/_SRC/nakigoe-academy-night-2048.jpg"/>
<meta property="og:image" content="https://nakigoe.org/_SRC/nakigoe-academy-night-2048.jpg"/>
<meta property="og:image:type" content="image/jpeg"/>
<meta property="og:image:width" content="2048"/>
<meta property="og:image:height" content="2048"/>
<meta name="theme-color" content="#260040"/>
<meta name="apple-mobile-web-app-status-bar-style" content="#260040"/>
<meta name="msapplication-navbutton-color" content="#260040"/>
<meta name="msapplication-TileColor" content="#260040"/>
<meta name="msapplication-TileImage" content="_SVG/icon.svg"/>
<link rel="icon" href="_SVG/icon.svg" sizes="any" type="image/svg+xml"/>
<link rel="apple-touch-icon-precomposed" href="_SVG/icon.svg" sizes="any" type="image/svg+xml"/>
<link rel="apple-touch-startup-image" href="_SVG/icon.svg" type="image/svg+xml"/>
@nakigoe I split this issue to #31 to be easy to handle and save me from confusion.
Your extension is working for my website https://nakigoe.org, confirmed I did not try it in the giant complex website with APIs, Libraries, and databases, it is the next target for testing.
Here are my messages on publish from GitHub repo: