Sentry does not have a specific integration for Sitecore JSS. However, we can integrate a Sitecore JSS application with the front-end technology we choose, which could be Angular/React/.NET Core/Next.js etc. In this example, I will be integrating a Sitecore JSS with Next.js solution with Sentry.
npx @sentry/wizard@latest -i nextjs
For adding custom information to the logs, that are specific to Sitecore can be made as below:
Import Sentry
from @sentry/nextjs
.
import * as Sentry from "@sentry/nextjs";
Add the below lines to your [[…path]].tsx file, exactly before you return the component defintion.
Sentry.setContext("jss-sitecore-page", {
isEditing: isEditing,
renderingType: layoutData?.sitecore?.context?.renderingType,
dataBaseName: layoutData?.sitecore?.route?.databaseName,
site: layoutData?.sitecore?.context?.site,
language: layoutData?.sitecore?.context?.language,
path: layoutData?.sitecore?.route?.name,
});
// Add the above lines before the return statement
return (
<ComponentPropsContext value={componentProps}>
<SitecoreContext
componentFactory={isEditing ? editingComponentFactory : componentFactory}
layoutData={layoutData}
>
{/*
Sitecore Pages supports component rendering to avoid refreshing the entire page during component editing.
If you are using Experience Editor only, this logic can be removed, Layout can be left.
*/}
{isComponentRendering ? (
<EditingComponentPlaceholder rendering={layoutData.sitecore.route} />
) : (
<Layout layoutData={layoutData} />
)}
</SitecoreContext>
</ComponentPropsContext>
);
In your middleware, or in this example the normal-mode plugin. Add a try/catch block in the exec method. And, add the below lines:
try {
const path = extractPath(context.params);
const sitecoreContext = props?.layoutData?.sitecore?.context;
Sentry.addBreadcrumb({
category: "site_name",
message: sitecoreContext?.site?.name,
});
Sentry.configureScope((scope) => {
scope.setTag("page_mode", "normal");
scope.setTag(
"database",
props?.layoutData?.sitecore?.route?.databaseName,
);
scope.setTag("site", sitecoreContext?.site?.name);
scope.setTag("language", sitecoreContext?.language);
scope.setExtra("item_path", path);
scope.setExtra("item_id", props?.layoutData?.sitecore?.route?.itemId);
});
// Rest of your code here
} catch (err) {
Sentry.configureScope((scope) => {
scope.setTag("page_mode", "normal");
scope.setTag(
"database",
props?.layoutData?.sitecore?.route?.databaseName,
);
scope.setTag("site", props?.layoutData?.sitecore?.context?.site?.name);
scope.setTag("language", props?.layoutData?.sitecore?.context?.language);
scope.setExtra("item_path", extractPath(context?.params));
scope.setExtra("item_id", props?.layoutData?.sitecore?.route?.itemId);
});
Sentry.captureException(err);
return props;
}
I have also pushed the source code to my github repository: Sitecore.JSS.NextJs.Sentry