Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Angular2 PreRendering not working correctly when base href set to subdirectory #1049

Closed
alterius opened this issue Jun 21, 2017 · 3 comments
Closed

Comments

@alterius
Copy link

I'm trying to make PreRendering work with an Angular2 app based in a subdirectory.

Steps to recreate:

  1. Created a new project from the Microsoft SPA Templates dotnet new angular -n DashboardTest (dotnet new --install Microsoft.AspNetCore.SpaTemplates::* to install the templates package)
  2. Removed all routing from Startup.cs and replaced with app.UseMvc();. This means that only routes I've explicitly defined will be mapped.
  3. Added [Route("/dashboard/{*angularRoute}")] attribute to the Index() action on the HomeController to catch all requests to my chosen subdirectory of /dashboard and pre-render the angular app (the Home/Index view already contains <app asp-prerender-module="ClientApp/dist/main-server">Loading...</app> for pre-rendering.)
  4. Updated the base href tag in _Layout.cshtml to <base href="/dashboard" /> to correctly configure the angular app to treat /dashboard as it's root.

On the surface everything appears to work beautifully, however behind the scenes the pre-rendering has broken. To prove this, load view-source:http://localhost:5000/dashboard/counter (in Chrome) - you'll see the rendered output is actually the /home route, not the /counter route as expected.

Suggested fix:
Add public string BaseHref { get; set; } and unencodedPathAndQuery = !string.IsNullOrEmpty(BaseHref) && unencodedPathAndQuery.StartsWith(BaseHref) ? unencodedPathAndQuery.Substring(BaseHref.Length) : unencodedPathAndQuery; to PrerenderTagHelper

@ADefWebserver
Copy link

This is the fix that is already in the Dev branch that should address the issue:
#961 (comment)

@alterius
Copy link
Author

#961 appears to solve a problem with the Angular app root directory - this isn't an issue for me. It's the pre-rendering that's not working correctly.

@SteveSandersonMS
Copy link
Member

There are indeed some known issues with workarounds. Please see #488 and #571.

Closing this one as duplicate.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants