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

Feature/initial words to build #552

Merged
merged 4 commits into from
Oct 26, 2024
Merged
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
2 changes: 1 addition & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
"react": "18.3.1",
"react-dom": "18.3.1",
"reset-css": "^5.0.2",
"riimut": "^0.9.1",
"sass": "^1.77.2",
"scandinavian-dictionary-crosslinker": "^0.6.0",
"sitemap": "^7.1.1",
"slugify": "^1.6.6",
"teljari": "^1.1.1",
"younger-futhark": "^1.2.5"
"teljari": "^1.1.1"
},
"devDependencies": {
"@types/jest": "^29.5.12",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function Footer({ letters }: FooterProps) {
<li>
<ExternalLink
title="Younger Futhark Runes"
href="https://github.com/stscoundrel/younger-futhark"
href="https://github.com/stscoundrel/riimut"
/>
</li>
</ul>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Layout/__snapshots__/Layout.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ exports[`Layout component Letter layout Matches snapshot 1`] = `
</li>
<li>
<a
href="https://github.com/stscoundrel/younger-futhark"
href="https://github.com/stscoundrel/riimut"
rel="noopener noreferrer"
target="_blank"
>
Expand Down Expand Up @@ -1694,7 +1694,7 @@ exports[`Layout component Word layout Matches snapshot 1`] = `
</li>
<li>
<a
href="https://github.com/stscoundrel/younger-futhark"
href="https://github.com/stscoundrel/riimut"
rel="noopener noreferrer"
target="_blank"
>
Expand Down
7 changes: 7 additions & 0 deletions src/components/WordDefinition/WordDefinition.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const crosslinks = [
},
]

const runes = 'ᛅᚠ-ᛒᚢᚱᚦᚱ'

describe('WordDefinition component', () => {
test('Does not crash', () => {
const div = document.createElement('div')
Expand All @@ -65,6 +67,7 @@ describe('WordDefinition component', () => {
abbreviations={abbreviations}
similarEntries={[]}
crosslinks={crosslinks}
runes={runes}
/>,
)
})
Expand All @@ -76,6 +79,7 @@ describe('WordDefinition component', () => {
abbreviations={abbreviations}
similarEntries={[]}
crosslinks={crosslinks}
runes={runes}
/>,
).toJSON()
expect(tree).toMatchSnapshot()
Expand All @@ -88,6 +92,7 @@ describe('WordDefinition component', () => {
similarEntries={[]}
abbreviations={abbreviations}
crosslinks={crosslinks}
runes="ᚢᚱ-ᚢᛅᚾᛅ"
/>,
).toJSON()
expect(tree).toMatchSnapshot()
Expand All @@ -100,6 +105,7 @@ describe('WordDefinition component', () => {
abbreviations={abbreviations}
similarEntries={[]}
crosslinks={crosslinks}
runes={runes}
/>,
)
const { root } = tree
Expand All @@ -114,6 +120,7 @@ describe('WordDefinition component', () => {
abbreviations={abbreviations}
similarEntries={[]}
crosslinks={crosslinks}
runes={runes}
/>,
)
const { root } = tree
Expand Down
12 changes: 6 additions & 6 deletions src/components/WordDefinition/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { lettersToRunes } from 'younger-futhark'
import { Crosslink } from 'scandinavian-dictionary-crosslinker'
import type { Crosslink } from 'scandinavian-dictionary-crosslinker'
import { capitalize, getOlderSpelling } from 'lib/utils/strings'
import { addAbbreviationsToContent, CombinedAbbreviations } from 'lib/services/abbreviations'
import { addAbbreviationsToContent, type CombinedAbbreviations } from 'lib/services/abbreviations'
import Abbreviations from 'components/Abbreviations'
import { DictionaryEntry } from 'lib/services/dictionary'
import type { DictionaryEntry } from 'lib/services/dictionary'
import Crosslinks from 'components/Crosslinks'
import SimilarEntries from 'components/SimilarEntries'
import styles from './WordDefinition.module.scss'
Expand All @@ -13,10 +12,11 @@ interface WordDefinitionProps{
similarEntries: DictionaryEntry[],
abbreviations: CombinedAbbreviations,
crosslinks: Crosslink[],
runes: string,
}

export default function WordDefinition({
entry, similarEntries, abbreviations, crosslinks,
entry, similarEntries, abbreviations, crosslinks, runes,
}: WordDefinitionProps) {
const { word, definitions } = entry
const olderForm = getOlderSpelling(word)
Expand Down Expand Up @@ -57,7 +57,7 @@ export default function WordDefinition({
</p>}

<p>Possible runic inscription in <em>Younger Futhark</em>:
<span className={styles.rune}>{ lettersToRunes(word) }</span><br />
<span className={styles.rune}>{ runes }</span><br />
<small>Younger Futhark runes were used from 8th to 12th centuries
in Scandinavia and their overseas settlements</small>
</p>
Expand Down
23 changes: 22 additions & 1 deletion src/lib/services/dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getDictionary, DictionaryEntry as RawDictionaryEntry } from 'cleasby-vigfusson-dictionary'
import { getDictionary } from 'cleasby-vigfusson-dictionary'
import type { DictionaryEntry as RawDictionaryEntry } from 'cleasby-vigfusson-dictionary'
import { VALID_AS_FIRST } from 'old-norse-alphabet'
import { oldNorseSort } from 'old-norse-alphabet-sort'
import { slugifyWord, slugifyLetter } from '../utils/slugs'
Expand All @@ -18,6 +19,7 @@ export interface AlphabetLetter {
}

let cachedDictionary: DictionaryEntry[] | null = null
let cachedInitialPages: string[] | null = null

const addSlugs = (words: RawDictionaryEntry[]): DictionaryEntry[] => {
const existingSlugs = {}
Expand Down Expand Up @@ -113,6 +115,25 @@ export const getAlphabet = (): AlphabetLetter[] => {
return formattedLetters
}

/**
* Initial word pages to build are basically 5000
* headword pages based on modulus. Larger number
* can not be deployed in one go.
*/
export const getInitialWordsToBuild = (): string[] => {
if (cachedInitialPages) return cachedInitialPages

const allWords = getAllWords()

const result: string[] = []
for (let i = 0; i < allWords.length; i += 7) {
result.push(allWords[i].slug);
}

cachedInitialPages = result
return cachedInitialPages
}

export default {
getAllWords,
getByLetter,
Expand Down
7 changes: 5 additions & 2 deletions src/pages/api/revalidate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from 'next'
import type { NextApiRequest, NextApiResponse } from 'next'

import { getAllWords } from 'lib/services/dictionary'
import { getAllWords, getInitialWordsToBuild } from 'lib/services/dictionary'
import { getWordPath } from 'lib/utils/links'

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
Expand All @@ -18,9 +18,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

try {
const words = getAllWords()
const initialWords = getInitialWordsToBuild()

const { start, end: givenEnd } = req.query
const end = parseInt(givenEnd, 10) <= words.length ? parseInt(givenEnd, 10) : words.length - 1
const revalidates = words
.filter((word) => !initialWords.includes(word.slug))
.sort((a, b) => a.slug.localeCompare(b.slug))
.map((word) => getWordPath(word))
.slice(parseInt(start, 10), end)
Expand Down
33 changes: 25 additions & 8 deletions src/pages/word/[word].tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { useRouter } from 'next/router'

// Services.
import { Crosslink } from 'scandinavian-dictionary-crosslinker'
import type { Crosslink } from 'scandinavian-dictionary-crosslinker'
import {
getWord, getAlphabet, DictionaryEntry, AlphabetLetter, getSimilarWords,
getWord, getAlphabet, type DictionaryEntry, type AlphabetLetter, getSimilarWords,
getInitialWordsToBuild,
} from 'lib/services/dictionary'
import { CombinedAbbreviations, getAbbreviations } from 'lib/services/abbreviations'
import { type CombinedAbbreviations, getAbbreviations } from 'lib/services/abbreviations'

// Utils.
import { redirect404, Redirect404ResponseSchema } from 'lib/utils/redirect-404'
import { redirect404, type Redirect404ResponseSchema } from 'lib/utils/redirect-404'

// Components.
import Layout from 'components/Layout'
import WordDefinition from 'components/WordDefinition'
import Button from 'components/Button'
import { decodeLetter } from 'lib/utils/slugs'
import { getCrossLinks } from 'lib/services/crosslinks'
import { youngerFuthark } from 'riimut'

interface WordPageProps{
entry: DictionaryEntry,
Expand All @@ -24,6 +26,7 @@ interface WordPageProps{
letter: AlphabetLetter,
abbreviations: CombinedAbbreviations,
crosslinks: Crosslink[],
runes: string,
}

interface WordPageParams{
Expand All @@ -32,8 +35,14 @@ interface WordPageParams{
}
}

interface WordPath{
params: {
word: string
}
}

interface WordPageStaticPathsResponseSchema{
paths: string[]
paths: WordPath[]
fallback: string | boolean
}

Expand All @@ -45,11 +54,16 @@ interface WordPageStaticPropsResponseSchema{
* There are too many word paths for Vercel to build.
* It hits 16 000 file limit.
*
* Let's just build them as they are accessed.
* Build around 5000 pages initially and rest as they are accessed
* or remotely revalidated via API.
*/
export async function getStaticPaths(): Promise<WordPageStaticPathsResponseSchema> {
const initialPages = getInitialWordsToBuild()

return {
paths: [],
paths: initialPages.map((slug) => ({
params: { word: slug },
})),
fallback: 'blocking',
}
}
Expand All @@ -76,6 +90,7 @@ export async function getStaticProps(
)[0]
const abbreviations = getAbbreviations(entry)
const crosslinks = getCrossLinks(entry)
const runes = youngerFuthark.lettersToRunes(entry.word)

return {
props: {
Expand All @@ -85,12 +100,13 @@ export async function getStaticProps(
letters,
abbreviations,
crosslinks,
runes,
},
}
}

export default function Word({
entry, similarEntries, letters, abbreviations, crosslinks,
entry, similarEntries, letters, abbreviations, crosslinks, runes,
}) {
const router = useRouter()

Expand All @@ -105,6 +121,7 @@ export default function Word({
similarEntries={similarEntries}
abbreviations={abbreviations}
crosslinks={crosslinks}
runes={runes}
/>
<Button text="Back" action={() => router.back()} />
</Layout>
Expand Down
22 changes: 18 additions & 4 deletions tests/unit/lib/services/dictionary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isArray } from 'volva'
import { matchesSchema } from 'jafningjar'
import { oldNorseSort } from 'old-norse-alphabet-sort'
import {
getAllWords, getByLetter, getWord, getAlphabet, getSimilarWords,
getAllWords, getByLetter, getWord, getAlphabet, getSimilarWords, getInitialWordsToBuild,
} from 'lib/services/dictionary'

describe('Dictionary tests', () => {
Expand Down Expand Up @@ -151,9 +151,6 @@ describe('Dictionary tests', () => {
expect(foundÖ).toBeTruthy();
})

/**
* This test needs Node 14 to pass.
*/
test('Dictionary entries are alphabetically sorted', () => {
const maybeUnsorted = getDictionary()

Expand All @@ -162,4 +159,21 @@ describe('Dictionary tests', () => {

expect(maybeUnsorted).toEqual(sortedDictionry)
})

test('Returns initial batch of pages to build', () => {
const wordsToBuild = getInitialWordsToBuild()

// Correct amount sampled.
expect(wordsToBuild.length).toEqual(5030)

// Deterministic entry slugs, roughly spread through dictionary.
expect(wordsToBuild[0]).toEqual('a')
expect(wordsToBuild[10]).toEqual('af-heima')
expect(wordsToBuild[100]).toEqual('althydu-mal')
expect(wordsToBuild[1000]).toEqual('flosa-legr')
expect(wordsToBuild[2000]).toEqual('haeti')
expect(wordsToBuild[3000]).toEqual('nafn')
expect(wordsToBuild[4000]).toEqual('stefnu-timi')
expect(wordsToBuild[5000]).toEqual('ond')
})
})
2 changes: 1 addition & 1 deletion tests/unit/pages/__snapshots__/404.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ exports[`404 page page Matches snapshot 1`] = `
</li>
<li>
<a
href="https://github.com/stscoundrel/younger-futhark"
href="https://github.com/stscoundrel/riimut"
rel="noopener noreferrer"
target="_blank"
>
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/pages/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -635220,7 +635220,7 @@ exports[`Index page Matches snapshot 1`] = `
</li>
<li>
<a
href="https://github.com/stscoundrel/younger-futhark"
href="https://github.com/stscoundrel/riimut"
rel="noopener noreferrer"
target="_blank"
>
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/pages/__snapshots__/letter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16149,7 +16149,7 @@ exports[`Letter page: render Matches snapshot 1`] = `
</li>
<li>
<a
href="https://github.com/stscoundrel/younger-futhark"
href="https://github.com/stscoundrel/riimut"
rel="noopener noreferrer"
target="_blank"
>
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/pages/__snapshots__/search.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ exports[`Search page: render Matches snapshot 1`] = `
</li>
<li>
<a
href="https://github.com/stscoundrel/younger-futhark"
href="https://github.com/stscoundrel/riimut"
rel="noopener noreferrer"
target="_blank"
>
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/pages/__snapshots__/sources.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5047,7 +5047,7 @@ exports[`Sources page Matches snapshot 1`] = `
</li>
<li>
<a
href="https://github.com/stscoundrel/younger-futhark"
href="https://github.com/stscoundrel/riimut"
rel="noopener noreferrer"
target="_blank"
>
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/pages/__snapshots__/word.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ exports[`Word page: render & usage Matches snapshot 1`] = `
</li>
<li>
<a
href="https://github.com/stscoundrel/younger-futhark"
href="https://github.com/stscoundrel/riimut"
rel="noopener noreferrer"
target="_blank"
>
Expand Down
Loading
Loading