Skip to content

Location does not support base href containing origin (only base hrefs with just the path are supported) #48175

Closed

Description

@constantant

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

https://konstantin.kharitonov.nl/cdn-i18n-routing-issue/en/

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

Activity

  1. atscott commented on Nov 22, 2022

    @atscott
    Contributor

    This is working as expected. The application is misconfiguring APP_BASE_HREF and the <base> element. Your APP_BASE_HREF results 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 setting APP_BASE_HREF to / and your base element 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.

  2. constantant commented on Nov 22, 2022

    @constantant
    ContributorAuthor

    @atscott

    1. The code is on https://d2nbgvwjuy9x2a.cloudfront.net/cdn-i18n-routing-issue/
    2. The app is on https://konstantin.kharitonov.nl/cdn-i18n-routing-issue/
    3. building with ng build --localize
    4. Each index.html has <base> as https://d2nbgvwjuy9x2a.cloudfront.net/cdn-i18n-routing-issue/{locale}/
    5. APP_BASE_HREF must be https://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

  3. atscott commented on Nov 22, 2022

    @atscott
    Contributor

    It looks like you want your APP_BASE_HREF to be /cdn-i18n-routing-issue/{locale}/. Does that resolve the issue?

  4. constantant commented on Nov 22, 2022

    @constantant
    ContributorAuthor

    @atscott
    No, it will cause a security issue with the same result.

    Unhandled Navigation Error: SecurityError: Failed to execute 'replaceState' on 'History': A history state object with URL 'https://d2nbgvwjuy9x2a.cloudfront.net/cdn-i18n-routing-issue/en/' cannot be created in a document with origin 'https://konstantin.kharitonov.nl' and URL 'https://konstantin.kharitonov.nl/cdn-i18n-routing-issue/en/'.
    
  5. changed the title [-]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)[/+] on Nov 22, 2022
  6. added
    area: common
    on Nov 22, 2022
  7. added this to the needsTriage milestone on Nov 22, 2022
  8. atscott commented on Nov 22, 2022

    @atscott
    Contributor

    @constantant indeed, this is an issue. I'm surprised this isn't encountered more frequently. The Location is totally incapable of correctly processing a base href with an origin because it attempts to strip the base href from the path:

    return this.normalize(this._locationStrategy.path(includeHash));

    return Location.stripTrailingSlash(_stripBaseHref(this._baseHref, _stripIndexHtml(url)));

    return baseHref && url.startsWith(baseHref) ? url.substring(baseHref.length) : url;

    The path won't ever start with a base href containing the origin. Additionally, when the document's base element contains href, it seems like you can't call replaceState without a full URL. That is, simply calling window.history.replaceState({}, '', '/') will fail with the security exception you mentioned.

  9. atscott commented on Nov 22, 2022

    @atscott
    Contributor

    Note that you can work around this by forking Location and updating _stripBaseHref to handle your baseHref correctly (and then adding your fork to your providers for Location)

  10. added
    P3
    on Nov 22, 2022
  11. modified the milestones: needsTriage, Backlog on Nov 22, 2022
  12. 11 remaining items

  13. added a commit that references this issue on Dec 7, 2022
    bf44dc2
  14. angular-automatic-lock-bot commented on Jan 7, 2023

    @angular-automatic-lock-bot

    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.

  15. locked and limited conversation to collaborators on Jan 7, 2023
  16. added a commit that references this issue on Feb 1, 2023
    b401d94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: commonworkaround2: non-obvious

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions