Add link previews for social media

You can configure Appfarm to return Open Graph meta tags when a specific URL is accessed. The metadata in these tags are used by social media platforms and other services when a link is shared to generate rich previews with a title, description, and image.

The metadata can be populated in one of two ways:

  • Static data that applies to your entire app. The same metadata will be returned for any URL under your app's root URL.

  • Dynamic data from the database. The metadata is selected based on a unique identifier specified in the URL. For example, if you have published a number of articles in your app you might wish to populate custom metadata for each article.

Note

When an external service (like Facebook), checks a given app URL for Open Graph meta tags, the app is not loaded. Instead, the Appfarm Platform performs a look up and returns the data directly from the database.

This means that the data you wish to use must already exist in the database.

For best practices and more general information about metadata for social sharing, refer to this Ahrefs blog post about Open Graph meta tags.

Static data

To use the same metadata across your entire app you can set static content. Regardless of what URL for your app an external service accesses, the same content will be returned.

Specify the content in App Settings under Shareable Link Info.

The meta tag for the Open Graph property og:url is also generated and populated with your app's root URL.

Dynamic data

To return metadata based on objects in your database you can configure dynamic data. For example, you may have published a number of articles in your app, each with its own title, description, and image. When an article is shared on social media, you want that specific article's metadata to be displayed.

To configure dynamic data you need to specify an object class and the properties that contain the data you wish to use. Additionally, you must specify a query parameter for your URLs that will contain the identifier used to look up the object in the database. A URL that returns metadata based on dynamic data might look as follows: https://thirty50.appfarm.app/handbook/?article=zHHdE0UuaMlJmEanW1kRn

Consider security implications

There are some security considerations to be made when using dynamic data.

  • Consider using a dedicated object class

  • Enable Store Random Identifier on the object class

  • Unauthenticated access must be enabled

We recommend using a dedicated object class to store the metadata. All data in this object class should be considered public and should not contain any sensitive data. Using a dedicated object class helps limit this risk.

Another benefit of using a dedicated object class is that you can use it to store metadata for different types of objects. For example, if you publish events in addition to articles in your app, you might also want to return metadata for those as well. A dedicated object class for metadata allows you to do that.

We also recommend that you enable Store Random Identifier on the object class. This will store a unique, random string on each object when it is created that is separate to the ID property. This string can be safely shared publicly, so it should be used as the identifying property.

For dynamic data to function correctly your app must be configured for unauthenticated access and the service account used must have a role with read access to the object class used to store the metadata.

Configuration

Configure dynamic data in App Settings under Shareable Link Info.

When the app is loaded with a valid value for the specified query parameter, the meta tag for the Open Graph property og:url is populated with a value equal to the loaded URL, including the query string. In any other case, it is populated with your app's root URL.

Debugging

To verify that dynamic data is configured correctly you can use an Open Graph validator tool, like the Facebook Sharing Debugger. If your app is setup to load shared content, you can also inspect your app's code to see the tags for a given page.

Loading shared content

If a person accesses a shared link you will most likely want to load the relevant data into your app and display it. Using our earlier example with articles, you would open a view with the specified article ready for them to read.

To do this you can add a URL Parameter in App Data with the same name as the Query Param Name specified under Shareable Link Info. Then you can read in, or filter on, that object in an action triggered by On App Load, and display it.

If you want to add functionality within your app to share a particular URL, social media platforms typically provide a way for you to generate a share link. Say you have a button, "Share on LinkedIn", with an action connected to On Click. Within that action you can add an Open URL action node. Then, using the function editor, you can format a URL for the share link. You may have to encode the URL being shared.

LinkedIn example
const url = encodeURIComponent(`https://${hostname}/handbook/?article=${randomIdentifier}`);
return `https://www.linkedin.com/sharing/share-offsite/?url=${url}`;

Last updated