Repository: mkdocs/mkdocs

Issue:
Template: nav.homepage.url on 404.html template not set
I have a [404.html](https://github.com/squidfunk/mkdocs-material/blob/master/src/404.html) defined in [Material for MkDocs](https://github.com/squidfunk/mkdocs-material) which does not include the `nav.homepage.url` in Jinja - it's just empty. On all other pages it correctly points to the home page. Is this correct? In my view this value should also be accessible from non-base templates.

Also reported in https://github.com/squidfunk/mkdocs-material/issues/127


Discussion:
I'm guessing this is related to the issues discussed in #77 (my comments on that issue explain how the system works and why it needs to work differently for error pages). The same relative/absolute problems would exist and pointing to the "homepage" in an error page template would be slightly different than with any other page. It may be that we don't have a reference to the "homepage" because it would be useless anyway. But I'll need to take a closer look to be sure.
Thanks for your first estimation. Maybe you can post a workaround, because it's really annoying that the 404 page doesn't really return to the homepage. Please keep in mind that it needs to be relative to the root, as some documentation is hosted in subdirectories (e.g. GitHub pages).
I'm almost certain this is related to the fix for #77. I haven't tested, but if the homepage is at the server root (`/`), then we may have a bug which returns an empty string instead (see [nav.py L100][1]:  `abs_url.rstrip('/')` would return an empty string if `abs_url == '/'`). Presumably when I added that I assumed `abs_url` would never equal `'/'`, so we just need to test and confirm whether that is possible.

[1]: https://github.com/mkdocs/mkdocs/blob/aebfcdc39c8c0c3b15dc6b25df0f0c173e813a1e/mkdocs/nav.py#L100

Resolving patch:
diff --git a/mkdocs/commands/build.py b/mkdocs/commands/build.py
--- a/mkdocs/commands/build.py
+++ b/mkdocs/commands/build.py
@@ -76,7 +76,8 @@ def get_global_context(nav, config):
 
     return {
         'nav': nav,
-        'base_url': nav.url_context.make_relative('/'),
+        # base_url should never end with a slash.
+        'base_url': nav.url_context.make_relative('/').rstrip('/'),
 
         'extra_css': extra_css,
         'extra_javascript': extra_javascript,
diff --git a/mkdocs/nav.py b/mkdocs/nav.py
--- a/mkdocs/nav.py
+++ b/mkdocs/nav.py
@@ -97,7 +97,7 @@ def make_relative(self, url):
         """
         if self.force_abs_urls:
             abs_url = '%s/%s' % (self.base_path.rstrip('/'), utils.path_to_url(url.lstrip('/')))
-            return abs_url.rstrip('/')
+            return abs_url
 
         suffix = '/' if (url.endswith('/') and len(url) > 1) else ''
         # Workaround for bug on `os.path.relpath()` in Python 2.6
diff --git a/mkdocs/utils/__init__.py b/mkdocs/utils/__init__.py
--- a/mkdocs/utils/__init__.py
+++ b/mkdocs/utils/__init__.py
@@ -286,7 +286,7 @@ def create_media_urls(nav, path_list):
             continue
         # We must be looking at a local path.
         url = path_to_url(path)
-        relative_url = '%s/%s' % (nav.url_context.make_relative('/'), url)
+        relative_url = '%s/%s' % (nav.url_context.make_relative('/').rstrip('/'), url)
         final_urls.append(relative_url)
 
     return final_urls


Test patch:
diff --git a/mkdocs/tests/nav_tests.py b/mkdocs/tests/nav_tests.py
--- a/mkdocs/tests/nav_tests.py
+++ b/mkdocs/tests/nav_tests.py
@@ -325,10 +325,10 @@ def test_force_abs_urls(self):
         self.assertEqual([n.title for n in nav_items],
                          ['Home', 'Running', 'Notes', 'License'])
         self.assertEqual([n.url for n in nav_items], [
-            '',
-            '/api-guide/running',
-            '/about/notes',
-            '/about/sub/license'
+            '/',
+            '/api-guide/running/',
+            '/about/notes/',
+            '/about/sub/license/'
         ])
 
     def test_force_abs_urls_with_base(self):
@@ -351,10 +351,10 @@ def test_force_abs_urls_with_base(self):
         self.assertEqual([n.title for n in nav_items],
                          ['Home', 'Running', 'Notes', 'License'])
         self.assertEqual([n.url for n in nav_items], [
-            '/foo',
-            '/foo/api-guide/running',
-            '/foo/about/notes',
-            '/foo/about/sub/license'
+            '/foo/',
+            '/foo/api-guide/running/',
+            '/foo/about/notes/',
+            '/foo/about/sub/license/'
         ])
 
     def test_invalid_pages_config(self):