Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate ld+json data #3444

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"forwardPorts": [
3000,
9000,
9091, // used by docker production
24678 // used by nuxt when hot-reloading using polling
],
// Use 'onCreateCommand' to run commands at the end of container creation.
Expand Down
28 changes: 23 additions & 5 deletions mealie/routes/spa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,24 @@ def inject_meta(contents: str, tags: list[MetaTag]) -> str:


def inject_recipe_json(contents: str, schema: dict) -> str:
schema_as_html_tag = f"""<script type="application/ld+json">{json.dumps(jsonable_encoder(schema))}</script>"""
return contents.replace("</head>", schema_as_html_tag + "\n</head>", 1)
soup = BeautifulSoup(contents, "lxml")
schema_as_json = json.dumps(jsonable_encoder(schema))

script_tags = soup.find_all("script", {"type": "application/ld+json"})
for script_tag in script_tags:
try:
data = json.loads(script_tag.string)
if data.get("@type") == "Recipe":
script_tag.string = schema_as_json
break
except json.JSONDecodeError:
continue
else:
schema_as_html_tag = soup.new_tag("script", type="application/ld+json")
schema_as_html_tag.string = schema_as_json
soup.head.append(schema_as_html_tag)

return str(soup)
michael-genson marked this conversation as resolved.
Show resolved Hide resolved


def content_with_meta(group_slug: str, recipe: Recipe) -> str:
Expand Down Expand Up @@ -145,9 +161,11 @@ def content_with_meta(group_slug: str, recipe: Recipe) -> str:
]

global __contents
__contents = inject_recipe_json(__contents, as_schema_org)
__contents = inject_meta(__contents, meta_tags)
return __contents
contents = __contents # make a local copy so we don't modify the global contents
contents = inject_recipe_json(contents, as_schema_org)
contents = inject_meta(contents, meta_tags)

return contents


def response_404():
Expand Down
Loading