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

Add a friendly message when building nextjs app and site items are missing #1066

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import {
SitecoreContext,
ComponentPropsContext,
handleEditorFastRefresh,
<% if (prerender === 'SSG') { -%>
StaticPath,
<% } -%>
} from '@sitecore-jss/sitecore-jss-nextjs';
import { SitecorePageProps } from 'lib/page-props';
import { sitecorePagePropsFactory } from 'lib/page-props-factory';
// different componentFactory method will be used based on whether page is being edited
import { componentFactory, editingComponentFactory } from 'temp/componentFactory';
<% if (prerender === 'SSG') { -%>
import { sitemapFetcher } from 'lib/sitemap-fetcher';

<% } -%>

const SitecorePage = ({ notFound, componentProps, layoutData }: SitecorePageProps): JSX.Element => {
Expand Down Expand Up @@ -56,19 +60,24 @@ export const getStaticPaths: GetStaticPaths = async (context) => {
// ahead of time (non-development mode in this example).
// See https://nextjs.org/docs/basic-features/data-fetching#incremental-static-regeneration

let paths: StaticPath[] = [];
let fallback: boolean | 'blocking' = 'blocking';

if (process.env.NODE_ENV !== 'development') {
// Note: Next.js runs export in production mode
const paths = await sitemapFetcher.fetch(context);
try {
// Note: Next.js runs export in production mode
paths = await sitemapFetcher.fetch(context);
} catch (error) {
console.log('Error occurred while generating static paths');
console.log(error);
}

return {
paths,
fallback: process.env.EXPORT_MODE ? false : 'blocking',
};
fallback = process.env.EXPORT_MODE ? false : fallback;
}

return {
paths: [],
fallback: 'blocking',
paths,
fallback,
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import nock from 'nock';
import {
getSiteEmptyError,
GraphQLSitemapService,
GraphQLSitemapServiceConfig,
languageError,
Expand Down Expand Up @@ -360,6 +361,24 @@ describe('GraphQLSitemapService', () => {
});
});

it('should throw error if query returns nothing for a provided site name', async () => {
const service = new GraphQLSitemapService({ endpoint, apiKey, siteName });
nock(endpoint)
.post('/', (body) => {
return body.variables.siteName === siteName;
})
.reply(200, {
data: {
site: {
siteInfo: null,
},
},
});
await service.fetchSSGSitemap(['en']).catch((error: RangeError) => {
expect(error.message).to.equal(getSiteEmptyError(siteName));
});
});

it('should throw error if empty language is provided', async () => {
mockPathsRequest();

Expand Down Expand Up @@ -503,6 +522,24 @@ describe('GraphQLSitemapService', () => {
});
});

it('should throw error if query returns nothing for a provided site name', async () => {
art-alexeyenko marked this conversation as resolved.
Show resolved Hide resolved
const service = new GraphQLSitemapService({ endpoint, apiKey, siteName });
nock(endpoint)
.post('/', (body) => {
return body.variables.siteName === siteName;
})
.reply(200, {
data: {
site: {
siteInfo: null,
},
},
});
await service.fetchExportSitemap('en').catch((error: RangeError) => {
expect(error.message).to.equal(getSiteEmptyError(siteName));
});
});

it('should provide a default GraphQL client', () => {
const service = new TestService({
endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { getPersonalizedRewrite } from '@sitecore-jss/sitecore-jss/personalize';

/** @private */
export const languageError = 'The list of languages cannot be empty';
/**
* @param siteName
*/
/** @private */
export function getSiteEmptyError(siteName: string) {
art-alexeyenko marked this conversation as resolved.
Show resolved Hide resolved
return `Site "${siteName}" does not exist or site item tree is missing`;
}

const languageEmptyError = 'The language must be a non-empty string';

Expand Down Expand Up @@ -275,9 +282,13 @@ export class GraphQLSitemapService {
after,
});

results = results.concat(fetchResponse?.site?.siteInfo?.routes?.results);
hasNext = fetchResponse.site.siteInfo.routes.pageInfo.hasNext;
after = fetchResponse.site.siteInfo.routes.pageInfo.endCursor;
if (!fetchResponse?.site?.siteInfo) {
throw new RangeError(getSiteEmptyError(this.options.siteName));
} else {
results = results.concat(fetchResponse.site.siteInfo.routes?.results);
hasNext = fetchResponse.site.siteInfo.routes?.pageInfo.hasNext;
after = fetchResponse.site.siteInfo.routes?.pageInfo.endCursor;
}
}
return results;
}
Expand Down