not found on empty search

This commit is contained in:
keyan 2022-01-27 14:15:18 -06:00
parent afed19430c
commit f1fd1f3c9c
2 changed files with 11 additions and 3 deletions

View File

@ -31,12 +31,20 @@ export default async function getSSRApolloClient (req, me = null) {
})
}
export function getGetServerSideProps (query, variables = null, foundField) {
export function getGetServerSideProps (query, variables = null, foundField, requireVar) {
return async function ({ req, query: params }) {
const client = await getSSRApolloClient(req)
const vars = { ...params, ...variables }
if (requireVar && !vars[requireVar]) {
return {
notFound: true
}
}
const { error, data } = await client.query({
query,
variables: { ...params, ...variables }
variables: vars
})
if (error || !data || (foundField && !data[foundField])) {

View File

@ -4,7 +4,7 @@ import { ITEM_SEARCH } from '../fragments/items'
import SearchItems from '../components/search-items'
import { useRouter } from 'next/router'
export const getServerSideProps = getGetServerSideProps(ITEM_SEARCH)
export const getServerSideProps = getGetServerSideProps(ITEM_SEARCH, null, null, 'q')
export default function Index ({ data: { search: { items, cursor } } }) {
const router = useRouter()