fix(dx): make gatsby id static for hot reloading (#44542)

* fix: make gatsby id static for hot reloading

* fix: catch duplication of challenge.id

* fix: handle CertificateNodes

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Tom
2022-01-10 03:03:20 -06:00
committed by GitHub
parent 6ce1c12bfa
commit 4e59659a00
2 changed files with 25 additions and 5 deletions

View File

@ -1,7 +1,13 @@
const crypto = require('crypto'); const crypto = require('crypto');
const { blockNameify } = require('../../../utils/block-nameify'); const { blockNameify } = require('../../../utils/block-nameify');
function createChallengeNode(challenge, reporter) { const createdIds = new Set();
function createChallengeNode(
challenge,
reporter,
{ isReloading } = { isReloading: false }
) {
// challengeType 11 is for video challenges (they only have instructions) // challengeType 11 is for video challenges (they only have instructions)
// challengeType 7 is for certificates (they only have tests) // challengeType 7 is for certificates (they only have tests)
// challengeType 12 is for CodeAlly/CodeRoad challenge // challengeType 12 is for CodeAlly/CodeRoad challenge
@ -43,6 +49,18 @@ function createChallengeNode(challenge, reporter) {
}; };
} }
// Challenge id should be unique for CertificateNodes, but not for
// ChallengeNodes
const id =
internal.type === 'ChallengeNode' ? challenge.fields.slug : challenge.id;
if (createdIds.has(id) && !isReloading) {
throw Error(`
Challenge slugs must be unique, but ${id} already exists.
`);
}
createdIds.add(id);
return JSON.parse( return JSON.parse(
JSON.stringify( JSON.stringify(
Object.assign( Object.assign(
@ -54,7 +72,9 @@ function createChallengeNode(challenge, reporter) {
sourceInstanceName: 'challenge' sourceInstanceName: 'challenge'
}, },
{ challenge }, { challenge },
{ id: crypto.randomUUID() } {
id
}
) )
) )
); );

View File

@ -45,7 +45,7 @@ exports.sourceNodes = function sourceChallengesSourceNodes(
File changed at ${filePath}, replacing challengeNode id ${challenge.id} File changed at ${filePath}, replacing challengeNode id ${challenge.id}
` `
); );
createVisibleChallenge(challenge); createVisibleChallenge(challenge, { isReloading: true });
}) })
.catch(e => .catch(e =>
reporter.error(`fcc-replace-challenge reporter.error(`fcc-replace-challenge
@ -111,8 +111,8 @@ ${e.message}
}); });
} }
function createVisibleChallenge(challenge) { function createVisibleChallenge(challenge, options) {
createNode(createChallengeNode(challenge, reporter)); createNode(createChallengeNode(challenge, reporter, options));
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {