5
votes

Summary

I have been using ScriptApp.getService().getUrl() to get the current instance (dev|prod) in my Google Apps Script project successfully for some time now. As of yesterday, the returned value seems to depend completely on whether the URL ends with /dev or not. Has anyone else seen this issue or have a workaround?

Previous Behavior

getUrl would always return https://script.google.com/a/dustinluck.com/macros/s/{deployment-id}/exec. The deployment id would be exactly what I'd see in the browser's URL bar.

Current Behavior

getUrl always returns the dev URL if the address in the browser's URL bar ends with /dev or the prod URL if it doesn't. This is not a problem when first displaying the web form since the URL correctly ends with /dev, however, it seems that when the form is submitted and a call is made to Google Apps Script code using google.script.run, the URL contains /callback and therefore getEnv evaluates to prod.

Sample Code to Reproduce

The app is published to execute as me and allows anonymous access, however, I see the same behavior when published to execute as the user accessing the web app and/or limiting access to the web app.

code.gs

function doGet(e) {
  var pageHtmlTemplate = HtmlService.createTemplateFromFile("default");
  pageHtmlTemplate.env = getEnv();
  return pageHtmlTemplate.evaluate();
}

function getEnv() {
  var devId = 'AKfycbwou0odFWX6II6YaeSaSOaXF4faYrzJ5XygssntdnI';
  var prodId = 'AKfycbxO20AYwEDPvdRsXu_K9pOb-E_iWRV12Wuv3TCApF53vDeuQpU';
  var currUrl = ScriptApp.getService().getUrl();
  var env = "unknown";
  if (currUrl.indexOf(devId) != -1) {
    env = "dev";
  } else if (currUrl.indexOf(prodId) != -1) {
    env = "prod";
  }
  
  return env + " (" + currUrl + ")";
}

default.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  </head>
  <body>
    <h1>Env on load</h1>
    <?= env ?>
    
    <h1>Env on callback</h1>
    <div id="test">
    </div>
  </body>
  <script>
      $(document).ready(function() {
          google.script.run.withSuccessHandler(success).getEnv();
      });

      function success(env) {
          $("#test").html(env);
      }
  </script>
</html>

Test URLs

Both of these have the prod script id, but depending on whether it ends with /dev or /exec, the results of getUrl change.

2
it seems that when the form is submitted and a call is made to Google Apps Script code using google.script.run, the URL contains /callback and therefore getEnv evaluates to prod. What do you mean? Could you focus on the actual problem instead of the changed behavior?TheMaster
The problem is the changed behavior. When ScriptApp.getService().getUrl() is called and the URL does not contain \dev, the production URL is returned. So when the callback happens using the dev URL, the returned environment is not correct.Dustin Luck
I have added code to demonstrate the behavior of getUrl when doing a callback via google.script.runDustin Luck
you could post the update as answer, since it resolved your issue.TheMaster
@TheMaster "Update" moved to a community wiki answer.Rubén

2 Answers

1
votes

Posted by the OP as an "update" to the question

Update

(Apr 17, 2020) After republishing this script, the previous (correct) behavior returned. I can only guess that some recent updates from Google fixed this issue.

-1
votes

I've seen the same problem, try to disable Chrome V8 apps script via menu Execute -> "Disable ...Chrome V8".