Skip to content

Commit 04da9b1

Browse files
authored
Merge pull request #2701 from codecrafters-io/fix-pre-rendering-tracks
Fix fetching languages when pre-rendering tracks
2 parents 3725717 + e2d6319 commit 04da9b1

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

app/adapters/application.ts

+14
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import JSONAPIAdapter from '@ember-data/adapter/json-api';
22
import { inject as service } from '@ember/service';
33
import config from 'codecrafters-frontend/config/environment';
44
import { posthog } from 'posthog-js';
5+
import type FastbootService from 'ember-cli-fastboot/services/fastboot';
56
import type SessionTokenStorageService from 'codecrafters-frontend/services/session-token-storage';
67
import type VersionTrackerService from 'codecrafters-frontend/services/version-tracker';
78

89
export default class ApplicationAdapter extends JSONAPIAdapter {
910
namespace = 'api/v1';
1011

12+
@service declare fastboot: FastbootService;
1113
@service declare sessionTokenStorage: SessionTokenStorageService;
1214
@service declare versionTracker: VersionTrackerService;
1315

@@ -34,4 +36,16 @@ export default class ApplicationAdapter extends JSONAPIAdapter {
3436
get host() {
3537
return config.x.backendUrl;
3638
}
39+
40+
shouldBackgroundReloadAll() {
41+
// Don't use background reloading under FastBoot, otherwise it will run
42+
// after the app is destroyed and crash the build in a very funny way.
43+
return !this.fastboot.isFastBoot;
44+
}
45+
46+
shouldBackgroundReloadRecord() {
47+
// Don't use background reloading under FastBoot, otherwise it will run
48+
// after the app is destroyed and crash the build in a very funny way.
49+
return !this.fastboot.isFastBoot;
50+
}
3751
}

app/routes/track.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import config from 'codecrafters-frontend/config/environment';
55
import scrollToTop from 'codecrafters-frontend/utils/scroll-to-top';
66
import type AuthenticatorService from 'codecrafters-frontend/services/authenticator';
77
import type CourseModel from 'codecrafters-frontend/models/course';
8-
import type FastbootService from 'ember-cli-fastboot/services/fastboot';
98
import type LanguageModel from 'codecrafters-frontend/models/language';
109
import type MetaDataService from 'codecrafters-frontend/services/meta-data';
1110
import type Store from '@ember-data/store';
@@ -20,7 +19,6 @@ export default class TrackRoute extends BaseRoute {
2019
allowsAnonymousAccess = true;
2120

2221
@service declare authenticator: AuthenticatorService;
23-
@service declare fastboot: FastbootService;
2422
@service declare metaData: MetaDataService;
2523
@service declare store: Store;
2624

@@ -49,12 +47,9 @@ export default class TrackRoute extends BaseRoute {
4947
include: 'extensions,stages,language-configurations.language',
5048
})) as unknown as CourseModel[];
5149

52-
// TODO: Investigate why running this in FastBoot causes a build error
53-
if (!this.fastboot.isFastBoot) {
54-
(await this.store.findAll('language', {
55-
include: 'primer-concept-group,primer-concept-group.author,primer-concept-group.concepts,primer-concept-group.concepts.author',
56-
})) as unknown as LanguageModel[];
57-
}
50+
(await this.store.findAll('language', {
51+
include: 'primer-concept-group,primer-concept-group.author,primer-concept-group.concepts,primer-concept-group.concepts.author',
52+
})) as unknown as LanguageModel[];
5853

5954
const language = this.store.peekAll('language').find((language) => language.slug === params.track_slug)!;
6055

app/serializers/application.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ export const SERIALIZER_SHOEBOX_IDENTIFIER = 'application-serializer-data';
66
export default class ApplicationSerializer extends JSONAPISerializer {
77
@service fastboot;
88

9-
normalize(...args) {
9+
normalize(typeClass, hash) {
1010
if (this.fastboot.isFastBoot) {
11-
// Get the record's original resorce hash that was passed in arguments
12-
const [, resourceHash] = args;
11+
// Throw human-readable error if FastBoot service is destroyed instead of "document is undefined"
12+
if (this.fastboot.isDestroyed) {
13+
throw new Error(`Attempted to write a record into shoebox using a destroyed FastBoot service: ${hash.type}`);
14+
}
1315

1416
// Retrieve existing records from shoebox cache
1517
const shoeboxRecords = this.fastboot.shoebox.retrieve(SERIALIZER_SHOEBOX_IDENTIFIER) || [];
1618

1719
// Update shoebox cache, adding the new record
18-
this.fastboot.shoebox.put(SERIALIZER_SHOEBOX_IDENTIFIER, [...shoeboxRecords, resourceHash]);
20+
this.fastboot.shoebox.put(SERIALIZER_SHOEBOX_IDENTIFIER, [...shoeboxRecords, hash]);
1921
}
2022

21-
return super.normalize(...args);
23+
return super.normalize(typeClass, hash);
2224
}
2325

2426
normalizeQueryRecordResponse(store, primaryModelClass, payload, id, requestType) {

0 commit comments

Comments
 (0)