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
That's all you need to do. **Basset will download the file to `storage/app/public/bassets` from wherever it is, then output the now-public path to your asset.**
19
+
That's all you need to do. **Basset will download the file to the predefined disk, then output that disk path to your asset.**
20
20
21
21
Using Basset, you easily internalize and use:
22
22
- files from external URLs (like CDNs)
23
23
- files from internal, but non-public URLs (like the vendor directory)
24
24
- entire archives from external URLs (like GitHub)
25
25
- entire directories from local, non-public paths (like other local projects)
26
26
27
-
No more publishing package files. No more using NPM just to download some files. It's a simple yet effective solution in the age of `HTTP/2` and `HTTP/3`.
27
+
No more publishing package files. No more NPM bloat, just to download some files. It's a simple yet effective solution in the age of `HTTP/2` and `HTTP/3`.
> Basset is disabled by default on local environment. If you want to change it, please set `BASSET_DEV_MODE=false` in your env file.
43
-
44
50
#### Storage Symlink
45
-
Basset uses the `public` disk to store cached assets in a directory that is publicly-accessible. So it needs you to run `php artisan storage:link` to create the symlink. The installation command will create ask to run that, and to add that command to your `composer.json`. That will most likely make it work on your development/staging/production servers. If that's not the case, make sure you create the links manually wherever you need them, with the command `php artisan storage:link`.
51
+
The default disk remains `basset`, which lives under `storage/app/public`. Run `php artisan storage:link` so the cached assets are reachable from `public/`. The installer keeps the commandin your composer scripts.
52
+
53
+
If you prefer to keep cached files under `public/` instead of `storage/`, switch to the `public_basset` disk (`BASSET_DISK=public_basset`) and commit or ignore `public/basset` according to your workflow.
|`public_basset`|`public/basset`| Assets are ready to serve after deploy; can be versioned | Git noise unless ignored;|
46
61
47
-
#### Disk
48
-
By default Basset uses the `public` disk. If you're having trouble with the assets not showing up on page, you might have an old Laravel configuration for it. Please make sure your disk is properly setup on `config/filsystems.php` - it should look like [the default one](https://github.com/laravel/laravel/blob/10.x/config/filesystems.php#L39-L45).
62
+
If you move to `public_basset`, re-run `php artisan basset:install --git-ignore` (or edit the file yourself) when you want the installer to append `public/basset` to `.gitignore`.
49
63
50
64
## Usage
51
65
52
66
### The `basset()` Helper
53
67
54
-
You can just use the `basset()` helper instead of Laravel's `asset()` helper, and point to CDNs and non-public files too. Use [Laravel's path helpers](https://laravel.com/docs/10.x/helpers#paths-method-list) to construct the absolute path to your file, then Basset will take care of the rest.
68
+
You can just use the `basset()` helper instead of Laravel's `asset()` helper, and point to CDNs and non-public files too. Use [Laravel's path helpers](https://laravel.com/docs/12.x/helpers#paths-method-list) to construct the absolute path to your file, then Basset will take care of the rest.
In Blade you can now call `@basset('crud.select2')` or `basset('crud.select2')` and Basset will internalize the mapped source.
185
+
186
+
Need a different CDN or integrity hash? Create a class that implements `Backpack\Basset\OverridesAssets`, point `config('backpack.basset.asset_overrides')` to it, and call `Basset::map()` again inside `assets()`.
187
+
188
+
Use `php artisan basset:list-named` anytime to check which keys are currently registered.
189
+
156
190
### The `basset` Commands
157
191
158
192
Copying an asset from CDNs to your server could take a bit of time, depending on the asset size. For large pages, that could even take entire seconds. You can easily prevent that from happening, by internalizing all assets in one go. You can use `php artisan basset:cache` to go through all your blade files, and internalize everything that's possible. If you ever need it, `basset:clear` will delete all the files.
159
193
160
194
```bash
161
-
php artisan basset:cache # internalizes all @bassets
195
+
php artisan basset:cache # internalizes all @bassets and named assets
162
196
php artisan basset:clear # clears the basset directory
php artisan basset:list-named # shows all registered named assets
163
199
```
164
200
165
201
In order to speed up the first page load on production, we recommend you to add `php artisan basset:cache` command to your deploy script.
166
202
167
-
### Basset Cached Event
168
-
169
-
If you require customized behavior after each asset is cached, you can set up a listener for the `BassetCachedEvent` in your `EventServiceProvider`. This event will be triggered each time an asset is cached.
170
-
171
203
## Configuration
172
204
173
205
Take a look at [the config file](https://github.com/Laravel-Backpack/basset/blob/main/src/config/backpack/basset.php) for all configuration options. Notice some of those configs also have ENV variables, so you can:
174
-
- enable/disable dev mode using `BASSET_DEV_MODE=false` - this will force Basset to internalize assets even on localhost
206
+
- enable/disable dev mode using `BASSET_DEV_MODE=false` - when enabled Basset will check for changes in your url/files and update the cached assets
175
207
- change the disk where assets get internalized using `BASSET_DISK=yourdiskname`
176
208
- disable the cache map using `BASSET_CACHE_MAP=false` (needed on serverless like Laravel Vapor)
177
209
210
+
### Dev Mode
211
+
212
+
When `BASSET_DEV_MODE` is `true`, Basset keeps hashing local files and code blocks so edits are picked up immediately. Remote URLs are still downloaded the first time they are hit; this avoids surprises when you lose connectivity.
213
+
178
214
## Deployment
179
215
180
216
There are a lot of deployment options for Laravel apps, but we'll try to cover the gotchas of the most popular ones here:
@@ -271,6 +307,42 @@ If you use the default `public` disk, Basset requires that the symlink between t
271
307
272
308
Note for Homestead users: the symlink can't be created inside the virtual machine. You should stop your instance with: `vagrant down`, create the symlink in your local application folder and then `vagrant up` to bring the system back up.
273
309
310
+
#### Where are cached assets stored?
311
+
312
+
Basset provides a few options out-of-the-box:
313
+
-**`basset` disk** - inside the storage directory (eg. `storage/app/basset`) [DEFAULT]
314
+
- PROs: the Git history is clean - because your cached assets will NOT be tracked by Git;
315
+
- CONs: you have to run `php artisan basset:cache` in your deploy script, which adds seconds to your deploy time; plus, it opens up a corner case on deployment - because assets are being re-cached upon deployment, if a CDN is down during deployment, the system will not be able to internalize it; if will however internalize it when the CDN is back on, and the page that loads the file gets accessed; we consider the tradeoffs minor and unlikely, which is why this is the DEFAULT;
316
+
- How to enable: do nothing, or do `BASSET_DISK=basset` in your .env file;
317
+
-**`public_basset` disk** - inside the public directory (eg. `public/basset`)
318
+
- PROs: you are certain the same assets you have on localhost will be in production, because the assets are commited to Git;
319
+
- CONs: your Git history will be dirtier, because it will contain changes to libraries of CSS/JS files;
320
+
- How to enable: do `BASSET_DISK=public_basset` in your .env file or `config/backpack/basset.php` config file;
321
+
-**custom** - you can completely customize what disk is used to store the assets - just change it in the config file; most common customizations:
322
+
- store assets on a S3 bucket (using a custom disk);
323
+
- store assets in `public_basset` disk, but add `public/basset` to .gitignore;
324
+
325
+
#### Can I track the assets in git, just like my source code? (aka NOT gitignore CSS and JS assets)
326
+
327
+
Yes, you can track the assets in your application repository and avoid downloading them on each deployment (aka. have them in git). The easiest way to do that is to set the `BASSET_DISK=public_basset` in your .ENV, or in the basset config file. This will store the assets in the `public/basset` directory by default and they will now be committed to git alongside the rest of your application code. But note that this has both PROs and CONs:
328
+
- PROs: This is advantageous as you know what assets are in your application right when you deploy, avoiding issues like a CDN being down at the deployment time and breaking your application production.
329
+
- CONs: As a downside, you must be 100% sure all assets are internalized on localhost, and commited to git. Otherwise, when a page is accessed in production, Basset will internalize that file in production alone (it always prioritizes having production in a working state), which means you'll have uncommitted changes in your production code. You will then have to fix merge conflicts in production, or do a git reset before each deployment.
330
+
331
+
To summarize - if you're 100% sure that `php artisan basset:cache` is pulling all assets your application needs, you can safely commit your assets to git. If not, you are exposing yourself to conflicts in production (which can be managed as well).
332
+
333
+
### Events
334
+
335
+
If you require customized behavior after each asset is cached, you can set up a listener for the `BassetCachedEvent` in your `EventServiceProvider`. This event will be triggered each time an asset is cached.
336
+
337
+
## Upgrading from v1 to v2
338
+
339
+
340
+
To upgrade Basset:
341
+
-**Step 1.** Run `php artisan basset:install --git-ignore` to refresh composer hooks and optionally ignore the public cache folder.
342
+
-**Step 2.** Decide on `BASSET_DISK` (`basset` + storage symlink, or `public_basset` + git rule) before deploying.
343
+
-**Step 3.** Replace any custom `@loadOnce` usage with `@basset` or `@bassetBlock`; the old directive now proxies the new ones.
344
+
-**Step 4.** Ensure deploy scripts warm the cache (`basset:cache` or `basset:fresh`) so your public folder is ready when the app boots.
345
+
274
346
## Change log
275
347
276
348
Please see the [releases tab](https://github.com/Laravel-Backpack/basset/releases) for more information on what has changed recently.
@@ -292,6 +364,7 @@ If you discover any security related issues, please email hello@backpackforlarav
0 commit comments