Skip to content

Commit

Permalink
fix(storefront): Properly catching not found content files by slug on…
Browse files Browse the repository at this point in the history
… route context

Returns 404 and redirect to fallback
  • Loading branch information
leomp12 committed Mar 9, 2024
1 parent 6a003e7 commit 32dd183
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/storefront/config/storefront.cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ global.__storefrontCMS = (fs, resolvePath, _parseMatter) => {
contentCache[filename] = content;
}
}
if (typeof content.then === 'function') return content;
if (typeof content?.then === 'function') return content;
return filename === 'settings'
? content
: new Promise((resolve) => { resolve(content); });
Expand Down
8 changes: 7 additions & 1 deletion packages/storefront/src/lib/ssr-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ const loadRouteContext = async (
if (cmsContent) cmsContent.$filename = contentFilename;
}
try {
if (contentFilename && !cmsContent) {
const error = new Error('Content file does not exist by current slug') as
Partial<ApiError> & { message: string };
error.statusCode = 404;
throw error;
}
(await Promise.all(apiPrefetchings)).forEach((response) => {
if (response) {
const { config: { endpoint }, data } = response;
Expand All @@ -256,7 +262,7 @@ const loadRouteContext = async (
}
});
} catch (err: any) {
const error: ApiError = err;
const error: Partial<ApiError> & { message: string } = err;
const status = error.statusCode || 500;
if (status === 404) {
if (urlPath.endsWith('/')) {
Expand Down

0 comments on commit 32dd183

Please sign in to comment.