9.4.1 • Published 2 days ago

@financial-times/dotcom-server-react-jsx v9.4.1

Weekly downloads
145
License
MIT
Repository
-
Last release
2 days ago

@financial-times/dotcom-server-react-jsx

This package provides server-side rendering for React components. It is primarily designed to be used during the transition between n-handlebars and JSX rendering.

Getting started

This package is compatible with Node 12+ and is distributed on npm.

npm install --save-dev @financial-times/dotcom-server-react-jsx

It is best used within an Express application but can also be used as a standalone library.

Usage with Express

After installing the package you must register it as a view engine for your Express application. This will enable you to render template files with the matching file extension and send the result as a response to requests.

Please note that you will need to extend Node's require() function to enable the use of .jsx files at runtime. See using JSX at runtime.

const express = require('express')
+ const { PageKitReactJSX } = require('@financial-times/dotcom-server-react-jsx')

+ const renderer = new PageKitReactJSX(options)
+ app.engine('.jsx', renderer.engine)

When using this module as a view engine Express will find the component file, decorate any data passed to it with properties from app.locals and response.locals, and automatically send the rendered result. See the Express render documentation for more information.

app.get('/', (request, response) => {
  const data = {
    pageTitle: 'Home',
    content: 'Hello World!'
  }

  response.render('Home.jsx', data)
})

Please note that where to lookup template files can be configured using Express's settings and component files must have a default export.

Standalone usage

This module can be used without integrating it fully into your application. This may be suitable for applications which are not built with Express or for ad-hoc template rendering needs. This is intended to provide some convenient extra functionality over React's built-in render methods.

+ const { PageKitReactJSX } = require('@financial-times/dotcom-server-react-jsx')
+ const renderer = new PageKitReactJSX(options)

When using this module as a standalone library you will need to find template files, provide all data, and handle the rendered output manually.

const Home = require('../views/Home.jsx')

app.get('/', async (request, response, next) => {
  const data = {
    pageTitle: 'Home',
    content: 'Hello World!'
  }

  try {
    const html = await renderer.render(Home, data, true)
    response.send(html)
  } catch (error) {
    next(error)
  }
})

API

render(component, templateContext[, includeDoctype])

Renders the given component to a string.

If the component has a .getInitialProps() method then this method will be called with the value of the templateContext argument and the resolved value of this method passed to the component as props. If the component does not have this method then the value of templateContext will be passed directly to the component instead.

If includeDoctype is true then the output will be prefixed with the HTML document pragma.

renderView(templatePath: string, templateContext: any, callback: (error, html) => void)

This method is intended to be used as a view engine for Express.

Options

useStaticRendering

Directs all methods to use ReactDOMServer.renderToStaticMarkup() instead of ReactDOMServer.renderToString(). This should be enabled if you are rendering static pages and are not using React on the client. Defaults to false.

Using JSX at runtime

By default Node cannot parse files which contain JSX syntax because it is extension of JavaScript rather than a feature of the language. However, it is widely supported by JavaScript parsers and transpilers.

There are two options for using files which include JSX code:

  1. Transpile the code and write the plain JS output to a new file
  2. Transpile the code on-the-fly and keep the plain JS output in memory

Option 1 is the best choice if you intend to distribute your code. For example if you publish your code to npm then this should always be in plain JS so that consumers of your code do not need to transpile it themselves.

Option 2 is a good choice for code used only in your application. If the code is transpiled efficiently then this should not add any extra overhead but it may cause start-up times to increase. If you are using a template library such as Pug, Handlebars, or EJS you may be using a similar technique already!

Whichever option you choose you will need to use a transpiler. Some popular options are Babel, Bublé, and Sucrase:

  • Babel is the most popular transpiler and is the most capable. It has support for the most recent and upcoming JS standards and syntax extensions and can output ES3 code. It has options to add relevant polyfills for features not supported by your target environment and tries to produce spec-compliant code. However, Babel can require significant configuration and has many dependencies. This is usually the best option for transpiling code targeting the browser.
  • Bublé can transpile ES2015-16 syntax and output ES5 code. It does not attempt to polyfill features and favours speed and simplicity over spec-compliance. It supports JSX out of the box and is simple to configure. Bublé is a good choice for projects requiring minimal transformations.
  • Sucrase is capable of transpiling the latest JS features and syntax extensions to ES2015 code, including JSX. It is fast and lightweight so it is suitable for using in server-side projects.
9.4.1

2 days ago

9.4.0

23 days ago

9.3.4

3 months ago

8.2.6

3 months ago

9.3.3

3 months ago

9.3.2

4 months ago

9.3.1

4 months ago

9.3.0

4 months ago

9.2.1

4 months ago

9.2.0

4 months ago

9.1.1

4 months ago

9.1.0

5 months ago

9.0.3

5 months ago

8.2.3

7 months ago

8.2.2

7 months ago

8.2.5

7 months ago

8.2.4

7 months ago

9.0.0-beta.5

9 months ago

9.0.0-beta.4

9 months ago

9.0.0-beta.3

9 months ago

9.0.0-beta.2

9 months ago

9.0.0-beta.1

9 months ago

9.0.0-beta.9

8 months ago

9.0.0-beta.8

8 months ago

9.0.0-beta.7

8 months ago

9.0.0-beta.6

8 months ago

8.2.1

8 months ago

8.2.0

8 months ago

9.0.2

6 months ago

9.0.1

7 months ago

9.0.0-beta.15

7 months ago

9.0.0-beta.16

7 months ago

9.0.0-beta.13

8 months ago

9.0.0-beta.14

8 months ago

9.0.0-beta.11

8 months ago

9.0.0-beta.12

8 months ago

9.0.0-beta.10

8 months ago

8.1.0

11 months ago

7.3.5

1 year ago

7.3.4

1 year ago

8.0.0

1 year ago

7.3.3

1 year ago

7.3.2

1 year ago

7.3.1

1 year ago

7.3.0

1 year ago

7.2.6

1 year ago

7.2.7

1 year ago

7.1.3

2 years ago

7.2.5

1 year ago

7.2.4

2 years ago

7.1.4

2 years ago

7.1.1

2 years ago

7.1.0

2 years ago

7.0.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.2

2 years ago

5.1.0

2 years ago

4.2.0

2 years ago

5.0.0

2 years ago

4.0.0

2 years ago

4.1.0

2 years ago

3.0.0

2 years ago

2.6.1

3 years ago

2.6.2

3 years ago

2.6.0

3 years ago

2.5.2

3 years ago

2.5.1

3 years ago

2.4.0

3 years ago

2.3.0

3 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.1

4 years ago

2.1.0-pre.1

4 years ago

2.1.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.6.5

4 years ago

0.7.1

4 years ago

0.7.0-beta.1

4 years ago

0.7.0-beta.2

4 years ago

0.7.0

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.8

4 years ago

0.5.7

4 years ago

0.5.6

4 years ago

0.5.5

4 years ago

0.4.5

4 years ago

0.5.4

4 years ago

0.4.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.5.0-beta.3

4 years ago

0.5.0-beta.1

4 years ago

0.5.0-beta.2

4 years ago

0.4.1

4 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.1-test.2

5 years ago

0.0.1-test.1

5 years ago