Skip to content

Commit 126ec64

Browse files
committed
Load castle.umd.js by default from /vendor/castle-js.
1 parent bcbf73e commit 126ec64

4 files changed

Lines changed: 27 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## [Unreleased][main]
44

5-
- Drop `cdn.castle.io`; configure the browser SDK from a host-imported module or a hosted UMD via `castle_javascript_tag(src:)`
5+
- Drop `cdn.castle.io`; load `castle.umd.js` from `/vendor/castle-js` via `castle_javascript_tag`
66
- Seed `window.Castle` before a UMD script so 3.x (`name: @castleio/castle-js`) attaches to the same global
77
- Support `@castleio/castle-js` 2.x (`injectTokenOnSubmit` / `createRequestToken`) and 3.x (`configure({ pk })` + `createRequestToken`)
88
- Add `publishable_key` configuration and drop `app_id`

README.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,18 @@ en:
6262
6363
#### Browser SDK
6464
65-
Install [`@castleio/castle-js`](https://docs.castle.io/docs/sdk-browser) (2.x or 3.x). Host it the same way you host the rest of your JavaScript.
66-
67-
**Module import.** Configure the SDK in your pack and keep the instance on `window.__castleDevise`:
68-
69-
```javascript
70-
import * as Castle from '@castleio/castle-js'
71-
72-
window.__castleDevise = Castle.configure({ pk: YOUR_PUBLISHABLE_KEY })
73-
```
65+
Install [`@castleio/castle-js`](https://docs.castle.io/docs/sdk-browser) and serve the npm `dist` directory at `/vendor/castle-js` (keep `castle.umd.js` and the worker files together):
7466

7567
```ruby
7668
<%= castle_javascript_tag %>
7769
```
7870

79-
**Script tag** (UMD). Publish the npm `dist` directory with your static assets (for example `public/castle-js`, a CDN, or nginx). Keep `castle.umd.js` and the worker files in the same directory, then pass that URL:
71+
That loads `/vendor/castle-js/castle.umd.js` and seeds `window.Castle` before the script runs (3.x UMD is named `@castleio/castle-js`). Pass `src:` for a different UMD URL:
8072

8173
```ruby
82-
<%= castle_javascript_tag(src: "/castle-js/castle.umd.js") %>
74+
<%= castle_javascript_tag(src: "/assets/castle.umd.js") %>
8375
```
8476

85-
`src` seeds `window.Castle` before the script runs (3.x UMD is named `@castleio/castle-js`).
86-
8777
Add the following tag to the the `<form>` tag in both `devise/registrations/new.html.erb` and `devise/sessions/new.html.erb` (if you haven't generated them yet, run `rails generate devise:views`):
8878

8979
```ruby

lib/castle_devise/helpers/castle_helper.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ module CastleHelper
1414
}
1515
JS
1616

17-
# Configures the Castle browser SDK with { pk: }. Pass src: to also load a
18-
# hosted UMD build; omit it when the host app already imported the npm module.
17+
DEFAULT_UMD_SRC = "/vendor/castle-js/castle.umd.js"
18+
19+
# Loads castle.umd.js and configures it with { pk: }. Pass src: to use a
20+
# different UMD URL, or src: nil when the host app already loaded the SDK.
1921
#
2022
# @param src [String, nil] URL or path of castle.umd.js (keep workers in the same directory)
2123
# @return [String]
@@ -29,7 +31,7 @@ module CastleHelper
2931
# <title>Your app title</title>
3032
#
3133
# <!-- the rest of your layout -->
32-
def castle_javascript_tag(src: nil)
34+
def castle_javascript_tag(src: DEFAULT_UMD_SRC)
3335
parts = []
3436
if src
3537
parts << javascript_tag(UMD_SHIM)

spec/castle_devise/helpers/castle_helper_spec.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,35 @@
1010
CastleDevise.configuration.publishable_key = "pk_spec"
1111
end
1212

13-
it "configures the SDK without loading a script when src is omitted" do
13+
it "loads castle.umd.js from /vendor/castle-js" do
1414
html = helper.castle_javascript_tag
1515

16+
expect(html).to include("/vendor/castle-js/castle.umd.js")
17+
expect(html).to include("window.module")
18+
expect(html).to include("window.Castle")
19+
expect(html).to include("@castleio/castle-js")
1620
expect(html).to include(".configure")
17-
expect(html).to include("pk")
1821
expect(html).to include("pk_test")
1922
expect(html).to include("castleDeviseOnFormSubmit")
20-
expect(html).not_to include("castle.umd.js")
2123
expect(html).not_to include("cdn.castle.io")
24+
expect(html).not_to include("castle.browser.js")
2225
end
2326

24-
it "loads a hosted UMD build when src is given" do
25-
html = helper.castle_javascript_tag(src: "/castle-js/castle.umd.js")
27+
it "loads a custom UMD src when given" do
28+
html = helper.castle_javascript_tag(src: "/assets/castle.umd.js")
2629

27-
expect(html).to include("/castle-js/castle.umd.js")
30+
expect(html).to include("/assets/castle.umd.js")
31+
expect(html).not_to include("/vendor/castle-js/castle.umd.js")
2832
expect(html).to include("window.module")
29-
expect(html).to include("window.Castle")
30-
expect(html).to include("@castleio/castle-js")
33+
expect(html).not_to include("cdn.castle.io")
34+
end
35+
36+
it "skips the script tag when src is nil" do
37+
html = helper.castle_javascript_tag(src: nil)
38+
39+
expect(html).to include(".configure")
3140
expect(html).to include("pk_test")
41+
expect(html).not_to include("castle.umd.js")
3242
expect(html).not_to include("cdn.castle.io")
3343
end
3444
end

0 commit comments

Comments
 (0)