Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit44d9539

Browse files
committed
refactor(@angular/ssr): replace logic to determine the original URL
Using `currentNavigation` might not work in all cases as this will be null when the navigate has already completed.
1 parent3040b77 commit44d9539

File tree

1 file changed

+13
-17
lines changed
  • packages/angular/ssr/src/utils

1 file changed

+13
-17
lines changed

‎packages/angular/ssr/src/utils/ng.ts‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import{APP_BASE_HREF,PlatformLocation}from'@angular/common';
9+
import{LocationStrategy}from'@angular/common';
1010
import{
1111
ApplicationRef,
1212
typePlatformRef,
@@ -21,7 +21,7 @@ import {
2121
platformServer,
2222
ɵrenderInternalasrenderInternal,
2323
}from'@angular/platform-server';
24-
import{ActivatedRoute,Router}from'@angular/router';
24+
import{ActivatedRoute,Router,UrlSerializer}from'@angular/router';
2525
import{Console}from'../console';
2626
import{joinUrlParts,stripIndexHtmlFromURL}from'./url';
2727

@@ -60,12 +60,12 @@ export async function renderAngular(
6060
serverContext:string,
6161
):Promise<{hasNavigationError:boolean;redirectTo?:string;content:()=>Promise<string>}>{
6262
// A request to `http://www.example.com/page/index.html` will render the Angular route corresponding to `http://www.example.com/page`.
63-
consturlToRender=stripIndexHtmlFromURL(url).toString();
63+
consturlToRender=stripIndexHtmlFromURL(url);
6464
constplatformRef=platformServer([
6565
{
6666
provide:INITIAL_CONFIG,
6767
useValue:{
68-
url:urlToRender,
68+
url:urlToRender.href,
6969
document:html,
7070
},
7171
},
@@ -96,31 +96,27 @@ export async function renderAngular(
9696
applicationRef=awaitbootstrap({ platformRef});
9797
}
9898

99-
constenvInjector=applicationRef.injector;
100-
constrouter=envInjector.get(Router);
101-
constinitialUrl=router.currentNavigation()?.initialUrl.toString();
102-
10399
// Block until application is stable.
104100
awaitapplicationRef.whenStable();
105101

106102
// TODO(alanagius): Find a way to avoid rendering here especially for redirects as any output will be discarded.
103+
constenvInjector=applicationRef.injector;
107104
constrouterIsProvided=!!envInjector.get(ActivatedRoute,null);
105+
constrouter=envInjector.get(Router);
108106
constlastSuccessfulNavigation=router.lastSuccessfulNavigation();
109107

110108
if(!routerIsProvided){
111109
hasNavigationError=false;
112-
}elseif(lastSuccessfulNavigation?.finalUrl&&initialUrl!==null){
110+
}elseif(lastSuccessfulNavigation?.finalUrl){
113111
hasNavigationError=false;
114112

115-
const{ finalUrl}=lastSuccessfulNavigation;
116-
constfinalUrlStringified=finalUrl.toString();
117-
118-
if(initialUrl!==finalUrlStringified){
119-
constbaseHref=
120-
envInjector.get(APP_BASE_HREF,null,{optional:true})??
121-
envInjector.get(PlatformLocation).getBaseHrefFromDOM();
113+
consturlSerializer=envInjector.get(UrlSerializer);
114+
constlocationStrategy=envInjector.get(LocationStrategy);
115+
constfinalUrlSerialized=urlSerializer.serialize(lastSuccessfulNavigation.finalUrl);
116+
constfinalExternalUrl=joinUrlParts(locationStrategy.getBaseHref(),finalUrlSerialized);
122117

123-
redirectTo=joinUrlParts(baseHref,finalUrlStringified);
118+
if(urlToRender.href!==newURL(finalExternalUrl,urlToRender.origin).href){
119+
redirectTo=finalExternalUrl;
124120
}
125121
}
126122

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp