Simorgh
The BBC's Open Source Web Application. Contributions welcome! Used on some of our biggest websites, e.g.
Install / Use
/learn @bbc/SimorghREADME
Simorgh
BBC World Service News websites are rendered using Simorgh, a ReactJS based application. Simorgh also renders AMP news article pages for World Service and Public Service News.
Simorgh provides a fast and accessible web experience used by millions of people around the world each month (see list of websites using Simorgh). It is regularly maintained and well documented, and we welcome open source contributors.
Simorgh is primarily maintained by the BBC News Web Engineering teams. It delivers highly trusted news to readers all over the world, currently in (41 languages). We support a wide range of devices and care deeply about scale, performance, and accessibility. We work in agile, flexible teams, and have an exciting roadmap for future development.
Documentation index
Please familiarise yourself with our:
- Code of conduct
- Coding Standards
- Contributing guidelines
- Guide to Code Reviews
- GPG Signing Guide
- Primary README (you are here)
- Recommended Tools
- Troubleshooting
NB there is further documentation colocated with relevant code. The above list is an index of the top-level documentation of our repo.
Simorgh Overview
A High Level User Journey
The initial page load - Server Side Render (SSR)
A request to a BBC home page (for example https://www.bbc.com/mundo) is passed on to the Simorgh application from a proprietary routing and caching service (called Mozart).
The request matches a route in our express server using a regex match (homePagePath). If the URL matches the pre-defined regex pattern for a home page we fetch some params from the route using the getRouteProps function. This returns the service, isAmp, route and match properties. Route is a react-router route that defines a method to fetch the initial JSON used to render the page and the React container in which to render i.e. HomePage, this is typically called getInitialData
Once data is returned we pull the status code and pass all of this data as props to our main document using renderDocument.
The Document passes the URL, JSON data, BBC Origin, isAmp and the service to the main App container and the result is rendered to a string using reacts own renderToString method. This string is then passed to DocumentComponent as the main app along with the assets array, style tags (the output from styled components) and any scripts/links that need to be added to the head. This is then rendered to static HTML markup using reacts own renderToStaticMarkup and sent back to the user as static HTML. Included in this response are links to our JS bundles which a users device will download to bootstrap the single page application (SPA) for subsequent journeys.
Now that the raw HTML has been downloaded, the client-side JS file kicks in and hydrates the initial response with the client side application. During this process react uses the initial JSON payload (available on the global window object SIMORGH_DATA) to hydrate the original markup returned by ReactDOMServer. React expects that the rendered content is identical between the server and the client (This is why we send the initial JSON payload with the SSR page, so the hydration phase runs with the same data that the server render used).
Rendering a Page
The JSON payload for an article consists of a number of Blocks. Each block is an object which represents an element on the page, this could be a Heading, an Image, a Paragraph etc. Each of these blocks has a block type and a block type will match up to a specific container in Simorgh e.g. blockType: image will match to the Image container.
The ArticleMain container will iterate over each JSON block, match it against its corresponding react container and pass the data via props. These containers are where the logic for rendering each block type sits. It is at this point where we use the installed frontend components from the Psammead component library. For example the Image container will import the Figure container, and Figure will import and use the psammead-image and the psammead-image-placeholder components. An image on an article will generally have a caption, so the Figure container will import the caption container which may include more frontend components from Psammead to render a caption on top of the image.
This process is repeated for each block within an article, ultimately rendering the main body of a news article using a combination of React containers for the business logic and React components for the frontend markup.
A Page Render Lifecycle
Each render is passed through a set of HOC's (Higher Order Components) to enhance the page, these HOC's are;
- withVariant
- withContexts
- withPageWrapper
- withError
- withData
- withHashChangeHandler
With a selection of page types passed through withOptimizelyProvider, that enables usage of Optimizely in the selected page types.
withVariant
The variant HOC ensures that services that have variants (e.g. simp, lat) always redirects to a url that renders the appropriate variant.
If a user navigates to a url without providing the variant, and variant is set in cookie, the cookie variant page is rendered. Otherwise, the default variant page is rendered
If a user navigates to a url with a variant, and variant is set in cookie, the cookie variant page is rendered. Otherwise, the requested variant page is rendered.
withContexts
The withContexts HOC is a wrapper that provides access to the different context providers available in the application. Any child component inside of these context providers has access to the context data via the use hook.
withPageWrapper
The page wrapper HOC simply wraps the Article containers with a layout, at present we only have a single page layout. This layout includes the header, footer and context providers rendering the main body as a child between the header and the footer.
withError
The error HOC checks the error prop passed in, if error is set to null the Article container is simply returned.
If error is set to true the Error component is returned, giving the user a visual indication of the error e.g. a 500 error page.
withData
Assuming the other HOC's have returned the original Article container the data HOC will run some validation checks on the JSON data passed in via the data prop. If all of the checks are satisfied the ArticleContainer will be returned with a single pageData prop. This pageData props will house the JSON data to be rendered e.g. the Optimo blocks for a given article.
withHashChangeHandler
The withHashChangeHandler HOC is a wrapper applied to all pages that checks for changes to the URL hash value. Pages include accessibility controls to skip content should the user choose to do so, this utilises the URL hash to skip users to specific areas of the page. Due to the nature of the client side routing, changes to the URL results in a re-render. This causes some unsightly UI flickering for some components, specifically media and social embeds. This HOC applies checks to the URL so see if a re-render is necessary, or if not preventing a re-render using React.memo.
withOptimizelyProvider
The withOptimizelyProvider HOC returns components that have been enhanced with access to an Optimizely client, that is used to run our A/B testing. This is done to limit bundle sizes, as we seperate some of our bundles by page type, that means if we're only running A/B testing on certain page types, we can prevent polluting page type bundles with the weight of the SDK library we use for Optimizely.
withOptimizelyProvider should be added as the value of the handlerBeforeContexts object key within applyBasicPageHandlers.js, as the ckns_mvt is set within the UserContext, so the withOptimizelyProvider HOC needs to be applied in the correct order alongside the withContexts HOC. This makes the ckns_mvt available on first time visits to pass into the OptimizelyProvider, along with attributes such as service, which is used for determining when Optimizely should enable an experiment.
Example for Article page:
import withOptimizelyProvider from '#app/legacy/containers/PageHandlers/withOptimizelyProvider';
import ArticlePage from './ArticlePage';
import applyBasicPageHandlers from '../utils/applyBasicPageHandlers';
export default applyBasicPageHandlers(ArticlePage, {
handlerBeforeContexts: withOptimizelyProvider,
});
Adding a new Page type
When adding a new page type there are several parts required.
1) Fixture data should be added to /data/{{service}}/{{pageType}}/
- This should be done for each service using the page type.
- [Fixture data example](https://gi
