You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `:to` parameter is only required if you want to change the destination logical import name. If you drop the :to option, you must place the :under option directly after the first parameter.
85
88
89
+
The `integrity: true` option automatically calculates integrity hashes for all files in the directory, providing security benefits without manual hash management.
90
+
86
91
Allows you to:
87
92
88
93
```js
@@ -131,6 +136,137 @@ If you later wish to remove a downloaded pin:
131
136
Unpinning and removing "react"
132
137
```
133
138
139
+
## Subresource Integrity (SRI)
140
+
141
+
For enhanced security, importmap-rails automatically includes [Subresource Integrity (SRI)](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity) hashes by default when pinning packages. This ensures that JavaScript files loaded from CDNs haven't been tampered with.
142
+
143
+
### Default behavior with integrity
144
+
145
+
When you pin a package, integrity hashes are automatically included:
146
+
147
+
```bash
148
+
./bin/importmap pin lodash
149
+
Pinning "lodash" to vendor/javascript/lodash.js via download from https://ga.jspm.io/npm:[email protected]/lodash.js
150
+
Using integrity: sha384-PkIkha4kVPRlGtFantHjuv+Y9mRefUHpLFQbgOYUjzy247kvi16kLR7wWnsAmqZF
151
+
```
152
+
153
+
This generates a pin in your `config/importmap.rb` with the integrity hash:
If you need to disable integrity checking (not recommended for security reasons), you can use the `--no-integrity` flag:
162
+
163
+
```bash
164
+
./bin/importmap pin lodash --no-integrity
165
+
Pinning "lodash" to vendor/javascript/lodash.js via download from https://ga.jspm.io/npm:[email protected]/lodash.js
166
+
```
167
+
168
+
This generates a pin without integrity:
169
+
170
+
```ruby
171
+
pin "lodash"# @4.17.21
172
+
```
173
+
174
+
### Adding integrity to existing pins
175
+
176
+
If you have existing pins without integrity hashes, you can add them using the `integrity` command:
177
+
178
+
```bash
179
+
# Add integrity to specific packages
180
+
./bin/importmap integrity lodash react
181
+
182
+
# Add integrity to all pinned packages
183
+
./bin/importmap integrity
184
+
185
+
# Update your importmap.rb file with integrity hashes
186
+
./bin/importmap integrity --update
187
+
```
188
+
189
+
### Automatic integrity for local assets
190
+
191
+
For local assets served by the Rails asset pipeline (like those created with `pin` or `pin_all_from`), you can use `integrity: true` to automatically calculate integrity hashes from the compiled assets:
192
+
193
+
```ruby
194
+
# config/importmap.rb
195
+
196
+
# Automatically calculate integrity from asset pipeline
Modern browsers will automatically validate these integrity hashes when loading the JavaScript modules, ensuring the files haven't been modified.
257
+
258
+
### Redownloading packages with integrity
259
+
260
+
The `pristine` command also includes integrity by default:
261
+
262
+
```bash
263
+
# Redownload all packages with integrity (default)
264
+
./bin/importmap pristine
265
+
266
+
# Redownload packages without integrity
267
+
./bin/importmap pristine --no-integrity
268
+
```
269
+
134
270
## Preloading pinned modules
135
271
136
272
To avoid the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, importmap-rails uses [modulepreload links](https://developers.google.com/web/updates/2017/12/modulepreload) by default. If you don't want to preload a dependency, because you want to load it on-demand for efficiency, append `preload: false` to the pin.
@@ -217,7 +353,7 @@ Pin your js file:
217
353
pin "checkout", preload:false
218
354
```
219
355
220
-
Import your module on the specific page. Note: you'll likely want to use a `content_for` block on the specifc page/partial, then yield it in your layout.
356
+
Import your module on the specific page. Note: you'll likely want to use a `content_for` block on the specific page/partial, then yield it in your layout.
0 commit comments