[Done] Allow setting metadata with anvil.server.FormResponse

Really great feature! anvil.server.FormResponse is a total game-changer! I can finally stop using my hacky workaround (which had downsides like slower responses and cookies not working).

However, just one limitation right now - I can’t change the metadata information like my title, description, or image when returning the form response. So for example, if I get a request to example.com/form1, I will want to return the specific title and description (or image if required) for form1.

5 Likes

This would be a wonderful feature to add.

2 Likes

I really appreciate the current development efforts in the area of creating greater visibility for the indexing of subpages, and offering a way to serve pages based on sub page specific URLs. But…I think we need to be able to add meta tags for such sub pages as well. I also think this is a more general request than adding metadata only to pages served with anvil.server.FormResponse.

This idea of adding the meta tags seems to be a common request across various platforms, e.g., in Wordpress applications. https://blog.hubspot.com/website/add-meta-tags-to-wordpress

For example, some wordpress plug-ins provide this functionality, such as Yoast SEO. https://www.bluehost.com/blog/yoast-seo-wordpress-seo-plugin/

In some low or no code environments like Drupal, there has been some effort to do so, but Drupal of course has the difficulty of rebuilding their code base frequently, so their most focused meta tag module seems to be poorly supported. https://www.drupal.org/project/metatag/issues/2665182

Similarly, in Django (once a fork from Drupal, I believe) there’s effort along the same lines. Wagtail SEO Guide | AccordBox

This not a criticism of anything now available in Anvil. I offer this only to share some alternative approaches that are out there.

By default, Anvil already allows you to set default metadata like Title, Description, Image, Favicon. You can always include other meta tags in your Native Library.

1 Like

Thanks, but isn’t that only going to address the entire app as determined by the start page? I’m not sure what you mean by Native Library and how that would solve the issue of meta tags specific to particular “pages” that and external indexer would see or try to route others to. I appreciate your earlier suggestion for how one could do a workaround, and that strikes me as a great start.

Yes, this is why this feature request exists.

The thing is, routes are a very new feature and Anvil have confirmed already that this is just the start and a lot of additional improvements are coming.

My reply was for this line only.

1 Like

Thank you. I have faith in Anvil and the team. I’m proceeding with my development all within Anvil.

2 Likes

If I’m understanding things correctly, I also would love to be able to set metadata with anvil.server.FormResponse.

My pages are loaded via anvil.server.FormResponse (see code below), and I would love to be able to set metadata so that links would show proper previews on social media. The links are to particular items on my site, so the metadata needs to be set differently depending on the hashroute provided.

Am I right in thinking I basically can’t do this at the moment?

My code, which is run whenever anyone comes to my app (this was my solution to harvesting query parameters so I could collect the click ID from adverts to my site):

@anvil.server.route("/")
def collectQueryParametersAndSendOnToHomePage(**p):
  return anvil.server.FormResponse('HomePage', queryParameters=p)

This post is outdated. Now, you can just use the Routing Dependency (Beta) which has logic for setting metadata

2 Likes

I can see various useful functions in

But I don’t see their use covered in the docs.

Importing an underscore module to use them feels a bit icky.

Is some guidance I’ve missed?

See the https://routing-docs.anvil.works/routes/#route-methods

You can override the meta method which should return a dictionary.

Looks like it’s quite well hidden.

class DashboardRoute(Route):
    …
    def meta(self, **loader_args):
        return {"title": "Dashboard", "description": "…"}
1 Like

and can that dictionary contain anything I like? the docs only mention the title tag.

I have other meta tags to create.

What if I want to add multiple meta tags?

I can see the function get_or_create_meta_tag (and I can see it does exactly what I need), but I don’t see it being used anywhere else in the codebase, so I’m not sure how I’m supposed to drive it other than just importing it myself.

1 Like

There’s a PR open that will support other meta tags

But it’s on hold until anvil can serve the underlying page and override the meta tags on the server

Which tags are you looking to fix?
Maybe we can merge that and it’ll be good enough

3 Likes

I need to add meta tags dynamically for my farcaster mini app stuff:

For each page I want to share, I need to add a meta tag:

<meta name="fc:frame" content="<stringified FrameEmbed JSON>" />

for routes that include params or query strings, I’ll need to generate that json dynamically.

ok so something like this


class SomeRoute(Route):
    def meta(self, **loader_args):
        query = loader_args["query"]
        some_var = query.get("some_var")
        return {"fc:frame": some_var, ...}


1 Like

Nice! Thank you. Off to play…

Can’t seem to get this working. I’ve added routing to my MWE and put a simple meta in place. When I run it, I can’t see any tag appearing in the page’s source:

It won’t work yet.

I was just scoping out the requirements.
Working on something now and I’ll update you when it’s live. Probably end of day.

Ahhh!!! Sorry, my misunderstanding.