PWA asset links API

Continuing the discussion from TWA application requires assetlinks.json:

What I’m trying to do:
I am trying to publish an app through Google Play Store and one of the things required to remove the URL bar is to serve an asset links JSON file.

The usual approach is to host this at https://example.com/.well-known/assetlinks.json and then the API take scare of the rest. The problem is that Anvil takes this as a server call and therefor never serves the data.

What I’ve tried and what’s not working:
Taking a very different approach I found that the Anvil logs we catching this error:

anvil.server.NoServerFunctionError: No server function matching "http-wellknown:/assetlinks.json" has been registered

So I figured maybe I could create a callable server function to return the data.
So when you navigate to example.com/.well-known/assetlinks.json it should show you the contents of that JSON file.
Which works without error but doesn’t display the data on the page so presumably cant be processed by the API.

Code Sample:

@anvil.server.callable("http-wellknown:/assetlinks.json")
def serve_well_known (*args, **kwargs):
  asset_links_json = [
    {
      "relation": ["delegate_permission/common.handle_all_urls"],
      "target": {
        "namespace": "android_app",
        "package_name": "com.example.twa",
        "sha256_cert_fingerprints":
          ["..."]
      }
    }
  ]
  
  url_media = anvil.URLMedia("https://example.com/_/theme/assetlinks.json")
  return url_media

The question is how do I go from a server return function into HTML or something that can be read by the request for example.com/.well-known/assetlinks.json

I have tried a number of return types including json.dumps() and creating a HttpResponse Object (whcih cannot be serialised in the return) but no joy.

Clone link:
share a copy of your app

1 Like

Hi @rickhurlbatt,

You have actually stumbled across an unreleased feature, which is fully implemented and supported, but not yet documented. Here is what you’re looking for:

@anvil.server.wellknown_endpoint("/assetlinks.json")
def assetlinks():
  return ["assetlinks", "json", "goes", "here"]

We’ll be releasing the autocompletion and documentation for this soon, but the summary is that this wellknown_endpoint decorator works exactly like the http_endpoint decorator, but the endpoints you configure land under /.well-known instead of under /_/api.

Right now this is only supported in Full Python 3 and Python 3.10 server runtimes.

I hope that helps!

10 Likes

This has made me smile for two reasons
Firstly, it is just amazing being supported by such a wonderful like Anvil. You guys make all the challenges just that little bit easier.

Secondly I feel like the Indiana Jones of Asset Links discovering previously unknown features. Maybe I need a hat and to learn to code with a whip on hand :thinking:#2023goals

6 Likes

10 Likes