quilt_rails 1.9.2 → 1.10.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: 19ed91c686fab67ad1642a071a8d62214c154c246cf4d8cb4027ac3a50483620
4
- data.tar.gz: 73ad8104d3e54a5b3ba56aa912fa7147eaeab671f75e310c5dd1dcb6d297471b
3
+ metadata.gz: f4580b1b1870a7fb2afa9bc64bab975e8ec3c7041f775de16356d31151ab4768
4
+ data.tar.gz: 8914278b57d9bb8bad32c33c2e9697289073fe80576248b08bfe60f0b6f2536d
5
5
  SHA512:
6
- metadata.gz: 10618fcc4252375f45e54b98ed366c47fa06de9541b57e748081dab0ba66d0817f78e38c4d3a8e23315f2402c79d4dde5c86c85132e3b7e97534a0165166fee0
7
- data.tar.gz: 838be176ef13ae501f40c2eb6763a7d0416d42ce6908650dd03ecfdcc6039dc35d7769518b60f977dda0d3fa81cffc10a7e6656beeda9e510c74e4c5587d0321
6
+ metadata.gz: e908b7d01e781f43ccf9c3a21d595aef8fffb612fe5da76d29f1cbef9a635ef5cbd8b8b29e6e9cbd8a9bdf170ac7fbabe7a4e0c8880a3bd81a90fde2d2da5a03
7
+ data.tar.gz: 66fe45a859a30dde3a31e76563cb6d5ef5c0d6621f9127cb1a41c270dde190b47227a5d31bcca81bdb26098e172a67e811d7c58c551a74fa8b7d6d8e0d1968bb
data/README.md CHANGED
@@ -300,6 +300,67 @@ function App() {
300
300
  export default App;
301
301
  ```
302
302
 
303
+ ##### Example: sending headers from Rails controller
304
+
305
+ ```ruby
306
+ class ReactController < ApplicationController
307
+ include Quilt::ReactRenderable
308
+
309
+ def index
310
+ render_react(headers: { 'x-custom-header': 'header-value-a' })
311
+ end
312
+ end
313
+ ```
314
+
315
+ You will need to serialize the result of the useRequestHeader hook in order for it to persist to the client
316
+
317
+ ```tsx
318
+ // app/ui/foundation/CustomUniversalProvider.tsx
319
+ import {createContext} from 'react';
320
+ import {createUniversalProvider} from '@shopify/react-universal-provider';
321
+
322
+ export const CustomContext = createContext<string | null>(null);
323
+ export const CustomUniversalProvider = createUniversalProvider('custom-key', CustomContext);
324
+ ```
325
+
326
+ ```tsx
327
+ // app/ui/index.tsx
328
+
329
+ import React from 'react';
330
+ import {useRequestHeader} from '@shopify/react-network';
331
+ import {CustomUniversalProvider} from './foundation/CustomUniversalProvider';
332
+ import {ComponentWithCustomHeader} from './components/ComponentWithCustomHeader';
333
+
334
+ function App() {
335
+ // get `x-custom-header` from the request that was sent through Rails ReactController
336
+ const customHeader = useRequestHeader('x-custom-header');
337
+
338
+ return (
339
+ <CustomUniversalProvider value={customHeader}>
340
+ <h1>My application ❤️</h1>
341
+ <ComponentWithCustomHeader />
342
+ </CustomUniversalProvider>
343
+ );
344
+ }
345
+
346
+ export default App;
347
+ ```
348
+
349
+ ```tsx
350
+ // app/ui/components/ComponentWithCustomHeader.tsx
351
+
352
+ import React, {useContext} from 'react';
353
+ import {CustomContext} from '../foundation/CustomUniversalProvider';
354
+
355
+ export function ComponentWithCustomHeader() {
356
+ // get `x-custom-header` from serialized context
357
+ // will be 'header-value-a' in this example
358
+ const customHeader = useContext(CustomContext);
359
+
360
+ return <span>{customHeader}</span>;
361
+ }
362
+ ```
363
+
303
364
  ##### Example: redirecting
304
365
 
305
366
  ```tsx
@@ -6,34 +6,38 @@ module Quilt
6
6
  module ReactRenderable
7
7
  include ReverseProxy::Controller
8
8
 
9
- def render_react
9
+ def render_react(headers: {})
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
15
+ call_proxy(headers)
16
16
  end
17
17
  end
18
18
 
19
19
  private
20
20
 
21
- def call_proxy
21
+ def call_proxy(headers)
22
22
  if defined? ShopifySecurityBase
23
23
  ShopifySecurityBase::HTTPHostRestriction.whitelist([Quilt.configuration.react_server_host]) do
24
- proxy
24
+ proxy(headers)
25
25
  end
26
26
  else
27
- proxy
27
+ proxy(headers)
28
28
  end
29
29
  end
30
30
 
31
- def proxy
31
+ def proxy(headers)
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
 
35
+ unless headers.blank?
36
+ Quilt::Logger.log("[ReactRenderable] applying custom headers #{headers.inspect}")
37
+ end
38
+
35
39
  begin
36
- reverse_proxy(url, headers: { 'X-CSRF-Token': form_authenticity_token }) do |callbacks|
40
+ reverse_proxy(url, headers: headers.merge('X-CSRF-Token': form_authenticity_token)) do |callbacks|
37
41
  callbacks.on_response do |status_code, _response|
38
42
  Quilt::Logger.log("[ReactRenderable] #{url} returned #{status_code}")
39
43
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Quilt
3
- VERSION = "1.9.2"
3
+ VERSION = "1.10.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.9.2
4
+ version: 1.10.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: 2019-12-10 00:00:00.000000000 Z
11
+ date: 2020-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties