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

fix: detect 404 api responses in SSG #1608

Merged
merged 1 commit into from
Oct 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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