Skip to content

Commit

Permalink
fix(runtime): detect 404 api responses in SSG (#1608)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Oct 19, 2022
1 parent 4e66f1e commit 4a79197
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion playground/basic/content/0.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ title: Index

- [Playground](/playground)
- [Query Builder](/query-playground)
- [Not found](/404)
- [Custom 404 page](/404)
- [Not Found Content](/not-found-content)
9 changes: 8 additions & 1 deletion playground/basic/pages/[...slug].vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<template>
<div class="content-page">
<ContentDoc />
<ContentDoc>
<template #not-found>
Back
<NuxtLink to="/">
Home page
</NuxtLink>
</template>
</ContentDoc>
</div>
</template>

Expand Down
10 changes: 9 additions & 1 deletion src/runtime/composables/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ export const fetchContentNavigation = async (queryBuilder?: QueryBuilder | Query
return generateNavigation(params || {})
}

return $fetch(apiPath, {
const data = await $fetch(apiPath, {
method: 'GET',
responseType: 'json',
params: {
_params: jsonStringify(params || {}),
previewToken: useCookie('previewToken').value
}
})

// On SSG, all url are redirected to `404.html` when not found, so we need to check the content type
// to know if the response is a valid JSON or not
if (typeof data === 'string' && data.startsWith('<!DOCTYPE html>')) {
throw new Error('Not found')
}

return data
}
10 changes: 9 additions & 1 deletion src/runtime/composables/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,22 @@ export const createQueryFetch = <T = ParsedContent>(path?: string) => async (que
return db.fetch(query)
}

return $fetch(apiPath as any, {
const data = await $fetch(apiPath as any, {
method: 'GET',
responseType: 'json',
params: {
_params: jsonStringify(params),
previewToken: useCookie('previewToken').value
}
})

// On SSG, all url are redirected to `404.html` when not found, so we need to check the content type
// to know if the response is a valid JSON or not
if (typeof data === 'string' && data.startsWith('<!DOCTYPE html>')) {
throw new Error('Not found')
}

return data
}

/**
Expand Down

0 comments on commit 4a79197

Please sign in to comment.