Skip to content

Commit

Permalink
Add Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkitkkr committed Jul 20, 2022
1 parent 9feb90c commit 59d5d8f
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.md
Expand Up @@ -49,12 +49,13 @@ To provide a high performance framework for integrating Ruby on Rails with React
Given that `rails/webpacker` gem already provides basic React integration, why would you use "React on Rails"?

1. Easy passing of props directly from your Rails view to your React components rather than having your Rails view load and then make a separate request to your API.
1. Tight integration with [shakapacker](https://github.com/shakacode/shakapacker) (or it's predecessor [rails/webpacker](https://github.com/rails/webpacker)).
1. Server-Side Rendering (SSR), often used for SEO crawler indexing and UX performance.
1. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/ReactTraining/react-router#readme) integration with server-side-rendering.
1. [Internationalization (I18n) and (localization)](https://www.shakacode.com/react-on-rails/docs/guides/i18n)
1. A supportive community. This [web search shows how live public sites are using React on Rails](https://publicwww.com/websites/%22react-on-rails%22++-undeveloped.com+depth%3Aall/).
1. [ReScript Support](https://github.com/shakacode/rescript-react-on-rails-example).
2. Tight integration with [shakapacker](https://github.com/shakacode/shakapacker) (or it's predecessor [rails/webpacker](https://github.com/rails/webpacker)).
3. Server-Side Rendering (SSR), often used for SEO crawler indexing and UX performance.
4. [File System Based Automated Bundle Generation for Components](https://www.shakacode.com/react-on-rails/docs/guides/file-system-based-automated-bundle-generation.md)
5. [Redux](https://github.com/reactjs/redux) and [React Router](https://github.com/ReactTraining/react-router#readme) integration with server-side-rendering.
6. [Internationalization (I18n) and (localization)](https://www.shakacode.com/react-on-rails/docs/guides/i18n)
7. A supportive community. This [web search shows how live public sites are using React on Rails](https://publicwww.com/websites/%22react-on-rails%22++-undeveloped.com+depth%3Aall/).
8. [ReScript Support](https://github.com/shakacode/rescript-react-on-rails-example).

See [Rails/Webpacker React Integration Options](https://www.shakacode.com/react-on-rails/docs/guides/rails-webpacker-react-integration-options) for comparisons to other gems.

Expand Down
116 changes: 116 additions & 0 deletions docs/guides/file-system-based-automated-bundle-generation.md
@@ -0,0 +1,116 @@
# File System Based Automated Bundle Generation

To use the automated bundle generation feature introduced in React on Rails v14.0.0, Please upgrade to use [Shakapacker v6.5.0](https://github.com/shakacode/shakapacker/tree/v6.5.0) at least. If you are currently using webpacker, please follow the migration steps available [here](https://github.com/shakacode/shakapacker/blob/master/docs/v6_upgrade.md).

## Configuration

### Enable nested_entries for Shakapacker
To use the automated bundle generation feature, In the `webpacker.yml` file, set

```yml
nested_entries: true
```

For more details, see [Configuration and Code ](https://github.com/shakacode/shakapacker#configuration-and-code) section in [shakapacker](https://github.com/shakacode/shakapacker/).

### Configure Components Directory
`components_directory` is the directory used to automatically detect and register components for usage on the rails view.
Configure `config/initializers/react_on_rails`
to set the name for `components_directory`.

```rb
config.components_directory = "ror_components"
```

Now all React components inside `config.components_directory` will automatically be registered for the usage with `react_component` & `react_component_hash` helper methods provided by React on Rails.

### Configure `auto_load_bundle` Option

For automated component registry, `react_component` and `react_component_hash` view helper method tries to load generated bundle for component from the generated directory automatically per `auto_load_bundle` option. `auto_load_bundle` option in `config/initializers/react_on_rails` configures the default value that will be passed to component helpers. The default is `false`, and the option can be passed at the time of individual usage too.

*Note, Starting from React om Rails version 15.0.0, the default value for `auto_load_bundle` option will be `true`.*

In order to update the default, configure `config/initializers/react_on_rails`.
```rb
config.auto_load_bundle = true
```

### Update `.gitignore` file
React on Rails, per the settings in your `config/webpacker.yml`, automatically generates pack files for components to be registered in the `packs/generated` directory. To avoid committing generated files into the version control system, Please update `.gitignore` to have

```gitignore
### Generated React on Rails packs
packs/generated
```

*Note, the directory might be different depending on the `source_entry_path` in `config/webpacker.yml`.*

## Usage

### Basic usage

Considering the `webpacker.yml` file is configured as instructed [here](https://github.com/shakacode/shakapacker#configuration-and-code), With following configurations

```yml
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
nested_entries: true
# And more
```

the directory structure will look like this
```
app/javascript:
└── packs: # sets up webpack entries
│ └── application.js # references ../src/my_component.js
│ └── application.css
└── src: # any directory name is fine. Referenced files need to be under source_path
│ └── component.js
└── stylesheets:
│ └── my_styles.css
└── images:
└── logo.svg
public/packs # webpack output
```

Now, To automatically register, `A_ComponentOne`, `B_ComponentOne`, `B_ComponentTwo` for the usage with `react_component` & `react_component_hash` helpers, Create directory structure as mentioned below:

```
app/javascript:
└── packs:
│ └── application.js
│ └── application.css
└── src:
│ └── A
│ │ └── ...
│ │ └── ror_components # configured as `components_directory`
│ │ └── A_ComponentOne.jsx
│ └── B
│ │ └── ror_components # configured as `components_directory`
│ │ │ └── B_ComponentOne.jsx
│ │ │ └── B_ComponentTwo.jsx
public/packs
```

To register react component, It is no longer needed to create a pack entry and manually register it by calling `ReactOnRails.register`. With automatically generated packs, you can directly use `A_ComponentOne`, `B_ComponentOne` and `B_ComponentTwo` in rails view using:

```erb
<%= react_component("A_ComponentOne", {}, auto_load_bundle: true) %>
<%= react_component("B_ComponentOne", {}, auto_load_bundle: true) %>
<%= react_component("B_ComponentTwo", {}, auto_load_bundle: true) %>
```

`auto_load_bundle` option can be omitted when, if `config/initializers/react_on_rails` is configured with `config.auto_load_bundle = true`.

### Server Rendering and Client Rendering Components

If server rendering is enabled, the component will be registered for the usage both on server and client rendering. In order to have separate definitions for client and server rendering, Name the components files as `Component_Name.server.jsx` and `Component_Name.client.jsx`. During the server rendering, the `Component_Name.server.jsx` definition will be used, and for client rendering, `Component_Name.client.jsx` file will be used.

### Using Automated Bundle Generation Feature with already defined packs

To use Automated Bundle Generation Feature with already defined packs, `config/initializers/react_on_rails` should explicitly be configured with `config.auto_load_bundle = false`, in order to avoid automated loading of component packs. For the components using Bundle Generation Feature, explicitly pass `auto_load_bundle` option in `react_component` and `react_component_hash`.


0 comments on commit 59d5d8f

Please sign in to comment.