Code Monkey home page Code Monkey logo

react-scripts-with-ssr's Introduction

react-scripts-with-ssr Build Status

This is a fork of react-scripts, which adds support for server-side rendering (SSR).

react-scripts is a part of Create React App, which can be used with a customized version of react-scripts.

Heads up!

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

This version of react-scripts-with-ssr is not able to create proper production build, because node env is set to development mode. All the configuration and requirement must be merged with the latest react-scripts from https://github.com/facebook/create-react-app/tree/master/packages/react-scripts

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Getting Started

Install create-react-app:

npm i -g create-react-app

Generate a new project (add --typescript for TypeScript support):

npx create-react-app my-app --scripts-version react-scripts-with-ssr
cd my-app

Start a local webpack dev server with integrated server-side rendering:

npm start

Build a production version and test server-side rendering locally:

npm run build
npm run serve

How it works

SSR entry point

The script will generate an additional entry point for server-side rendering in src/index.ssr.js (or src/index.ssr.tsx). It exports an express-style request handler, that looks like this:

export default (request, response) => {
  // ...
  response.status(200).send("...");
};

Webpack will compile this entry point as a separate library build/ssr.js. In production, that library may be imported by an executable node script, which sets up an express server and plugs in the request handler. Such a script is provided as an example at react-scripts-with-ssr/scripts/serve.js, which you may start with npm run serve to test your production builds. That script however is not part of the compilation. It is up to you to integrate the request handler into your server-side runtime environment.

During development, the request handler will be integrated into the local webpack dev server. Start it with npm start and open http://localhost:3000 to see the server-side rendered output.

If src/index.ssr.js exports a function called devServerHandler, it will be invoked and its return value will be used as a request handler during development instead. This gives your entry point the possibility to make environment-specific adjustments. For example, all assets are compiled in-memory during development and should not be served from the real file system.

Relative Urls, PUBLIC_URL and BASE_HREF

create-react-app provides a variable called PUBLIC_URL, which is accessible via process.env.PUBLIC_URL in JavaScript or by using the placeholder %PUBLIC_URL% in HTML to reference assets. This variable is determined at compile time and must be specified before compilation, which makes your compiled web app dependant on the specified location.

For projects with server-side rendering however, there is another way to deal with this. Instead of hardcoding the public url, HTML assets can be referenced with a relative url and the server-side renderer can read a specified base url at runtime and render it into a base tag. react-scripts-with-ssr supports using PUBLIC_URL but also allows the BASE_HREF approach:

  • Define a BASE_HREF environment variable in your server runtime environment containing your base url (e.g. https://example.com/folder).
  • Add <base href="%BASE_HREF%/" /> to your index.html template. Notice the trailing slash.
  • Replace %BASE_HREF% with the value of process.env.BASE_HREF in your server-side request handler.
  • Make all asset references in HTML relative. Do not use %PUBLIC_URL% in URLs or server-relative URLs (e.g. /some/url).
  • Make the build scripts emit relative paths by providing PUBLIC_URL as environment variable with the value . during compile time or setting the homepage field in package.json to ..
  • If you need access to BASE_HREF in JavaScript, read it from document.getElementsByTagName("base")[0].href;.

Environment variables

create-react-app embeds environment variables prefixed with REACT_APP in the build during compile time and provides them with a simulated process.env object using Webpack's DefinePlugin.

While embedding is necessary on the client-side, the ssr request handler runs in a Node.js context with real environment variables. react-scripts-with-ssr allows accessing external environment variables in the ssr request handler during runtime. However, all variables contained in the client-side build are also embedded into the ssr request handler as default values if they are not provided by the runtime environment.

If you want to make your server's runtime environment variables available on the client-side, you can do this with a clientEnv variable:

  • Add a variable assignment script <script>clientEnv=%CLIENT_ENV%</script> to the head section in public/index.html.
  • Replace the placeholder in the ssr request handler with .replace(/%CLIENT_ENV%/g, JSON.stringify({ FOO: process.env.FOO })).
  • Access the value on the client-side with clientEnv.FOO.
  • For TypeScript: Declare the global variable declare const clientEnv: NodeJS.ProcessEnv;.

You might also use this mechanism to transfer preloaded state to the client.

Contribute

Merge react-scripts updates

Clone create-react-app and create a subtree branch for react-scripts:

git clone [email protected]:facebook/create-react-app.git create-react-app
cd create-react-app
git checkout [email protected]
git subtree split -P packages/react-scripts -b react-scripts-v2.1.3

Merge the subtree branch into this project:

git remote add upstream ../create-react-app
git merge upstream react-scripts-v2.1.3

Sponsors

react-scripts-with-ssr's People

Contributors

andriijas avatar arcanis avatar bebbi avatar bondz avatar bradfordlemley avatar brunolemos avatar enoahnetzach avatar existentialism avatar frontsideair avatar fson avatar gaearon avatar gaumala avatar ianschmitz avatar iansu avatar jeffposnick avatar joernb avatar lpalmes avatar luftywiranda13 avatar mareksuscak avatar nhajidin avatar pd4d10 avatar ro-savage avatar ryansully avatar sidoshi avatar tharakawj avatar themre avatar timer avatar tuchk4 avatar viankakrisna avatar xjlim avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.