Skip to content
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
161 changes: 56 additions & 105 deletions src/components/overrides/Head.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,41 @@ import { differenceInCalendarDays } from "date-fns";
import "tippy.js/dist/tippy.css";

import { getEntry } from "astro:content";
import { getOgImage } from "~/util/props";
import { CollectionEntry } from "astro:content";

// grab the current top-level folder. Remove . characters for 1.1.1.1 URL
const currentSection = Astro.url.pathname.split("/")[1].replaceAll(".", "");
const head = Astro.props.entry.data.head;

if (currentSection) {
const product = await getEntry("products", currentSection);

if (product) {
if (product.data.meta.title) {
const titleIdx = Astro.props.entry.data.head.findIndex(
(x) => x.tag === "title",
);
const titleIdx = head.findIndex((x) => x.tag === "title");

let title: string;

if (titleIdx !== -1) {
const existingTitle = Astro.props.entry.data.head[titleIdx].content;
const existingTitle = head[titleIdx].content;
title = `${existingTitle} · ${product.data.meta.title}`;

Astro.props.entry.data.head[titleIdx] = {
head[titleIdx] = {
tag: "title",
attrs: {},
content: title,
};
} else {
title = `${Astro.props.entry.data.title} · ${product.data.meta.title}`;
Astro.props.entry.data.head.push({
head.push({
tag: "title",
attrs: {},
content: title,
});
}

Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: { property: "og:title", content: title },
content: "",
Expand All @@ -47,15 +48,15 @@ if (currentSection) {

if (product.data.product.title) {
const productName = product.data.product.title;
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
name: "pcx_product",
content: productName,
},
content: "",
});
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
name: "algolia_product_filter",
Expand All @@ -66,58 +67,7 @@ if (currentSection) {
}

if (product.data.product.group) {
const group = product.data.product.group.toLowerCase();

let ogImage = "https://developers.cloudflare.com/cf-twitter-card.png";

const images: Record<string, string> = {
"cloudflare essentials":
"https://developers.cloudflare.com/core-services-preview.png",
"cloudflare one": "https://developers.cloudflare.com/zt-preview.png",
"developer platform":
"https://developers.cloudflare.com/dev-products-preview.png",
"network security":
"https://developers.cloudflare.com/core-services-preview.png",
"application performance":
"https://developers.cloudflare.com/core-services-preview.png",
"application security":
"https://developers.cloudflare.com/core-services-preview.png",
};

if (images[group]) {
ogImage = images[group];
}

const tags = [
{
tag: "meta",
attrs: {
name: "image",
content: ogImage,
},
content: "",
},
{
tag: "meta",
attrs: {
name: "og:image",
content: ogImage,
},
content: "",
},
{
tag: "meta",
attrs: {
name: "twitter:image",
content: ogImage,
},
content: "",
},
] as const;

Astro.props.entry.data.head.push(...tags);

Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
name: "pcx_content_group",
Expand All @@ -129,7 +79,7 @@ if (currentSection) {
}

if (currentSection === "style-guide") {
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
name: "robots",
Expand All @@ -138,46 +88,12 @@ if (currentSection) {
content: "",
});
}

if (currentSection === "changelog") {
let changelogImage =
"https://developers.cloudflare.com/changelog-preview.png";

const tags = [
{
tag: "meta",
attrs: {
name: "image",
content: changelogImage,
},
content: "",
},
{
tag: "meta",
attrs: {
name: "og:image",
content: changelogImage,
},
content: "",
},
{
tag: "meta",
attrs: {
name: "twitter:image",
content: changelogImage,
},
content: "",
},
] as const;

Astro.props.entry.data.head.push(...tags);
}
}

// Add noindex tag if present in frontmatter

if (Astro.props.entry.data.noindex) {
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
name: "robots",
Expand All @@ -191,15 +107,15 @@ if (Astro.props.entry.data.noindex) {
// content type
if (Astro.props.entry.data.pcx_content_type) {
const contentType = Astro.props.entry.data.pcx_content_type;
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
name: "pcx_content_type",
content: contentType,
},
content: "",
});
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
name: "algolia_content_type",
Expand All @@ -212,7 +128,7 @@ if (Astro.props.entry.data.pcx_content_type) {
// other products
if (Astro.props.entry.data.products) {
const additionalProducts = Astro.props.entry.data.products;
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
name: "pcx_additional_products",
Expand All @@ -225,7 +141,7 @@ if (Astro.props.entry.data.products) {
// other products
if (Astro.props.entry.data.tags) {
const pageTags = Astro.props.entry.data.tags;
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
name: "pcx_tags",
Expand All @@ -240,7 +156,7 @@ if (Astro.props.entry.data.updated) {
new Date(),
Astro.props.entry.data.updated,
);
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
name: "pcx_last_reviewed",
Expand All @@ -256,7 +172,7 @@ if (Astro.props.entry.data.pcx_content_type === "changelog") {
const href = new URL(Astro.site ?? "");
href.pathname = Astro.props.entry.slug.concat("/index.xml");

Astro.props.entry.data.head.push({
head.push({
tag: "link",
attrs: {
rel: "alternate",
Expand All @@ -268,15 +184,15 @@ if (Astro.props.entry.data.pcx_content_type === "changelog") {
}

if (Astro.props.entry.data.external_link) {
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
content: "noindex",
name: "robots",
},
content: "",
});
Astro.props.entry.data.head.push({
head.push({
tag: "meta",
attrs: {
content: `0; url=${Astro.props.entry.data.external_link}`,
Expand All @@ -285,6 +201,41 @@ if (Astro.props.entry.data.external_link) {
content: "",
});
}

const ogImagePath = await getOgImage(
(Astro.props.originalEntry as CollectionEntry<"changelog">) ??
Astro.props.entry,
);
const ogImageUrl = new URL(ogImagePath, Astro.url.origin).toString();

const ogImageTags = [
{
tag: "meta",
attrs: {
name: "image",
content: ogImageUrl,
},
content: "",
},
{
tag: "meta",
attrs: {
name: "og:image",
content: ogImageUrl,
},
content: "",
},
{
tag: "meta",
attrs: {
name: "twitter:image",
content: ogImageUrl,
},
content: "",
},
] as const;

head.push(...ogImageTags);
---

<script src="src/scripts/footnotes.ts"></script>
Expand Down
1 change: 1 addition & 0 deletions src/content/docs/workers/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ sidebar:
head:
- tag: title
content: Cloudflare Workers
cover: ~/assets/images/workers/kv-write.svg
---

import {
Expand Down
1 change: 1 addition & 0 deletions src/pages/changelog/[...slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const props = {
headings,
hideTitle: true,
hideBreadcrumbs: true,
originalEntry: note,
} as StarlightPageProps;
---

Expand Down
Loading
Loading