Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
26 changes: 25 additions & 1 deletion lib/__tests__/resolve-page-id.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { describe, expect, it } from 'vitest'

import { lookupSlug, resolvePageIdFromMaps } from '../resolve-page-id'
import {
isResolvablePageSlug,
lookupSlug,
resolvePageIdFromMaps
} from '../resolve-page-id'

const maps = {
pageUrlOverrides: {
Expand Down Expand Up @@ -50,3 +54,23 @@ describe('lookupSlug', () => {
).toBe('2bc5cb08-cf2c-810b-83af-c01fde10aefa')
})
})

describe('isResolvablePageSlug', () => {
it('accepts slugs in the committed index', () => {
expect(isResolvablePageSlug('oct-25', maps)).toBe(true)
})

it('accepts slugs from override-page collections', () => {
expect(
isResolvablePageSlug('rapid-prototyping-highlighty', maps, {
'rapid-prototyping-highlighty': '2bc5cb08-cf2c-810b-83af-c01fde10aefa'
})
).toBe(true)
})

it('rejects leftover posts that are not on any public listing', () => {
expect(
isResolvablePageSlug('rapid-prototyping-highlighty-part-2', maps)
).toBe(false)
})
})
15 changes: 15 additions & 0 deletions lib/resolve-page-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,18 @@ export function lookupSlug(
const pageId = slugMap[slug] || slugMap[normalized]
return pageId ? (parsePageId(pageId) ?? undefined) : undefined
}

/**
* True when `/<slug>` would resolve the same way `resolveNotionPage` does:
* committed index / URL overrides, then collection slugs from override pages.
* Used to keep the RSS feed from advertising leftover posts that 404.
*/
export function isResolvablePageSlug(
slug: string,
maps: PageIdLookupMaps,
extraSlugMap: Record<string, string> = {}
): boolean {
return Boolean(
resolvePageIdFromMaps(slug, maps) || lookupSlug(slug, extraSlugMap)
)
}
18 changes: 17 additions & 1 deletion pages/feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ import RSS from 'rss'
import * as config from '@/lib/config'
import { getSocialImageUrl } from '@/lib/get-social-image-url'
import { notion } from '@/lib/notion-api'
import { canonicalPageMap } from '@/lib/notion-index'
import { getPageSlug } from '@/lib/page-slug'
import { getCollectionBlockIds } from '@/lib/posts-collection'
import {
getCollectionBlockIds,
getOverrideCollectionSlugMap
} from '@/lib/posts-collection'
import { isResolvablePageSlug } from '@/lib/resolve-page-id'

export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
if (req.method !== 'GET') {
Expand Down Expand Up @@ -94,6 +99,13 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
return { props: {} }
}

const extraSlugMap = await getOverrideCollectionSlugMap()
const pageIdMaps = {
pageUrlOverrides: config.pageUrlOverrides,
pageUrlAdditions: config.pageUrlAdditions,
canonicalPageMap
}

// Get all pages that belong to this collection
for (const blockId of collectionBlockIds) {
const block =
Expand Down Expand Up @@ -124,6 +136,10 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
config.description

const slug = getPageSlug(block, recordMap) || pageId
if (!isResolvablePageSlug(slug, pageIdMaps, extraSlugMap)) {
console.warn('skipping feed item with unresolvable slug', slug)
continue
}
const url = `${config.host}/${slug}`

const lastUpdatedTime = getPageProperty<number>(
Expand Down