Skip to content

Commit c1bd91b

Browse files
committed
added some defenses against broken asset links, and using correct environment when creating contentful client
task: https://www.notion.so/stackbit/Sourcebit-Contentful-Sourcebit-crashes-when-Contentful-have-broken-assets-or-link-to-assets-c611ed56260242d2a456eb4142307a96
1 parent afd11dd commit c1bd91b

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
/node_modules
2+
/.idea

index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports.options = {
3939
module.exports.bootstrap = async ({ getPluginContext, options, refresh, setPluginContext }) => {
4040
const isPreview = options.preview !== undefined ? options.preview : options.watch;
4141
const host = options.host || (isPreview ? 'preview.contentful.com' : undefined);
42+
const environment = options.environment || 'master';
4243
const clientManagement = contentfulManagement.createClient({
4344
accessToken: options.accessToken
4445
});
@@ -63,7 +64,7 @@ module.exports.bootstrap = async ({ getPluginContext, options, refresh, setPlugi
6364
sys: {
6465
type: 'Link',
6566
linkType: 'Environment',
66-
id: options.environment
67+
id: environment
6768
}
6869
}
6970
]
@@ -76,7 +77,8 @@ module.exports.bootstrap = async ({ getPluginContext, options, refresh, setPlugi
7677
const client = contentful.createClient({
7778
accessToken,
7879
host,
79-
space: options.spaceId
80+
space: options.spaceId,
81+
environment: environment
8082
});
8183
const { assets, entries, nextSyncToken } = await syncWithRetry(client, {
8284
initial: true,

lib/contentful-util.js

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ function normalizeEntries({ contentTypes, entries, options }) {
66
}
77

88
function normalizeEntry({ contentTypes, entry, options }) {
9+
if (!entry) {
10+
return null;
11+
}
912
if (!entry.fields) {
1013
return entry;
1114
}
@@ -21,6 +24,9 @@ function normalizeEntry({ contentTypes, entry, options }) {
2124
let fields;
2225

2326
if (isAsset) {
27+
if (!entry.fields.file) {
28+
return null;
29+
}
2430
const file = Object.values(entry.fields.file)[0];
2531

2632
fields = {

0 commit comments

Comments
 (0)