Created
January 25, 2013 20:58
-
-
Save barrypitman/4637786 to your computer and use it in GitHub Desktop.
Allow Turbolinks to update the browser address bar correctly after an ajax request is redirected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Provide the correct response URL to turbolinks in the 'X-XHR-Current-Location' header. Used to update the browser | |
* history after an ajax request is redirected. | |
* | |
* @author barry | |
* @since 2013/01/24 11:46 AM | |
*/ | |
public class TurboLinksUrlFilter implements Filter { | |
/** | |
* turbolinks checks for this header by default | |
*/ | |
private static final String RESPONSE_HEADER = "X-XHR-Current-Location"; | |
private static final String REQUEST_HEADER = "X-XHR-Referer"; | |
public void init(FilterConfig config) throws ServletException { | |
} | |
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { | |
HttpServletResponse response = (HttpServletResponse) resp; | |
HttpServletRequest request = (HttpServletRequest) req; | |
if (!response.isCommitted() && request.getHeader(REQUEST_HEADER) != null) { | |
response.setHeader(RESPONSE_HEADER, getFullURL(request)); | |
} | |
chain.doFilter(req, resp); | |
} | |
public void destroy() { | |
} | |
private String getFullURL(HttpServletRequest request) { | |
StringBuffer url = request.getRequestURL(); | |
if (request.getQueryString() != null) { | |
url.append('?'); | |
url.append(request.getQueryString()); | |
} | |
return url.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment