jsakamoto / BlazorWasmPreRendering.Build

When you publish your Blazor Wasm app, this package pre-renders and saves the app as static HTML files in your public folder.
https://jsakamoto.github.io/BlazorWasmPreRendering.Build/
Mozilla Public License 2.0
261 stars 15 forks source link

Partially pre-rendering #5

Closed pmeems closed 2 years ago

pmeems commented 2 years ago

This is not a real issue, more a question. I have an Azure Static WebApp with Azure Functions and I'm investigating if your package will work in my scenario. I tried it and my index and counter pages are pre-rendered successful, but with my FetchData page the package wants to pre-render the data as well. But during publish the Function is not up yet so it generates an error message. And I don't want to pre-render the data.

Can this package pre-render all static data and leave the dynamic data as is? If so how do I do this?

My code is at https://github.com/pmeems/BlazorWasmTailwind and my static website runs at https://purple-meadow-079619e03.azurestaticapps.net/

jsakamoto commented 2 years ago

If your code can detect that it is running on the pre-rendering process or not, is it useful?

For example, if the IWebAssemblyHostEnvironment.Environment can return "PreReindering" while pre-rendering process, you may be able to avoid API calls like this.

@inject IWebAssemblyHostEnvironment HostEnv
...
@code {
    ...
    protected override async Task OnInitializedAsync()
    {
        try
        {
            // 👇 "If the code is running on the pre-rendering process, then don't call APIs."
            if (HostEnv.Environment == "Prerendering") return;

            Forecasts = await _http.GetFromJsonAsync<WeatherForecast[]>("/api/WeatherForecast") ?? new WeatherForecast[] { };
        }

Currently, the "BlazorWasmPreRendering.Build" can't provide information on whether it is running on the pre-rendering process or not. But recently, I'm considering implementing that feature.

jsakamoto commented 2 years ago

@pmeems

I did it.

The document is following.

I hope this feature is helpful for you.

pmeems commented 2 years ago

WOW! Just made the suggested changes and updated the package and now it is working as I expect!

Good job!

I also have a question about sitemaps, but will create a new issue for that.

pmeems commented 2 years ago

I might have closed this question too soon. After deploying to Azure Static Web App, I can't see the pre-rendering starting. Or perhaps I should update my yaml: https://github.com/pmeems/BlazorWasmTailwind/blob/main/.github/workflows/azure-static-web-apps-purple-meadow-079619e03.yml Perhaps my output_location: "wwwroot" should be updated, but I'm not sure.

I would really appreciate it if you would suggest how to get this working on Azure Static Web App.

pmeems commented 2 years ago

Sorry for the confusion. I must read better. I just did everything that was in the updated documentation, not realizing it wasn't all necessary. After removing <BlazorWasmPrerenderingEnvironment>Production</BlazorWasmPrerenderingEnvironment> Azure deploys correctly as well.