Skip to content

Commit d71a6ef

Browse files
committed
fix(seo): restore the homepage OpenGraph image
`getOgImageUrl` strips the leading and trailing slashes off the pathname to build the image filename. For the homepage that pathname is `/`, so stripping left an empty string and the lookup missed — every docs page had an OpenGraph image and the homepage shipped `<meta property="og:image">` with no content. The homepage's collection id is `index`, which is what `getStaticPaths` names its image, so fall back to that when the slug comes out empty. Covered by a regression test that fails against the old expression. The generated-image set comes from the content collection and needs the Astro build pipeline, so the test stubs it and exercises the derivation, which is the half that was wrong. Change-Id: Ifb9a23ea2caa20d28489a4f21363d85ed5e3342c
1 parent 3a10326 commit d71a6ef

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/util/getOgImageUrl.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
import { getOgImageUrl } from './getOgImageUrl';
3+
4+
// `getOgImageUrl` looks a derived filename up in the set of images
5+
// astro-og-canvas actually generated, which comes from the content collection
6+
// and so needs the Astro build pipeline. Stub that set and test the derivation
7+
// — the half where the homepage bug was.
8+
vi.mock('../pages/open-graph/[...path]', () => ({
9+
getStaticPaths: async () => [
10+
{ params: { path: 'index.png' } },
11+
{ params: { path: 'merge-queue.png' } },
12+
],
13+
}));
14+
15+
describe('getOgImageUrl', () => {
16+
it('resolves the homepage to the index image', () => {
17+
// Regression: stripping the slashes off `/` left an empty slug, so the
18+
// homepage was the one page that shipped an empty `og:image`.
19+
expect(getOgImageUrl('/')).toBe('/open-graph/index.png');
20+
});
21+
22+
it('resolves a normal page, with or without a trailing slash', () => {
23+
expect(getOgImageUrl('/merge-queue')).toBe('/open-graph/merge-queue.png');
24+
expect(getOgImageUrl('/merge-queue/')).toBe('/open-graph/merge-queue.png');
25+
});
26+
27+
it('returns undefined when no image was generated', () => {
28+
expect(getOgImageUrl('/not-a-page')).toBeUndefined();
29+
});
30+
});

src/util/getOgImageUrl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const paths = new Set(routes.map(({ params }) => params.path));
2020
* @returns Path to the OpenGraph image if found. Otherwise, `undefined`.
2121
*/
2222
export function getOgImageUrl(path: string): string | undefined {
23-
const imagePath = path.replace(/^\//, '').replace(/\/$/, '') + '.png';
23+
// The homepage's collection id is `index`, so stripping its slashes leaves an
24+
// empty string and the lookup misses — which is why the homepage shipped with
25+
// an empty `og:image` while every other page had one.
26+
const slug = path.replace(/^\//, '').replace(/\/$/, '') || 'index';
27+
const imagePath = slug + '.png';
2428
if (paths.has(imagePath)) return '/open-graph/' + imagePath;
2529
}

0 commit comments

Comments
 (0)