- journal.routes.search (off by default) registers journal-search, the results page for ?q= in the API's relevance order (no sort is sent), server-rendered with a Polish-aware result count. - JournalSearchBox: an icon button and a WAI-ARIA combobox with debounced ?fields=suggest suggestions, stale answers dropped, query words marked ignoring diacritics; a plain GET form without JS. - The module keeps the search path out of the route cache and the sitemap and marks it noindex, follow. - Client: suggestPosts and the fields option; useJournalLinks.searchLink.
78 lines
3.5 KiB
Vue
78 lines
3.5 KiB
Vue
<script setup lang="ts">
|
|
// journal-search: the posts matching ?q, paginated with ?page=N, in the API's
|
|
// relevance order (no sort is sent). Rendered on the server, so the form and
|
|
// the results work without JS. The module keeps the page out of the route
|
|
// cache and the sitemap and marks it noindex, follow. An invalid or
|
|
// out-of-range page is a real 404.
|
|
import { JOURNAL_SEARCH_MIN_LENGTH } from '../utils/client.ts'
|
|
import { pageOutOfRange, parsePageParam } from '../utils/pagination.ts'
|
|
import { pluralForm, searchTermFromQuery, searchTermState } from '../utils/search.ts'
|
|
import { listTitle } from '../utils/seo.ts'
|
|
import { toListPost } from '../utils/view.ts'
|
|
import type { JournalModuleOptions } from '../utils/types.ts'
|
|
|
|
definePageMeta({ key: (route) => route.fullPath })
|
|
|
|
const route = useRoute()
|
|
const api = useJournalApi()
|
|
const { t, localeProperties } = useI18n()
|
|
const cfg = useRuntimeConfig().public.journal as JournalModuleOptions
|
|
|
|
const q = searchTermFromQuery(route.query.q)
|
|
const termState = searchTermState(q)
|
|
const page = parsePageParam(route.query.page)
|
|
if (page === null) throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
|
|
|
|
const { data, error } = await useAsyncData(`journal-search-${q}-${page}`, async () => {
|
|
if (termState !== 'ready') return null
|
|
const posts = await api.listPosts({ search: q, page, perPage: cfg.perPage.search ?? cfg.perPage.index })
|
|
return { ...posts, data: posts.data.map(toListPost) }
|
|
})
|
|
if (error.value) {
|
|
throw createError({ statusCode: (error.value as { statusCode?: number }).statusCode ?? 500, fatal: true })
|
|
}
|
|
if (data.value && pageOutOfRange(page, data.value.meta.last_page)) {
|
|
throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true })
|
|
}
|
|
|
|
const total = data.value?.meta.total ?? 0
|
|
const count = t(`journal.search.count.${pluralForm(localeProperties.value.language ?? 'en', total)}`, { count: total })
|
|
const heading = q ? t('journal.search.titleQuery', { query: q }) : t('journal.search.title')
|
|
const title = listTitle(heading, page, t('journal.pagination.pageTitle', { page }))
|
|
useSeoMeta({ title, ogTitle: title })
|
|
|
|
const term = ref(q)
|
|
const onSubmit = () => {
|
|
const value = term.value.trim()
|
|
return navigateTo({ path: route.path, query: value ? { q: value } : {} })
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<section class="journal-search-page">
|
|
<header class="journal-index-header">
|
|
<h1 class="journal-index-header__title">{{ title }}</h1>
|
|
</header>
|
|
<form class="journal-search-page__form" role="search" method="get" :action="route.path" @submit.prevent="onSubmit">
|
|
<input
|
|
v-model="term"
|
|
class="journal-search-page__input"
|
|
type="search"
|
|
name="q"
|
|
:aria-label="$t('journal.search.input')"
|
|
:placeholder="$t('journal.search.placeholder')"
|
|
/>
|
|
<button type="submit" class="journal-search-page__submit">{{ $t('journal.search.submit') }}</button>
|
|
</form>
|
|
<p v-if="termState === 'empty'" class="journal-search-page__message">{{ $t('journal.search.prompt') }}</p>
|
|
<p v-else-if="termState === 'short'" class="journal-search-page__message">
|
|
{{ $t('journal.search.short', { min: JOURNAL_SEARCH_MIN_LENGTH }) }}
|
|
</p>
|
|
<template v-else-if="data">
|
|
<p v-if="total > 0" class="journal-search-page__count">{{ count }}</p>
|
|
<JournalPostList :posts="data.data" :empty="$t('journal.search.none', { query: q })" />
|
|
<JournalPagination :current="data.meta.current_page" :last="data.meta.last_page" />
|
|
</template>
|
|
</section>
|
|
</template>
|