Skip to content

Commit 1784195

Browse files
atscottSplaktar
authored andcommitted
docs(router): clarify how base href is used to construct targets (angular#38123)
The documentation is not clear on how the base href and APP_BASE_HREF are used. This commit should help clarify more complicated use-cases beyond the most common one of just a '/' PR Close angular#38123
1 parent cfe5fad commit 1784195

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

aio/content/guide/router.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,15 @@ set the `href` value in `index.html` as shown here.
541541

542542
### HTML5 URLs and the `<base href>`
543543

544+
The guidelines that follow will refer to different parts of a URL. This diagram outlines what those parts refer to:
545+
546+
```
547+
foo://example.com:8042/over/there?name=ferret#nose
548+
\_/ \______________/\_________/ \_________/ \__/
549+
| | | | |
550+
scheme authority path query fragment
551+
```
552+
544553
While the router uses the <a href="https://developer.mozilla.org/en-US/docs/Web/API/History_API#Adding_and_modifying_history_entries" title="Browser history push-state">HTML5 pushState</a> style by default, you must configure that strategy with a `<base href>`.
545554

546555
The preferred way to configure the strategy is to add a <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base" title="base href">&lt;base href&gt; element</a> tag in the `<head>` of the `index.html`.
@@ -554,8 +563,16 @@ Some developers may not be able to add the `<base>` element, perhaps because the
554563

555564
Those developers may still use HTML5 URLs by taking the following two steps:
556565

557-
1. Provide the router with an appropriate [APP_BASE_HREF][] value.
558-
1. Use root URLs for all web resources: CSS, images, scripts, and template HTML files.
566+
1. Provide the router with an appropriate `APP_BASE_HREF` value.
567+
1. Use root URLs (URLs with an `authority`) for all web resources: CSS, images, scripts, and template HTML files.
568+
569+
* The `<base href>` `path` should end with a "/", as browsers ignore characters in the `path` that follow the right-most "/".
570+
* If the `<base href>` includes a `query` part, the `query` is only used if the `path` of a link in the page is empty and has no `query`.
571+
This means that a `query` in the `<base href>` is only included when using `HashLocationStrategy`.
572+
* If a link in the page is a root URL (has an `authority`), the `<base href>` is not used. In this way, an `APP_BASE_HREF` with an authority will cause all links created by Angular to ignore the `<base href>` value.
573+
* A fragment in the `<base href>` is _never_ persisted.
574+
575+
For more complete information on how `<base href>` is used to construct target URIs, see the [RFC](https://tools.ietf.org/html/rfc3986#section-5.2.2) section on transforming references.
559576

560577
{@a hashlocationstrategy}
561578

packages/common/src/location/location_strategy.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,21 @@ export const APP_BASE_HREF = new InjectionToken<string>('appBaseHref');
8181
* browser's URL.
8282
*
8383
* If you're using `PathLocationStrategy`, you must provide a {@link APP_BASE_HREF}
84-
* or add a base element to the document. This URL prefix that will be preserved
85-
* when generating and recognizing URLs.
84+
* or add a `<base href>` element to the document.
8685
*
87-
* For instance, if you provide an `APP_BASE_HREF` of `'/my/app'` and call
86+
* For instance, if you provide an `APP_BASE_HREF` of `'/my/app/'` and call
8887
* `location.go('/foo')`, the browser's URL will become
89-
* `example.com/my/app/foo`.
88+
* `example.com/my/app/foo`. To ensure all relative URIs resolve correctly,
89+
* the `<base href>` and/or `APP_BASE_HREF` should end with a `/`.
9090
*
91-
* Similarly, if you add `<base href='/my/app'/>` to the document and call
91+
* Similarly, if you add `<base href='/my/app/'/>` to the document and call
9292
* `location.go('/foo')`, the browser's URL will become
9393
* `example.com/my/app/foo`.
9494
*
95+
* Note that when using `PathLocationStrategy`, neither the query nor
96+
* the fragment in the `<base href>` will be preserved, as outlined
97+
* by the [RFC](https://tools.ietf.org/html/rfc3986#section-5.2.2).
98+
*
9599
* @usageNotes
96100
*
97101
* ### Example

0 commit comments

Comments
 (0)