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

Mounted router fall back to parent router when route is not found #883

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

schloerke
Copy link
Collaborator

@schloerke schloerke commented Sep 29, 2022

A different solution to #881

Situation:

  • Static handler is mounted at /;
  • Route /b/z is mounted at /a
  • Route /c is mounted at /a/b

Goal: Request /a/b/c

Current behavior:

  • The route of /a/b/c matches against mount location of /.
  • The static file handler attempts to handle /a/b/c.
    • No file is found, a 404 is returned.

New behavior:

  • The route of /a/b/c matches against mount location of /.
  • The static file handler attempts to handle /a/b/c
    • No file is found, route is forwarded
    • Mount's $route() is marked as routeNotFound()
  • The route of /a/b/c matches against mount location /a
    • The route is trimmed to /b/c
    • The route /b/c does not match any routes
    • Mount's $route() is marked as routeNotFound()
  • The route of /a/b/c matches against mount location /a/b
    • The route is trimmed to /c
    • The route /c matches route location /c
    • Result of /c is returned

This would allow for #882 to not be merged, keep the docs as the last mount location. This benefits the user in that their content will always have preference over anything mounted in /__docs__/

PR task list:

  • Should this behavior be opt in? Opt out?
  • How should the user opt in / out?
    • $set_fallback(fallback = TRUE)?
    • $mount(path, router, fallback = TRUE)?
  • Append mounted router, do not overwrite existing router
  • Update NEWS
  • Add tests
  • Update documentation with devtools::document()

@jcheng5
Copy link
Member

jcheng5 commented Sep 29, 2022

I suggest removing the IS_MOUNT tracking variable (tricky to make this work with async responses anyway) and instead have the route() method (which is recursive) never do 404 handling; instead, leave that to serve() (which is not recursive).

R/plumber.R Outdated Show resolved Hide resolved
@jcheng5 jcheng5 mentioned this pull request Oct 5, 2022
3 tasks
@jcheng5
Copy link
Member

jcheng5 commented Oct 6, 2022

As written, you can choose to either have 1) 405 and missing-trailing-slash automatically handled for you, or 2) a custom top-level 404 handler, but you cannot have both.

…ound.

In `$serve()`, move `routeNotFoundStep` before `postRouteStep`
In `$route()`, if `404` handler has been set, call `lastChanceRouteNotFound()`
@schloerke
Copy link
Collaborator Author

Going to wait on this PR for a bit as the changes are fairly drastic compared to current functionality

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

Successfully merging this pull request may close these issues.

2 participants