My understanding according to this post is that the default base href
is '/'
. This scotch tutorial says to include this base href:
<!-- ./index.html -->
<base href="/">
For a basic app, is this strictly necessary, or will browsers assume that '/'
is the base href
for relative paths anyway?
No, they will not. The HTML spec specifies how relative URLs are resolved.
Definition and Usage The href attribute specifies the base URL for all relative URLs on a page.
The <base> tag must have either an href or a target attribute present, or both. There can only be one single <base> element in a document, and it must be inside the <head> element.
--base-href If you deploy your Angular app to a subfolder, the '--base-href' is important to generate the correct routes. This parameter will update the <base href> tag inside the index. html. For example, if the index.
will browsers assume that '/' is the base href for relative paths anyway?
No, they will not.
The HTML spec specifies how relative URLs are resolved. It's not too hard to work through the specification and definitions to find that when a relative URL is resolved against a document that does not have a base URL specified via a <base> element, the effective ("fallback") base URL is not guaranteed to be /
. There are a few alternatives, but in the usual case, the document's fallback base URL is taken as the document's own URL. Typically, that's not /
.
It's not so clear whether it's a good idea to specify /
as every document's base URL. It does mean that every relative URL will resolve the same way, regardless of the document in which it appears; that could be taken as a plus. On the other hand, it differs from the default behavior for resolving relative URLs, which could be a source of bugs. Certainly, if you choose to do it then you should do it consistently throughout the application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With