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

Homepage partials example gets weird with the refresh button. #2559

Open
mmcc opened this issue Jun 28, 2024 · 5 comments
Open

Homepage partials example gets weird with the refresh button. #2559

mmcc opened this issue Jun 28, 2024 · 5 comments
Labels
bug Something isn't working. docs

Comments

@mmcc
Copy link

mmcc commented Jun 28, 2024

This is probably low priority, but figured I would call it out. I was playing with the partials examples while reading the docs, then wanted to reset the example so I refreshed. If you refresh you're now just sitting in the raw HTML of the example, and the back button will now cycle through the other ones you clicked.

Here's a stream.new recording of what I'm seeing

Or, as a gif:

screen recording

@marvinhagemeister
Copy link
Collaborator

Good catch, we should fix that

@marvinhagemeister marvinhagemeister added bug Something isn't working. docs labels Jun 28, 2024
@Sleepful
Copy link

It also breaks the URL history. Hitting the backwards button on the browser after clicking through some partials will yield the same result as mentioned above.

Is there a way to use partials without the redirect? Most of the time I just want to get some feedback from the server in HTML form, without any kind of redirect. If I want a redirect then I can set the headers myself on the server.

@Sleepful
Copy link

Sleepful commented Jun 29, 2024

Here is a quick and dirty way around it, if you are using a <form> that creates a POST request and potentially retrieves a Partial, redirecting GET requests to a standard location makes it less jarring when using the refresh/backwards button on the browser.

export const config: RouteConfig = {
  skipAppWrapper: true, // Skip the app wrapper during rendering
};

export const handler: Handlers<unknown, State> = {
  GET(_req, _ctx) {
    // Currently it can't be avoided that Partials create a new URL in browser history, which is strange
    // when using the backwards and/or refresh button in the browser. To compensate, manually redirect GET requests.
    return new Response("", {
      status: 307,
      headers: { Location: "/" },
    });
  },

  async POST(req, ctx) {
	ctx.render() // renders the partial
  }

Caveat: anchor tags still want to use a GET request so that's more difficult to redirect server-side.

I believe the ideal would be to be able to avoid any kind of client-side URL history with an attribute or similar. For example:

<form f-partial="/some/partial" redirect-client="false">

If you like this idea, I would be happy to implement it and submit a PR, just LMK. 😄

@marvinhagemeister
Copy link
Collaborator

Fresh will always redirect when using links. It doesn't redirect when using plain buttons without a form when the button has the f-partial attribute.

@Sleepful
Copy link

Sleepful commented Jun 30, 2024

True, it makes sense that links redirect.

For <form> submissions the URL in the action attribute should take precedence over f-partial, right? In which case, if I don't want the submission to change the URL then I just give it the current URL on the action attribute.

I found this #2409 but I suppose I don't have it in my Fresh release right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. docs
Projects
None yet
Development

No branches or pull requests

3 participants