Skip to content

relative url's can cause bad links #77

Closed

Description

@SvenDowideit

I am deploying to s3, and then have a hidden 404 error mkdocs page - when the users enters a long (incorrect url), then the error page is rendered using that url - thus all the menu links are broken.

Activity

  1. d0ugal commented on Dec 17, 2014

    @d0ugal
    Contributor

    FWIW, relative URLs have also caused a ton of issues with implementing #222.

    I'm not really a fan of relative URLs, it is only really useful for people that view the docs in static mode and I'm not sure how popular a use case that is.

    The only way I can think of solving this would be by having a config option - but I'm somewhat wary of this.

    @SvenDowideit I assume you already use this approach in your forked version?

  2. lovelydinosaur commented on Dec 17, 2014

    @lovelydinosaur
    Collaborator

    Is there an example for this?

    I use relative interlinking all the way through the REST framework docs. (Eg see links at the bottom of this document... https://raw.githubusercontent.com/tomchristie/django-rest-framework/master/docs/topics/rest-hypermedia-hateoas.md )

    What doesn't work?

  3. d0ugal commented on Dec 17, 2014

    @d0ugal
    Contributor

    Consider for example the search index, it is generated once for all plages. However, if you display the results in a modal the links need to be relative (or they wont work in static mode). To make this work in #222 I had to do this bit of gross magic, I'm trying to come up with a better solution.

    https://github.com/d0ugal/mkdocs/blob/search/mkdocs/themes/mkdocs/js/tipuesearch/tipuesearch_content.js
    https://github.com/d0ugal/mkdocs/blob/search/mkdocs/themes/mkdocs/base.html#L65

  4. lovelydinosaur commented on Dec 17, 2014

    @lovelydinosaur
    Collaborator

    it is only really useful for people that view the docs in static mode

    Maybe I'm just misunderstanding, but being able to click between docs in my editor is pretty neat.

    Consider for example a search index

    Are we just talking about relative links in the theme then? No problem from me with not supporting that.

  5. d0ugal commented on Dec 17, 2014

    @d0ugal
    Contributor

    Updated my comment as I rushed it on my phone. So yeah, I think for me the issue is supporting it at the theme level for me.

    I think with @SvenDowideit's example you have a similar issue when the 404 could be rendered at /invalid-path/ or /invalid/path/ but I may be missunderstanding that.

  6. d0ugal commented on Dec 17, 2014

    @d0ugal
    Contributor

    It is worth noting that we don't have this issue with the DRF theme, but I'm not sure if that is specific to that theme. The menu works fine: http://www.django-rest-framework.org/testing/testing/testing/

  7. lovelydinosaur commented on Dec 17, 2014

    @lovelydinosaur
    Collaborator

    I'm still surprised tho - I've never used anything except relative links with MkDocs,
    and put a stack of time into making them work initially.
    Be helpful to see a broken example case.

  8. lovelydinosaur commented on Dec 17, 2014

    @lovelydinosaur
    Collaborator

    I'm further confused because this report seems to be competely at odds with #192

    If you can't use relative or absolute URLs how are the rest of you folks getting by? :)

  9. d0ugal commented on Dec 17, 2014

    @d0ugal
    Contributor

    Hah, so I'd always use relative for document linking. I don't think absolute URLs work (although, with the strict mode disabled they may happen to work).

    I chimed in as I've had issues with #222 but that is a specific theme case so I may have just confused things. Maybe Sven can clarify the original issue.

  10. lovelydinosaur commented on Dec 18, 2014

    @lovelydinosaur
    Collaborator

    we don't have this issue with the DRF theme

    Yeah just noticed that the nav on the 404 page there is hardcoded in the theme :p

  11. SvenDowideit commented on Dec 19, 2014

    @SvenDowideit
    Author
  12. SvenDowideit commented on Dec 19, 2014

    @SvenDowideit
    Author

    mmm, iirc, its also very relevant if you use the same 'search' page for all 404's, which we do.

  13. ePirat commented on Jan 7, 2015

    @ePirat

    @d0ugal What exactly do you mean with

    I'm not really a fan of relative URLs, it is only really useful for people that view the docs in static mode and I'm not sure how popular a use case that is.

    Static mode?

  14. d0ugal commented on Jan 7, 2015

    @d0ugal
    Contributor

    @ePirat We possibly need a better name for this, basically when you browse the documentation in your browser on the local filesystem. So links like "/" don't work as that isn't the root of the documentation. So, for example mkdocs build && firefox site/index.html.

  15. 3 remaining items

  16. waylan commented on Aug 27, 2016

    @waylan
    Contributor

    Warning, the following is a complicated explanation of how the code works. Sorry, but explaining it is part of how I work out solutions, which you'll find at the end.

    I think the key to fixing this issue is to understand how the URLContext works. The 'short' explanation is that in mkdocs.commands.build.build_pages we have the loop: for page in site_navigation.walk_pages(): The key is in the walk_pages method. That method sets a page as "active" and yields it. build_pages then takes that page and builds it (renders Markdown and template, then writes to disc). The url property of each page is a method which makes a call to the URLContext using the "active" page to create URLs which are relative to that "active" page (the entire loop through the nav in the template happens with this one page set as "active"). After the page is built, the loop (in build_pages) continues, asking walk_pages for the next page. walk_pages then deactivates the current page and activates the next page before yielding it. The entire thing is dependent on the global "active" page and URLContext and the fact that we are in a synchronous system (it would never work in an asynchronous environment). That is how each page is generated with a complete nav where all the URLs are relative to that page with absolutely no consideration for the location of the current page in the template. Its a clever piece of code which removes the need for the template to care about relative URLs.

    However, there are pages which do not get this treatment. Specifically, any page which is not listed in the pages config setting. As it happens, all those pages are template based pages, such as error pages, sitemap.xml, search.html and anything in the extra_templates config setting. The key difference is that for these pages there is no "active" page, in which case the URLContext defaults to assuming an "active" URL of /. Of course, search.html is at the site root (as it sitemap.xml) so the relative paths in the nav are correct. However, extra_templates could be nested at any location (I assume this is an unreported bug as its a little used feature). Presumably, setting the "active" page in the URLContext to the path of the current template would build a proper nav for each page in extra_templates (untested, but I think it should work).

    But for error pages it's a little more tricky. As discussed previously, error pages could be served at any URL nested at any level by the server. There it no correct "active" page to make nav items relative to. Therefore, I propose that if there is no "active" page, rather than the default being to generate a URL for a page relative to /, instead generate an absolute URL with the base extrapolated from site_url. This absolute URL should then be returned by the mkdocs.nav.Page.url method when the error template builds it's nav.

  17. added 6 commits that reference this issue on Aug 28, 2016
    c24fb95
    2404726
    1bd942f
    51fe79c
    4ca2fef
    f5b5446
  18. waylan commented on Aug 29, 2016

    @waylan
    Contributor

    I have a working solution in #1039. It took a few iterations, but I have the least invasive patch possible (added 9 lines of code, not counting tests and comments). Only the error page is affected, everything else continues to behave as before.

  19. modified the milestones: 0.16, 1.0.0 on Aug 29, 2016
  20. csandanov commented on Sep 14, 2018

    @csandanov

    @waylan there's so much information and changes regarding the URLs I'm confused whether there is a solution to my problem described in #192 or not. Is it possible to make mkdocs generate absolute URLs (to avoid 404 by search crawlers) or not? I realize there are variables I can use in theme templates to get absolute URLs in navigation but what about markdown links in .md files? Relative URLs ../ generate tons of 404 errors by search crawler.

  21. waylan commented on Sep 14, 2018

    @waylan
    Contributor

    @csandanov please open a new issue and provide examples describing your problem.

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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions