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

Blazor: Static assets not found when using server/client projects at publish. [Includes workaround] #12073

Closed
Bartmax opened this issue Jul 10, 2019 · 6 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@Bartmax
Copy link
Contributor

Bartmax commented Jul 10, 2019

Describe the bug

referencing a file like <img src="/images/test.jpg" /> will work on development but not when publishing.
This means that you cannot dev and publish your project and reference the same files using the same request path, making the dev experience and publishing a pain.

To Reproduce

Steps to reproduce the behavior:

  1. Use preview-6
  2. add img tag on a .razor component that points to image on wwwroot at client project
  3. run app in development and go to said component
  4. See image is displayed correctly.
  5. Publish project dotnet publish -o {destinationFolder}
  6. Run published app
  7. See 404 when requesting the static asset.

Expected behavior

Both in development and publish the reference URL should match via the UseClientSideBlazorFiles middleware

Screenshots

image

Additional context

The problem looks like the static file provider is configured to look into a wrong? dist folder.
Inside the dist folder there are NuGet static assets, so I think this is not wrong but we are missing some glue or consistency.

Workaround

on client project csproj, use the "newer" way to set static file path like this:

<PropertyGroup>
    <StaticWebAssetBasePath 
        Condition="$(StaticWebAssetBasePath) == ''">_content/{PackageId}</StaticWebAssetBasePath>
</PropertyGroup>

Note: PackageId is the name of your client project, something like WebApplication1.Client

UseStaticSideBlazorFiles middleware still fails to find the assets on this location, so you can use another staticfilesmiddleware like this:

On server project:

app.UseStaticFiles(new StaticFileOptions
{
    RequestPath = "",
    FileProvider = 
        new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/_content/{PackageId}"))
});
@iAmBipinPaul
Copy link

[Blazor] Preview 6 blazorhosted project publish is broken #11185

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Jul 11, 2019
@Bartmax
Copy link
Contributor Author

Bartmax commented Jul 11, 2019

For completeness and to help others with this issue, I include the complete workaround on server Startup.cs file.:

var publishFolder = Path.Combine(Directory.GetCurrentDirectory(), $"wwwroot/_content/{ClientProjectId}");
if (Directory.Exists(publishFolder))
{
    app.UseStaticFiles(new StaticFileOptions
    {
        RequestPath = "",
        FileProvider = new PhysicalFileProvider(publishFolder)
    });
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapDefaultControllerRoute();
        endpoints.MapFallbackToClientSideBlazor<Client.Startup>($"_content/{ClientProjectId}/index.html");
    });
}
else
{

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapDefaultControllerRoute();
        endpoints.MapFallbackToClientSideBlazor<Client.Startup>("index.html");
    });
}

@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @Bartmax.
This should be fixed now and will roll out as part of the upcoming Preview 7 release.

@endeffects
Copy link

endeffects commented Jul 25, 2019

I still have this issue with preview7 when running a blazor client side application in a docker container.

@mkArtakMSFT
Copy link
Member

@javiercn do we have a separate issue where we track the Docker specific scenario?

@javiercn
Copy link
Member

@mkArtakMSFT No.

We have an issue for static web assets and docker, which partially impacts blazor client-side. The reason is currently blazor client-side uses two ways of bringing assets into the project.

  • For the app it plugs in specific providers through UseBlazorClientSideFiles()
  • For referenced libraries through static web assets it goes the normal static web assets route.

Publishing was broken for blazor client-side in preview6, but we fixed it in preview7.

I added a tracking issue for blazor client-side specific bits #12588

The staticweb assets piece I consider them tracked by #11609

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

5 participants