Repository navigation
Closed
Labels
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: commonIssues related to APIs in the @angular/common packageworkaround2: non-obvious
Milestone
Description
Which @angular/* package(s) are the source of the bug?
common, localize, router
Is this a regression?
No
Description
Routing gets broken if APP_BASE_HREF changed manually.
Regarding the docs, while deploying the bundles to CDN it needs to change the APP_BASE_HREF token to a URL where an app gonna be launched.
provide: APP_BASE_HREF,
useFactory: () => {
const [, root, locale] = location.pathname.split('/');
return `//${ location.host }/${ root }/${ locale }/`
}
There is a simple implementation of it: https://github.com/constantant/cdn-i18n-routing-issue
Please provide a link to a minimal reproduction of the bug
Please provide the exception or error you saw
core.mjs:9235 ERROR Error: Uncaught (in promise): Error: NG04002
Error: NG04002
at j.noMatchError (router.mjs:3663:16)
at router.mjs:3645:28
at catchError.js:10:39
at ne._error (OperatorSubscriber.js:23:21)
at ne.error (Subscriber.js:40:18)
at ne._error (Subscriber.js:64:30)
at ne.error (Subscriber.js:40:18)
at ne._error (Subscriber.js:64:30)
at ne.error (Subscriber.js:40:18)
at ne._error (Subscriber.js:64:30)
at q (zone.js:1211:31)
at q (zone.js:1165:17)
at zone.js:1278:17
at v.invokeTask (zone.js:406:31)
at Object.onInvokeTask (core.mjs:26589:33)
at v.invokeTask (zone.js:405:60)
at M.runTask (zone.js:178:47)
at _ (zone.js:585:35)
handleError @ core.mjs:9235
next @ core.mjs:27286
next @ Subscriber.js:91
_next @ Subscriber.js:60
next @ Subscriber.js:31
(anonymous) @ Subject.js:34
ne @ errorContext.js:19
next @ Subject.js:27
emit @ core.mjs:23077
(anonymous) @ core.mjs:26628
invoke @ zone.js:372
run @ zone.js:134
runOutsideAngular @ core.mjs:26501
onHandleError @ core.mjs:26628
handleError @ zone.js:376
runGuarded @ zone.js:147
i.microtaskDrainDone @ zone.js:1072
_ @ zone.js:592
Promise.then (async)
z @ zone.js:561
R @ zone.js:572
scheduleTask @ zone.js:396
onScheduleTask @ zone.js:283
scheduleTask @ zone.js:386
scheduleTask @ zone.js:221
scheduleMicroTask @ zone.js:241
ee @ zone.js:1265
q @ zone.js:1202
(anonymous) @ zone.js:1118
(anonymous) @ zone.js:1134
n @ jsonp chunk loading:77
(anonymous) @ 338.ac20b7bb511b4118.js:1
Show 18 more frames
Please provide the environment you discovered this bug in (run ng version)
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 15.0.0
Node: 16.13.1
Package Manager: npm 8.14.0
OS: win32 x64
Angular: 15.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1500.0
@angular-devkit/build-angular 15.0.0
@angular-devkit/core 15.0.0
@angular-devkit/schematics 15.0.0
@schematics/angular 15.0.0
rxjs 7.5.7
typescript 4.8.4
Anything else?
Notice that the minimal reproduction is building as a multilingual app with ng build --localize
The whole pipeline is available here: https://github.com/constantant/cdn-i18n-routing-issue/blob/master/.github/workflows/main.yml
Reacted by mmza94, Konstantin and Yuri Beliakov
Metadata
Metadata
Assignees
Labels
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: commonIssues related to APIs in the @angular/common packageworkaround2: non-obvious
Activity
atscott commentedon Nov 22, 2022
This is working as expected. The application is misconfiguring
APP_BASE_HREFand the<base>element. YourAPP_BASE_HREFresults in the router prepending//${ location.host }/${ root }/${ locale }/to every route but it doesn't look like that's what you're intending. I think instead, you want to be settingAPP_BASE_HREFto/and yourbaseelement would be configured to serve different assets from CDN.This issue doesn't fall into the bug report or feature request category. This issue tracker is not suitable for support requests, please repost your issue on StackOverflow using tag
angular.If you are wondering why we don't resolve support issues via the issue tracker, please check out this explanation.
constantant commentedon Nov 22, 2022
@atscott
ng build --localizeindex.htmlhas<base>ashttps://d2nbgvwjuy9x2a.cloudfront.net/cdn-i18n-routing-issue/{locale}/APP_BASE_HREFmust behttps://konstantin.kharitonov.nl/cdn-i18n-routing-issue/{locale}/Which point is wrong here?
If all good it means that there is an issue, I guess
atscott commentedon Nov 22, 2022
It looks like you want your
APP_BASE_HREFto be/cdn-i18n-routing-issue/{locale}/. Does that resolve the issue?constantant commentedon Nov 22, 2022
@atscott
No, it will cause a security issue with the same result.
[-]NG04002 error on APP_BASE_HREF manipulating while deployment to CDN[/-][+]`Location` does not support base href containing `origin` (only base hrefs with just the path are supported)[/+]atscott commentedon Nov 22, 2022
@constantant indeed, this is an issue. I'm surprised this isn't encountered more frequently. The
Locationis totally incapable of correctly processing abasehref with an origin because it attempts to strip the base href from the path:angular/packages/common/src/location/location.ts
Line 98 in fd2eea5
angular/packages/common/src/location/location.ts
Line 130 in fd2eea5
angular/packages/common/src/location/location.ts
Line 298 in fd2eea5
The
pathwon't ever start with a base href containing the origin. Additionally, when the document'sbaseelement containshref, it seems like you can't callreplaceStatewithout a full URL. That is, simply callingwindow.history.replaceState({}, '', '/')will fail with the security exception you mentioned.atscott commentedon Nov 22, 2022
Note that you can work around this by forking
Locationand updating_stripBaseHrefto handle yourbaseHrefcorrectly (and then adding your fork to yourprovidersforLocation)11 remaining items
fix(common): Update `Location` to support base href containing `origi…
docs(common): Add note to location about investigating base href hand…
docs(common): Add note to location about investigating base href hand…
docs(common): Add note to location about investigating base href hand…
angular-automatic-lock-bot commentedon Jan 7, 2023
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.
fix(common): Update `Location` to support base href containing `origi…