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

[REF-2643]Throw Errors for duplicate Routes #3155

Merged
merged 4 commits into from
May 3, 2024
Merged

Conversation

ElijahAhianyo
Copy link
Collaborator

@ElijahAhianyo ElijahAhianyo commented Apr 24, 2024

With this PR an error will be thrown when two pages have the same route.
Eg.

def index(): 
   return rx.fragment()

def page2():
   return rx.fragment()

app = App()
app.add_page(index)
app.add_page(page2, route="/") # will not work
def index(): 
   return rx.fragment()

@rx.page(route="/") # will not work
def page2():
   return rx.fragment()

app = App()
app.add_page(index)
@rx.page(route="posts")
def page2(): 
   return rx.fragment()

@rx.page(route="posts") # will not work
def page2():
   return rx.fragment()

app = App()
@rx.page(route="posts") # will not work
def page2(): 
   return rx.fragment()

@rx.page(route="posts") # will not work
def page2():
   return rx.fragment()

def page3():
    return rx.fragment()

app = App()
app.add_page(page3, route="posts") # this will work.

Note that add_page takes precedence over @rx.page

NB: This PR does not handle duplicates for dynamic routes as that is a much more complex use case. Will make a separate PR as a follow-up to handle that case.

Copy link

linear bot commented Apr 24, 2024

@ElijahAhianyo ElijahAhianyo changed the title [REF-2643][WIP]Throw Errors for duplicate Routes [WIP][REF-2643]Throw Errors for duplicate Routes Apr 24, 2024
@ElijahAhianyo ElijahAhianyo changed the title [WIP][REF-2643]Throw Errors for duplicate Routes [REF-2643]Throw Errors for duplicate Routes Apr 24, 2024
@ElijahAhianyo
Copy link
Collaborator Author

Needs reflex-dev/reflex-web#659 to pass CI

@ElijahAhianyo ElijahAhianyo marked this pull request as ready for review April 28, 2024 11:17
reflex/app.py Outdated
raise ValueError(
f"You cannot define a route with the same specificity as a optional catch-all route ('{route}' and '{new_route}')"
)
def replace_brackets_with_keywords(input_string):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move this outside of this function, so it won't get redefined every time

reflex/app.py Outdated
# / posts/[[...slug2]]-> /posts/__DOUBLE_CATCHALL_SEGMENT__
# /posts/[...slug3]-> /posts/__SINGLE_CATCHALL_SEGMENT__

# Replace [[...<slug>]] with __DOUBLE_CATCHALL_SEGMENT__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if as a short-circuit we can first check if "[" in input_string and only do the regexes if necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i actually had that logic right below this function which checks for the presence of a replaced keyword, but I can move the logic up so the replace_brackets_with_keyword function is not called for non dynamic routes

constants.RouteRegex.SINGLE_CATCHALL_SEGMENT,
constants.RouteRegex.DOUBLE_CATCHALL_SEGMENT,
)
for route in self.pages:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will end up doing n^2 comparisons - probably alright, but maybe we should save the hash value along with it when we add page?

@picklelo picklelo merged commit 9c7dbdb into main May 3, 2024
46 checks passed
@picklelo picklelo deleted the elijah/duplicate-routes branch August 21, 2024 20:43
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