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>
pull/44738/head
Tom 2022-01-10 03:03:20 -06:00 committed by GitHub
parent 6ce1c12bfa
commit 4e59659a00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View File

@ -1,7 +1,13 @@
const crypto = require('crypto');
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 7 is for certificates (they only have tests)
// 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(
JSON.stringify(
Object.assign(
@ -54,7 +72,9 @@ function createChallengeNode(challenge, reporter) {
sourceInstanceName: '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}
`
);
createVisibleChallenge(challenge);
createVisibleChallenge(challenge, { isReloading: true });
})
.catch(e =>
reporter.error(`fcc-replace-challenge
@ -111,8 +111,8 @@ ${e.message}
});
}
function createVisibleChallenge(challenge) {
createNode(createChallengeNode(challenge, reporter));
function createVisibleChallenge(challenge, options) {
createNode(createChallengeNode(challenge, reporter, options));
}
return new Promise((resolve, reject) => {