|
| 1 | +frontend-base |
| 2 | +============= |
| 3 | + |
| 4 | +|Build Status| |Codecov| |NPM Version| |npm_downloads| |license| |
| 5 | +|semantic release| |
| 6 | + |
| 7 | +Overview |
| 8 | +-------- |
| 9 | + |
| 10 | +Foundational application management code for Open edX micro-frontend |
| 11 | +applications. |
| 12 | + |
| 13 | +``frontend-base`` provides Open edX micro-frontends with: |
| 14 | + |
| 15 | +- A standardized, yet customizable application initialization process. |
| 16 | +- Lifecycle event subscriptions for startup sequence extension. |
| 17 | +- Convenient access to environment configuration and application state |
| 18 | + data. |
| 19 | +- A React-based application data provider. |
| 20 | +- High-level logging of events and errors to promote observability. |
| 21 | +- Fallback error handling. |
| 22 | + |
| 23 | +With respect to the initialization process, ``frontend-base`` helps |
| 24 | +manage the vast majority of what we expect all Open edX micro-frontends |
| 25 | +to have by default, namely: |
| 26 | + |
| 27 | +- Environment configuration |
| 28 | +- Authentication / access to an authenticated API client |
| 29 | +- Analytics |
| 30 | +- Logging |
| 31 | +- Internationalization |
| 32 | + |
| 33 | +Running the example app and test suite |
| 34 | +-------------------------------------- |
| 35 | + |
| 36 | +This repo has an example app that demonstrates basic usage of the |
| 37 | +library. |
| 38 | + |
| 39 | +:: |
| 40 | + |
| 41 | + npm install # do this once |
| 42 | + npm start # starts the dev server |
| 43 | + |
| 44 | +Visit `http://localhost:8080/ <http://localhost:8080/>`__ in your |
| 45 | +browser. |
| 46 | + |
| 47 | +The jest test suite can be run with: |
| 48 | + |
| 49 | +:: |
| 50 | + |
| 51 | + npm test |
| 52 | + |
| 53 | +Getting Started |
| 54 | +--------------- |
| 55 | + |
| 56 | +Initialization is managed via the ``App`` singleton. |
| 57 | + |
| 58 | +The simplest initialization sequence looks like this: |
| 59 | + |
| 60 | +:: |
| 61 | + |
| 62 | + import { App, APP_READY } from '@edx/frontend-base'; |
| 63 | + |
| 64 | + import HelloWorld from './HelloWorld'; // Your application component |
| 65 | + |
| 66 | + App.subscribe(APP_READY, () => { |
| 67 | + ReactDOM.render( |
| 68 | + <HelloWorld /> |
| 69 | + ) |
| 70 | + }); |
| 71 | + |
| 72 | + App.initialize(); |
| 73 | + |
| 74 | +This initialization sequence will do the following: |
| 75 | + |
| 76 | +- Read in expected environment configuration variables and validate |
| 77 | + that they're not ``undefined`` |
| 78 | +- Configure NewRelicLoggingService for @edx/frontend-logging. |
| 79 | +- Verify that the user is logged in and authenticated, with a current |
| 80 | + JWT token. |
| 81 | +- Redirect the user to login if they're not authenticated. |
| 82 | +- Initialize i18n without any messages. |
| 83 | +- Configure analytics with Segment. |
| 84 | + |
| 85 | +Error handling |
| 86 | +~~~~~~~~~~~~~~ |
| 87 | + |
| 88 | +If an error occurs during the initialization sequence, the application |
| 89 | +will go into an error state and log the error to the logging service. |
| 90 | + |
| 91 | +You can hook into this state by subscribing to the ``APP_ERROR`` event. |
| 92 | +For convenience, ``frontend-base`` provides a simple ``<ErrorPage>`` |
| 93 | +component that can be used to display errors to the user of the app as a |
| 94 | +fallback error experience. This experience can be used as-is (shown |
| 95 | +below), or replaced with your own error page experience. |
| 96 | + |
| 97 | +:: |
| 98 | + |
| 99 | + App.subscribe(APP_ERROR, (error) => { |
| 100 | + ReactDOM.render(<ErrorPage />, document.getElementById('root')); |
| 101 | + }); |
| 102 | + |
| 103 | +Error handling after initialization (in React) |
| 104 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 105 | + |
| 106 | +It's recommended that in React applications you use `error |
| 107 | +boundaries <https://reactjs.org/docs/error-boundaries.html>`__ to catch |
| 108 | +run-time errors in your React components. |
| 109 | + |
| 110 | +``frontend-base`` provides an error boundary component which displays |
| 111 | +``<ErrorPage>`` in the event of an uncaught error in React. If you use |
| 112 | +``<AppProvider>`` then you'll get this behavior for free. |
| 113 | + |
| 114 | +If you need a custom error page, you can add your own error boundary |
| 115 | +inside ``<AppProvider>`` and the fallback handling will effectively be |
| 116 | +ignored (since errors won't bubble up to it anymore). |
| 117 | + |
| 118 | +Foundational React Components |
| 119 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 120 | + |
| 121 | +``frontend-base`` also provides several companion React components which |
| 122 | +pair with the App singleton to help bootstrap your React application. |
| 123 | +Please see the API documentation for ``<AppProvider>`` and |
| 124 | +``<AppContext>`` specifically; they're an important part of the |
| 125 | +frontend-base ecosystem. |
| 126 | + |
| 127 | +Additional Resources |
| 128 | +-------------------- |
| 129 | + |
| 130 | +- `API |
| 131 | + Reference <https://github.com/edx/frontend-base/blob/master/docs/API.rst>`__ |
| 132 | + |
| 133 | +.. |Build Status| image:: https://api.travis-ci.org/edx/frontend-base.svg?branch=master |
| 134 | + :target: https://travis-ci.org/edx/frontend-base |
| 135 | +.. |Codecov| image:: https://img.shields.io/codecov/c/github/edx/frontend-base |
| 136 | + :target: https://codecov.io/gh/edx/frontend-base |
| 137 | +.. |NPM Version| image:: https://img.shields.io/npm/v/@edx/frontend-base.svg |
| 138 | + :target: https://www.npmjs.com/package/@edx/frontend-base |
| 139 | +.. |npm_downloads| image:: https://img.shields.io/npm/dt/@edx/frontend-base.svg |
| 140 | + :target: https://www.npmjs.com/package/@edx/frontend-base |
| 141 | +.. |license| image:: https://img.shields.io/npm/l/@edx/frontend-base.svg |
| 142 | + :target: https://github.com/edx/frontend-base/blob/master/LICENSE |
| 143 | +.. |semantic release| image:: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg |
| 144 | + :target: https://github.com/semantic-release/semantic-release |
0 commit comments