GAE: file_get_contents() and static files

480 views Asked by At

Instead of updating the same variable in various locations I parse files and extract the bits I need. One bit I am looking for is the cache name in my service-worker.js file. When I'm developing locally, I can easily retrieve the js file in the directory structure.

When I deploy my app to GAE, the service worker JS file needs to be declared static and so GAE stashes it away somewhere.

Is there a way to get to this file without making a URL call?

This is what I currently use and it's all working fine in DEV:

<?php
function getCacheId() {
    $cache_id = "";
    $c = file_get_contents ( dirname ( __FILE__ ) . "/service-worker.js" );
    if (preg_match ( "/CACHE_NAME = '(.*)'/", $c, $matches ) !== false) {
        if (count ( $matches )) {
            $cache_id = trim ( $matches[1] );
        }
    }
    return $cache_id;
}
?>
<script>
    const CACHEID = "<?php echo getCacheId() ?>";
</script>
1

There are 1 answers

0
Dan Cornilescu On BEST ANSWER

You need to also configure application_readable: true in addition to the static file declaration in your app.yaml file.

From Handlers element:

application_readable

Optional. Boolean. By default, files declared in static file handlers are uploaded as static data and are only served to end users. They cannot be read by an application. If this field is set to true, the files are also uploaded as code data so your application can read them. Both uploads are charged against your code and static data storage resource quotas.