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

When resolving a non-get request that results in a 200, recommend the user to use content-location #65

Closed
jho406 opened this issue Jun 7, 2024 · 1 comment

Comments

@jho406
Copy link
Collaborator

jho406 commented Jun 7, 2024

Context

When posting on a form enabled with data-sg-visit, the url changes the the received response's URL. For example, if you're on posts/new and the form does a POST to /posts to create a new post, its possible for the response to respond with a 200 because we may render errors. For example, in the controller:

def create
  @post = Post.new
  if @post.save
    redirect_to post_path(@post)
  else
   # render errors
    render :new
end

In this case the URL will change to /post, but will show the render of /posts/new which is incorrect. This happens with normal rails html too.

Superglue has a solution for this. If you add content-location to the headers, superglue will use that as the actual url. For example:

def create
  @post = Post.new
  if @post.save
    redirect_to post_path(@post)
  else
+  response.set_header("content-location", new_post_path)
    render :new
end

this will tell Superglue.js to set the url to posts/new correctly when it receives the response from Rails.

issue

I'd like to be able to give the developer some console.info that they should use the above methodology when using data-sg-visit or visit.

Maybe here:

const { superglue, pages = {} } = getState()

We can detect the request method (non-Get), the response code, if it was a 200 (not redirected), check the headers for content-location, if its not there we console.info the following copy:

We recommend setting a content-location when using data-sg-visit or visit with a POST, PUT, or PATCH
@jho406
Copy link
Collaborator Author

jho406 commented Jun 22, 2024

Closing this as its no longer needed with this PR #73

@jho406 jho406 closed this as completed Jun 22, 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

No branches or pull requests

1 participant