quilt_rails 1.11.1 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0375ff704f3b5de3a726c3483e1aee2808871d0979a6e4a35f03d490ad5f9c47
4
- data.tar.gz: 6ccb28ab1d0ac38f149922668ef43acfa5c43811a2b4c683772d28243614d6e9
3
+ metadata.gz: 801ba2d2d619ae8c7a6714bf69f06a8edeb2ae7edc1d0255f8e9df57755be211
4
+ data.tar.gz: bc17c1c983af45c573a589c1c9ae84d241b1af242a996d33998aa360ef560ae7
5
5
  SHA512:
6
- metadata.gz: 6999712f3a0bab58d7e824f2d9caf3570385e613cf77dc7adcc24eda499464a923bcfdbe9fb5caa64c2647448e1d49c89e4e932a6e49d2479cbbc7a6ff42ab0a
7
- data.tar.gz: 60013d5daf244057e5edf7c21f1fdadc8eefb5ff966fba8754077ae07af0a228f6aa5ea70f962f0af80044e20729af5d2a6ed8e41ec70039520c80d44716e245
6
+ metadata.gz: a3f14ae99c0e106a9b3a71606c7489c20c99446a91e8ddfe20ed7b294bab50a358927cb6f6e70ed295b814fe7b520039e7f14dcb68cb1a2d7395b934f92f3c3d
7
+ data.tar.gz: 21b493fecf60259353c8df068c219d51de3eb3859cf4889bc8f44b6c4ea8f2993c6e08d1f58dad0585bf98d9f10128a4001e651528afb779d3008f5b8ca902b1
data/README.md CHANGED
@@ -11,6 +11,7 @@ A turn-key solution for integrating Quilt client-side libraries into your Rails
11
11
  - [Generate Quilt boilerplate](#generate-quilt-boilerplate)
12
12
  - [Try it out](#try-it-out)
13
13
  - [Manual Install](#manual-installation)
14
+ - [Generate Rails boilerplate](#generate-rails-boilerplate)
14
15
  - [Install Dependencies](#install-dependencies)
15
16
  - [Setup the Rails app](#setup-the-rails-app)
16
17
  - [Add JavaScript](#add-javascript)
@@ -21,6 +22,7 @@ A turn-key solution for integrating Quilt client-side libraries into your Rails
21
22
  - [Interacting with the request and response in React code](#interacting-with-the-request-and-response-in-react-code)
22
23
  - [Dealing with isomorphic state](#dealing-with-isomorphic-state)
23
24
  - [Customizing the node server](#customizing-the-node-server)
25
+ - [Fixing rejected CSRF tokens for new user sessions](#fixing-rejected-csrf-tokens-for-new-user-sessions)
24
26
  - [Performance tracking a React app](#performance-tracking-a-react-app)
25
27
  - [Install dependencies](#install-dependencies)
26
28
  - [Setup an endpoint for performance reports](setup-an-endpoint-for-performance-reports)
@@ -80,7 +82,10 @@ This will install the Node dependencies, provide a basic React app (in TypeScrip
80
82
 
81
83
  #### Try it out
82
84
 
83
- `dev server`
85
+ ```sh
86
+ dev up
87
+ dev server
88
+ ```
84
89
 
85
90
  Will run the application, starting up both servers and compiling assets.
86
91
 
@@ -88,6 +93,8 @@ Will run the application, starting up both servers and compiling assets.
88
93
 
89
94
  An application can also be setup manually using the following steps.
90
95
 
96
+ [Generate Rails boilerplate](#generate-rails-boilerplate)
97
+
91
98
  #### Install dependencies
92
99
 
93
100
  ```sh
@@ -100,6 +107,25 @@ yarn add @shopify/sewing-kit @shopify/react-server
100
107
  # Add React
101
108
  yarn add react react-dom
102
109
 
110
+ # Add Typescript
111
+ yarn add typescript @types/react @types/react-dom
112
+ ```
113
+
114
+ ##### Define typescript config
115
+
116
+ ```json
117
+ // tsconfig.json
118
+ {
119
+ "extends": "@shopify/typescript-configs/application.json",
120
+ "compilerOptions": {
121
+ "baseUrl": ".",
122
+ "rootDir": "."
123
+ },
124
+ "include": ["app/ui"]
125
+ }
126
+ ```
127
+
128
+ ```sh
103
129
  yarn
104
130
  dev up
105
131
  ```
@@ -303,67 +329,45 @@ function App() {
303
329
  export default App;
304
330
  ```
305
331
 
306
- ##### Example: sending headers from Rails controller
332
+ ##### Example: sending custom data from Rails controller
333
+
334
+ In some cases you may want to send custom headers or basic data from Rails to your React server. Quilt facilitates this case by providing consumers with a `headers` and `data` argument on the `render_react` call.
335
+
336
+ **Note:** The data passed should be data that is unlikely or will never change over the course of the session before they render any React components.
307
337
 
308
338
  ```ruby
309
339
  class ReactController < ApplicationController
310
340
  include Quilt::ReactRenderable
311
341
 
312
342
  def index
313
- render_react(headers: { 'x-custom-header': 'header-value-a' })
343
+ render_react(headers: {'x-custom-header': 'header-value-a'}, data: {'some_id': 123})
314
344
  end
315
345
  end
316
346
  ```
317
347
 
318
- You will need to serialize the result of the useRequestHeader hook in order for it to persist to the client
319
-
320
- ```tsx
321
- // app/ui/foundation/CustomUniversalProvider.tsx
322
- import {createContext} from 'react';
323
- import {createUniversalProvider} from '@shopify/react-universal-provider';
324
-
325
- export const CustomContext = createContext<string | null>(null);
326
- export const CustomUniversalProvider = createUniversalProvider('custom-key', CustomContext);
327
- ```
348
+ The React server will serialize the provided quilt data using `x-quilt-data` as the ID. You can then get this serialized data on the client with `getSerialized` from `@shopify/react-html`.
328
349
 
329
350
  ```tsx
330
351
  // app/ui/index.tsx
331
352
 
332
353
  import React from 'react';
333
- import {useRequestHeader} from '@shopify/react-network';
334
- import {CustomUniversalProvider} from './foundation/CustomUniversalProvider';
335
- import {ComponentWithCustomHeader} from './components/ComponentWithCustomHeader';
354
+ import {getSerialized} from '@shopify/react-html';
355
+
356
+ const IS_CLIENT = typeof window !== 'undefined';
336
357
 
337
358
  function App() {
338
- // get `x-custom-header` from the request that was sent through Rails ReactController
339
- const customHeader = useRequestHeader('x-custom-header');
359
+ // get `x-quilt-data` from the request that was sent through Rails ReactController
360
+ const quiltData = IS_CLIENT ? getSerialized<{[key: string]: any}>('x-quilt-data') : null;
340
361
 
341
- return (
342
- <CustomUniversalProvider value={customHeader}>
343
- <h1>My application ❤️</h1>
344
- <ComponentWithCustomHeader />
345
- </CustomUniversalProvider>
346
- );
362
+ // Logs {"x-custom-header":"header-value-a","some_id":123}
363
+ console.log(quiltData);
364
+
365
+ return <h1>Data: {quiltData}</h1>;
347
366
  }
348
367
 
349
368
  export default App;
350
369
  ```
351
370
 
352
- ```tsx
353
- // app/ui/components/ComponentWithCustomHeader.tsx
354
-
355
- import React, {useContext} from 'react';
356
- import {CustomContext} from '../foundation/CustomUniversalProvider';
357
-
358
- export function ComponentWithCustomHeader() {
359
- // get `x-custom-header` from serialized context
360
- // will be 'header-value-a' in this example
361
- const customHeader = useContext(CustomContext);
362
-
363
- return <span>{customHeader}</span>;
364
- }
365
- ```
366
-
367
371
  ##### Example: redirecting
368
372
 
369
373
  ```tsx
@@ -424,7 +428,7 @@ class GraphqlController < ApplicationController
424
428
  end
425
429
  ```
426
430
 
427
- If your API **does** require session data, you may can use follow the following steps:
431
+ If your API **does** require session data, you can follow these steps:
428
432
 
429
433
  - Add an `x-shopify-react-xhr` header to all GraphQL requests with a value of 1 (this is done automatically if you are using `@shopify/react-graphql-universal-provider`)
430
434
  - Add a `protect_from_forgery with: Quilt::HeaderCsrfStrategy` override to your controllers
@@ -445,8 +449,6 @@ class GraphqlController < ApplicationController
445
449
  end
446
450
  ```
447
451
 
448
- -
449
-
450
452
  ## Performance tracking a React app
451
453
 
452
454
  Using [`Quilt::Performance::Reportable`](#performanceReportable) and [@shopify/react-performance](https://www.npmjs.com/package/@shopify/react-performance) it's easy to add performance tracking to apps using[`sewing_kit`](https://github.com/Shopify/sewing-kit/tree/master/gems/sewing_kit#sewing_kit-) for client-side-rendering or `quilt_rails` for server-side-rendering.
@@ -6,29 +6,29 @@ module Quilt
6
6
  module ReactRenderable
7
7
  include ReverseProxy::Controller
8
8
 
9
- def render_react(headers: {})
9
+ def render_react(headers: {}, data: {})
10
10
  raise DoNotIntegrationTestError if Rails.env.test?
11
11
 
12
12
  # Allow concurrent loading to prevent this thread from blocking class
13
13
  # loading in controllers called by the Node server.
14
14
  ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
15
- call_proxy(headers)
15
+ call_proxy(headers, data)
16
16
  end
17
17
  end
18
18
 
19
19
  private
20
20
 
21
- def call_proxy(headers)
21
+ def call_proxy(headers, data)
22
22
  if defined? ShopifySecurityBase
23
23
  ShopifySecurityBase::HTTPHostRestriction.whitelist([Quilt.configuration.react_server_host]) do
24
- proxy(headers)
24
+ proxy(headers, data)
25
25
  end
26
26
  else
27
- proxy(headers)
27
+ proxy(headers, data)
28
28
  end
29
29
  end
30
30
 
31
- def proxy(headers)
31
+ def proxy(headers, data)
32
32
  url = "#{Quilt.configuration.react_server_protocol}://#{Quilt.configuration.react_server_host}"
33
33
  Quilt::Logger.log("[ReactRenderable] proxying to React server at #{url}")
34
34
 
@@ -37,7 +37,11 @@ module Quilt
37
37
  end
38
38
 
39
39
  begin
40
- reverse_proxy(url, headers: headers.merge('X-CSRF-Token': form_authenticity_token)) do |callbacks|
40
+
41
+ reverse_proxy(
42
+ url,
43
+ headers: headers.merge('X-CSRF-Token': form_authenticity_token, 'X-Quilt-Data': headers.merge(data).to_json)
44
+ ) do |callbacks|
41
45
  callbacks.on_response do |status_code, _response|
42
46
  Quilt::Logger.log("[ReactRenderable] #{url} returned #{status_code}")
43
47
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Quilt
3
- VERSION = "1.11.1"
3
+ VERSION = "1.12.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quilt_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.1
4
+ version: 1.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathew Allen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-24 00:00:00.000000000 Z
11
+ date: 2020-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties