Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default route #388

Open
Addvilz opened this issue Aug 17, 2020 · 1 comment
Open

Default route #388

Addvilz opened this issue Aug 17, 2020 · 1 comment
Labels

Comments

@Addvilz
Copy link

Addvilz commented Aug 17, 2020

Is there an option to set up a "default" route?

Right now, a lot of frontend applications use HTML5 history mode for in-app navigation. This results in URL being replaced in browser history (ex. / => hello) without actually making a request to the new URL. This allows for frontend applications to replace their content dynamically, and use frontend only routing.

The issue is, if user reloads the page or just visits the URL by hand, there seems no way to handle that in Klein - at least I found nothing like that in source or docs - correct me if I am wrong here. As a result, user gets 404d.

If there is no such option, could we add one? If so, it should be possible to:

  • Have all post-routing 404's redirected to any one configurable path
  • Request with the new path should re-enter the router and match as usual

Kind of like

REQUEST /hello/world => 404? => rewrite path to / => REQUEST /

Less having hardcoded paths, not sure if there is a better way.

@Addvilz
Copy link
Author

Addvilz commented Aug 17, 2020

Something like this. Not sure yet how efficient this is.

class IndexFile(File):
    def __init__(self):
        super().__init__(os.path.join(static_files_at, 'index.html'))
        self.isLeaf = True

    def getChildWithDefault(self, path, request):
        return self

    def __repr__(self):
        return 'IndexFile()'


index_file = IndexFile()


class RewritingResource(File):
    def getChildWithDefault(self, path, request):
        child_resource = super(RewritingResource, self).getChildWithDefault(path, request)
        if isinstance(child_resource, NoResource):
            # Rewrite to /
            return index_file
        return child_resource

    def __repr__(self):
        return 'RewritingResource()'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants