Redirecting Without Runtime Support

The redirect action assumes that the redirection will occur in a Web page that has the KRL JavaScript runtime loaded. Sometimes this is not the case. For example, when returning from an OAuth Callback, the redirection occurs in the browser (e.g. session data is loaded) but not in the context of a page. 

The following pattern can be used to perform a raw redirect:

  rule perform_redirect {
    select when oauth return label "access_token"
    pre {
      url = "<your URL here";
      js = <<
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title></title>
  <META HTTP-EQUIV="Refresh" CONTENT="0;#{url}">
  <meta name="robots" content="noindex"/>
  <link rel="canonical" href="#{url}"/>
</head>
<body>
<p>
You are being redirected to <a href="#{url}">#{url}</a>
</p>
<script type="text/javascript">
window.location = #{url};
</script>
</body>
</html>
>>;
    }
    send_raw("text/html")
        with content= js
  }

This tries to perform the redirection with both a <script/> tag and with meta data in the HTML header. Overkill in some cases, but it does the job. 

 

 

Copyright Picolabs | Licensed under Creative Commons.