Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SE-4650] OEP-15 - add support for course-wide custom resources #581

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/courseware/data/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ function normalizeMetadata(metadata) {
specialExamsEnabledWaffleFlag: data.is_mfe_special_exams_enabled,
proctoredExamsEnabledWaffleFlag: data.is_mfe_proctored_exams_enabled,
isMasquerading: data.original_user_is_staff && !data.is_staff,
courseWideJs: data.course_wide_js,
courseWideCss: data.course_wide_css,
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/tab-page/LoadedTabPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ function LoadedTabPage({
celebrations,
canViewLegacyCourseware,
verifiedMode,
courseWideJs,
courseWideCss,
} = useModel(metadataModel, courseId);

// Logistration and enrollment alerts are only really used for the outline tab, but loaded here to put them above
Expand All @@ -46,6 +48,8 @@ function LoadedTabPage({
<>
<Helmet>
<title>{`${activeTab ? `${activeTab.title} | ` : ''}${title} | ${getConfig().SITE_NAME}`}</title>
{courseWideJs && courseWideJs.map(js => <script key={js} type="text/javascript" src={js} />)}
{courseWideCss && courseWideCss.map(css => <link key={css} rel="stylesheet" href={css} />)}
Comment on lines +51 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very leery of adding arbitrary css/js to the react side of things. The MFE is fast moving and doesn't want to have to make guarantees about DOM structure or anything like that.

For example, when we added the course-welcome and course-handouts features to the MFE, we "sandboxed" that arbitrary html/js inside iframes.

Can we do this asset loading inside the courseware iframe, to better isolate it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I'm sorry - I'm reading the part of the platform PR that says this is explicitly your point - you want them in the parent page too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm chatting with folks that are bigger frontend experts at edX - but this whole arbitrary JS approach is very much not where we are trying to heading with the MFEs. I'm trying to see what the vision for this kind of use case is.

And your use case is basically loading some external libraries that do page-wide stuff, not trying to manipulate any specific DOM structures, yeah? Because that would definitely be a problem, given how React works.

But maybe we can find a more future-proof way to inject external libraries...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkikix Glad to demo some of our use cases if it would help.

</Helmet>
<Header
courseOrg={org}
Expand Down
26 changes: 25 additions & 1 deletion src/tab-page/LoadedTabPage.test.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React from 'react';
import { Factory } from 'rosie';
import { initializeTestStore, render, screen } from '../setupTest';
import {
initializeTestStore,
render,
screen,
waitFor,
} from '../setupTest';
import LoadedTabPage from './LoadedTabPage';

jest.mock('../course-header/CourseTabsNavigation', () => () => <div data-testid="CourseTabsNavigation" />);
Expand Down Expand Up @@ -36,4 +41,23 @@ describe('Loaded Tab Page', () => {
render(<LoadedTabPage {...mockData} courseId={courseMetadata.id} />, { store: testStore });
expect(screen.getByTestId('StreakModal')).toBeInTheDocument();
});

it('adds course-wide scripts/styles to page', async () => {
const courseWideJs = ['https://testcdn.com/js/test.min.js', 'https://jquery.com/jquery.js'];
const courseWideCss = ['https://testcdn.com/js/test.min.css', 'https://jquery.com/jquery.css'];

const courseMetadata = Factory.build('courseMetadata', { course_wide_js: courseWideJs, course_wide_css: courseWideCss });
const testStore = await initializeTestStore({ courseMetadata }, false);
render(<LoadedTabPage {...mockData} courseId={courseMetadata.id} />, { store: testStore });

await waitFor(() => {
const scripts = Array.from(document.getElementsByTagName('script'));
const scriptUrls = scripts.filter(s => s.src).map(s => s.src.replace(/\/$/, ''));
const styles = Array.from(document.getElementsByTagName('link'));
const styleUrls = styles.filter(s => s.href).map(s => s.href.replace(/\/$/, ''));

courseWideJs.forEach(js => expect(scriptUrls).toContain(js));
courseWideCss.forEach(css => expect(styleUrls).toContain(css));
});
});
});