Skip to content

Commit

Permalink
feat: add metadata for social media sharing
Browse files Browse the repository at this point in the history
* fix: change the name of the function into `configure_webpage_metadata`
  • Loading branch information
FabienArcellier committed Nov 21, 2024
1 parent 985be09 commit fd3ad1c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions docs/framework/seo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Writer Framework offers you the possibility of optimizing metadata to optimize y
The page title is editable for web crawlers. This title is a key element for the SEO of your application. A bot will not load the app. It will see `Writer Framework` by default.

```python
writer.serve.configure_page_head(title="My amazing app")
writer.serve.configure_webpage_metadata(title="My amazing app")
```

If you need dynamic title,you can use a function instead of a hard coded parameter. The title will be evaluated when the Robot loads the page.
Expand All @@ -20,7 +20,7 @@ def _title():
last_news = db.get_last_news()
return f"Last news: {last_news.title}"

writer.serve.configure_page_head(title=_title)
writer.serve.configure_webpage_metadata(title=_title)
```

### Configure meta tags
Expand All @@ -29,7 +29,7 @@ http headers allow you to specify a title, a description and keywords which will

*./server_setup.py*
```python
writer.serve.configure_page_head(
writer.serve.configure_webpage_metadata(
title="My amazing app",
meta={
"description": "my amazing app",
Expand All @@ -50,15 +50,15 @@ def _meta():
"author": "Amazing company"
}

writer.serve.configure_page_head(meta=_meta)
writer.serve.configure_webpage_metadata(meta=_meta)
```

### Configure social networks

When you share a link on social networks, they will try to fetch the metadata of the page to display a preview.

```python
writer.serve.configure_page_head(
writer.serve.configure_webpage_metadata(
opengraph_tags= {
"og:title": "My App",
"og:description": "My amazing app",
Expand All @@ -79,6 +79,6 @@ def _opengraph_tags():
"og:url": f"https://myapp.com/news/{last_news.id}"
}

writer.serve.configure_page_head(opengraph_tags=_opengraph_tags)
writer.serve.configure_webpage_metadata(opengraph_tags=_opengraph_tags)
```

10 changes: 5 additions & 5 deletions src/writer/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,15 +560,15 @@ async def lifespan(app: FastAPI):
async with _lifespan_invoke(writer_lifespans, app):
yield

def configure_page_head(
def configure_webpage_metadata(
title: Union[str, Callable[[], str]] = "Writer Framework",
meta: Optional[Union[Dict[str, Any], Callable[[], Dict[str, Any]]]] = None,
opengraph_tags: Optional[Union[Dict[str, Any], Callable[[], Dict[str, Any]]]] = None
):
"""
Configures the page header for SEO and social networks from `server_setup` module.
>>> writer.serve.configure_page_head(
>>> writer.serve.configure_webpage_metadata(
>>> title="my App",
>>> meta={
>>> "description": "my amazing app",
Expand All @@ -591,14 +591,14 @@ def configure_page_head(
>>> "author": "Amazing company"
>>> }
>>> writer.serve.configure_page_head(
>>> writer.serve.configure_webpage_metadata(
>>> title=generated_title
>>> meta=generated_meta_tags
>>> )
OpenGraph tags are used by social networks to display information about the page. WF support them.
>>> writer.serve.configure_page_head(
>>> writer.serve.configure_webpage_metadata(
>>> title=generated_title
>>> opengraph_tags= {
>>> "og:title": "My App",
Expand All @@ -614,7 +614,7 @@ def configure_page_head(
>>> "og:description": "My amazing app",
>>> }
>>> writer.serve.configure_page_head(
>>> writer.serve.configure_webpage_metadata(
>>> title=generated_title
>>> opengraph_tags= generated_opengraph_tags
>>> )
Expand Down

0 comments on commit fd3ad1c

Please sign in to comment.