Home

Awesome

Blaze - Accelerate Laravel with Rocket-Like Speed

Introduction

Blaze is a versatile package that revolutionizes the way you build websites in Laravel. By seamlessly combining dynamic and static content generation, Blaze empowers developers to create high-performance websites with ease. With Blaze, you can achieve an average response time of just 4 milliseconds, ensuring lightning-fast performance for your applications. This comprehensive README.md will guide you through the setup and usage of Blaze, enabling you to harness its full potential for building dynamic and static websites in Laravel.

Here's a more detailed diagram illustrating how Blaze interacts with Laravel and Nginx for dynamic and static content generation, along with cache invalidation:

              +-----------------+       +-----------------+       +-----------------+
              |    Laravel      |       |     Nginx       |       |     Blaze       |
              |   Application   |       |   Web Server    |       |   Integration   |
              +-----------------+       +-----------------+       +-----------------+
                      |                        |                      |
                      | HTTP Requests/Responses|                      |
                      | ---------------------->|                      |
                      |                        |                      |
                      |   Dynamic Routes       |                      |
                      | ---------------------> |                      |
                      |                        |                      |
                      |                        |                      |
                      |  Static Files Serving  |                      |
                      |  (Cache Hit)           |                      |
                      | <--------------------- |                      |
                      |                        |                      |
                      |   Cache Miss           |                      |
                      |  (Dynamic Content)     |                      |
                      | ---------------------> |                      |
                      |                        |                      |
                      |   Cache File Creation  |                      |
                      |                        |                      |
                      | <--------------------- |                      |
                      |                        |                      |
                      |                        | Cache File Storage   |
                      |                        | and Management       |
                      |                        | -------------------->|
                      |                        |                      |
                      |                        |                      |
                      | Cache File Serving     |                      |
                      | (Cache Hit)            |                      |
                      | <--------------------- |                      |
                      |                        |                      |
                      | Cache Invalidation     |                      |
                      | (Model Changes,        |                      |
                      |  Custom Events, etc.)  |                      |
                      | ---------------------> |                      |
                      |                        |                      |
                      |                        | Cache File Removal   |
                      |                        | -------------------->|
                      |                        |                      |
                      |                        |                      |
                      |                        |                      |
                      |                        |                      |
              +-----------------+       +-----------------+       +-----------------+
              |   PHP-FPM       |       |   File System   |       |   Cache Files   |
              |   Process       |       |   (Serving      |       |                 |
              |   Manager       |       |   Static Files) |       |                 |
              +-----------------+       +-----------------+       +-----------------+
  1. HTTP requests are initially received by Nginx.
  2. Nginx forwards dynamic requests to the Laravel application.
  3. Laravel processes dynamic routes and generates responses for dynamic content.
  4. Responses are served to clients directly or cached by Blaze as static files.
  5. For cache hits, Nginx serves static files directly, bypassing Laravel for improved performance.
  6. If a cache miss occurs, Blaze generates cache files for the dynamic content and stores them.
  7. Cached files are served by Nginx for subsequent requests until invalidated.
  8. Cache invalidation can occur due to model changes, custom events, or other triggers.
  9. Upon invalidation, Blaze removes outdated cache files to ensure fresh content delivery.
  10. The cycle continues, providing a seamless blend of dynamic and static content delivery for optimal performance.

Note: having nginx is not required but using the provided config can significantly improve response time.

Features

Getting Started

1. Installation

2. Configuration

class EncryptCookies extends Middleware
{
    protected $except = [
        'blaze_id',
    ];
}

You should also add @blaze directive before the body tag ends for handle csrf.

3. Adding Blaze to Routes

4. Config the model for invalidating and refreshing the cache

5. Nginx Config

  # Map to replace forward slashes with underscores
  map $uri $sanitized_uri {
    ~/(.*) _$1;
    default $uri;
  }

  # Define a variable based on the presence of the blaze_id cookie
  map $http_cookie $blaze_id {
    default "";
    "~*blaze_id=([^;]+)(?:;|$)" $1;
  }

After that change / location to:

location / {
    etag off;
    
    # 1. All method except GET must be proxied to PHP-FPM (post, put, patch, delete)
    if ($request_method !~ ^(GET)$ ) {
        rewrite ^ /index.php$query_string last;
    }

    # 2. detect blaze identifier using map
    # 3. Sanitize the URI for match cache file using map.
    rewrite ^/(.*)[/?&=](.*)$ /$1_$2;

    # 4. Set the paths for private and public cache files
    set $private_cache_file "/cache/${blaze_id}_${sanitized_uri}?${query_string}.html";
    set $public_cache_file "/cache/public_${sanitized_uri}?${query_string}.html";

    # 5. Try to serve the private cached file if it exists, then the public cached file, and finally index.php
    try_files $private_cache_file $public_cache_file /index.php?$query_string;
}

TODO