This package integrates Varnish Cache with Bagisto to boost site performance by delivering cached pages quickly, while still supporting dynamic components through ESI (Edge Side Includes) or AJAX-based dynamic views.
- β‘ Full-page caching via Varnish
- π Automatic cache purging on product, category, or content updates
- π ESI support for dynamic blocks
- π± AJAX-based dynamic view loading for improved Core Web Vitals
- π Artisan commands for cache management
- π₯ Admin tools for purging cache & exporting VCL
- π¨ Theme-ready Blade components
- π‘ Middleware for cache headers (cacheable & non-cacheable routes)
Explanation:
- 443 (HTTPS) β Nginx handles SSL termination and forwards traffic.
- 80 (HTTP) β Varnish Proxy caches and serves pages, or passes the request to the backend.
- 8080 β Bagisto (Laravel app) generates fresh content when needed.
Flow Summary: Browser β Nginx β Varnish Proxy β Bagisto β Response (cached or fresh)
composer require bagisto/bagisto-varnishIn bootstrap/providers.php:
Note: Autoloading via Composerβs package auto-discovery is not possible for this provider. The registry order mattersβ
VarnishServiceProvidermust be listed after the Shop package or at the end of the providers array. Auto-discovery would load it too early, which can cause issues.
'providers' => [
Webkul\Varnish\Providers\VarnishServiceProvider::class,
],php artisan vendor:publish --provider="Webkul\Varnish\Providers\VarnishServiceProvider"-
Install Varnish 6.x on your server.
-
Replace
/etc/varnish/default.vclwith the provided file:Varnish/vcls/6.0.vcl -
Restart Varnish:
sudo systemctl restart varnish
You can integrate dynamic content in two ways:
In config/varnish.php, define a key (identifier) and its corresponding Blade view path:
return [
'esi' => [
'views' => [
...
'customer-desktop-dropdown' => 'varnish::shop.components.layouts.header.desktop.customer-dropdown',
...
]
],
];- Key β Used in ESI or AJAX call (
customer-desktop-dropdown) - Path β Full Blade view path to render (
varnish::...)
<esi:include src="/esi?tag=customer-desktop-dropdown" />- Injects content at the Varnish level (server-side).
- Appears immediately on page load.
- May affect LCP/FCP if the backend is slow.
<x-varnish::dynamic-view view="customer-desktop-dropdown" />- Loads via AJAX after user interaction.
- Improves LCP/FCP.
- Ideal for non-critical dropdowns, modals, and menus.
For routes that should NOT be cached by Varnish:
Cache-Control: no-cache, no-store, must-revalidate
For routes that should be cached:
Cache-Control: public, max-age=604800, s-maxage=604800
(Example: 7 days)
Weβve created a middleware Webkul\Varnish\Http\Middleware\VarnishCache to handle cache headers.
Attach it to routes like this:
Route::get('/', [HomeController::class, 'index'])
->name('shop.home.index')
->middleware('cache.response');Navigate to: Admin β Configuration β Full Page Cache β Configuration
Select Varnish as the cache application, then provide the following:
- Access List β IPs allowed to purge the cache (e.g.,
localhost). - Varnish Host URL β Varnish server IP and port for purging/banning cache via UI.
- Backend Host URL β Laravel Bagisto server IP used in the exported VCL.
- Backend Host Port β Laravel Bagisto server port used in the exported VCL.
- Grace Period β Duration for serving stale content if the backend is slow or unavailable.
Navigate to: Admin β Configuration β Full Page Cache β Cache Management
- Purge by URLs β Enter full URLs (comma-separated) to clear specific cache entries. Paths and domains must match exactly.
- Purge Everything β Clears all cache entries from Varnish. Use with caution, as it may temporarily affect performance.
The package automatically purges cache when:
- Products, categories, pages, orders, reviews, refunds, or theme changes occur.
You can also manually trigger purging by adding your own events in EventServiceProvider and calling:
VarnishCache::forget($urls);- Purge Full Cache
- Purge by URL
- Export VCL
- Use ESI for small, critical personalized blocks (e.g., login status, cart count).
- Use AJAX dynamic views for non-critical interactive elements to improve Core Web Vitals.
- Set Cache-Control headers with appropriate TTL values to control caching behavior.
- Always test on a staging environment before deploying to production.
- Monitor LCP, FCP, and TTFB after enabling Varnish.