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

Rewriting #901

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Rewriting #901

wants to merge 8 commits into from

Conversation

ematipico
Copy link
Member

@ematipico ematipico commented Apr 18, 2024

@ematipico ematipico marked this pull request as ready for review April 18, 2024 15:54
Internally, the `reroute` **must** create a new `Request` when attempting to render the rerouted route. The creation of this new `Request` will vary based on the signature used.

- Accepting a `string` allows to quickly reroute to a URL without too much hassle. When using a `string`, Astro will create a new `Request` with the new URL, and it will inherit all the data from the previous request.
> Astro won't do any particular check on the string. For example, it won't check for trailing slashes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't it call matchRoute internally? I'm pretty sure that code does check trailing slash rules.

Copy link
Member Author

Choose a reason for hiding this comment

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

Internally, we use route.pattern.test. pattern is the path-to-regex. Does this regex check for trailing slashes? If so I can omit the paragraph

@matthewp
Copy link
Contributor

We should figure out what happens if there are cycles; for example if /foo reroutes to /bar which reroutes back to /foo on an endless loop. Should we do anything to try and prevent that?

@ematipico
Copy link
Member Author

We should figure out what happens if there are cycles; for example if /foo reroutes to /bar which reroutes back to /foo on an endless loop. Should we do anything to try and prevent that?

In my early prototype, I was able to detect loops. I added a paragraph for that here c8f69ac (#901)

@matthewp
Copy link
Contributor

Let's say you had a middleware stack like this:

i18n -> custom -> auth -> page

Given that reroute() returns a response, I think this means that if you call reroute in the custom middleware function, then auth will not be called. Is that right? Or it the function aware of where the current point is in the middleware chain and continues to call the proceeding middleware functions?

@ematipico
Copy link
Member Author

Let's say you had a middleware stack like this:

i18n -> custom -> auth -> page

Given that reroute() returns a response, I think this means that if you call reroute in the custom middleware function, then auth will not be called. Is that right? Or it the function aware of where the current point is in the middleware chain and continues to call the proceeding middleware functions?

That's right, I updated the proposal to better explain the API and how next("/new") vs ctx.reroute("/new") differ

@ematipico
Copy link
Member Author

The commit f9ec599 (#901) changes "reroute" to "rewrite". We assessed that the majority of frameworks and services out there use the term "rewrite".

@florian-lefebvre florian-lefebvre changed the title Rerouting Rewriting May 24, 2024
@florian-lefebvre florian-lefebvre mentioned this pull request May 24, 2024
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.

None yet

2 participants