Laravel Export

repository·main·Indexed 20 days ago

https://github.com/spatie/laravel-export

A package by Spatie that transforms a dynamic Laravel application into a static site bundle. It crawls application URLs, converts them to HTML, and bundles them with public assets for static hosting. Features include configurable export disks, manual path specification, file inclusion/exclusion patterns, before/after hooks for shell commands, and response streaming to reduce memory usage for large sites.

Tokens
2.3K
Snippets
13
Records
15
Agent score
72%

What's inside laravel-export

  1. Upgrade from 1.3 to 1.4

    main

    When upgrading to version 1.4, note the following breaking changes and new features:

    • PHP Requirement: The minimum supported PHP version is now 8.4.
    • Dependency Update: The package now uses spatie/crawler ^9.1.
    • API Changes: The LocalClient signature has changed. It is now an invokable class used as a Guzzle handler. If you extend or use LocalClient directly, you must update your implementation.
  2. Enable response streaming to reduce memory usage

    main

    Version 1.4 supports response streaming via spatie/crawler ^9.1. For large sites, enabling streaming can help reduce memory consumption during the export process. This feature is disabled by default to maintain backward compatibility.

    'use_streaming' => true,
  3. Configure the export disk

    main

    By default, Laravel Export saves the bundle to a dist folder in your application root. You can change this by specifying a different filesystem disk in config/export.php. To use a custom location or driver (like S3), first define the disk in config/filesystem.php and then reference it in config/export.php.

    // config/export.php
    
    return [
        'disk' => 'export',
    ];
    
    // config/filesystem.php
    
    return [
        'disks' => [
            'export' => [
                'driver' => 'local',
                'root' => base_path('out'),
            ],
        ],
    ];
  4. Configure crawling and streaming

    main

    Laravel Export can automatically crawl your site to find pages. You can control this behavior and optimize memory usage for large sites via the configuration file.

    return [
        // Enable or disable automatic crawling
        'crawl' => true,
    
        // Enable streaming to reduce memory usage when crawling large sites (only works if 'crawl' is true)
        'use_streaming' => true,
    ];
  5. Use before and after hooks

    main

    Hooks allow you to execute shell commands before or after the export process. This is useful for building assets (e.g., via Yarn) or deploying the bundle (e.g., via Netlify CLI).

    return [
        // Run before export
        'before' => [
            'assets' => '/usr/local/bin/yarn production',
        ],
    
        // Run after export
        'after' => [
            'deploy' => '/usr/local/bin/netlify deploy --prod',
        ],
    ];
  6. Include and exclude files in the export

    main

    Use include_files to specify files or folders (relative to the app root) to include in the bundle. Use exclude_file_patterns to exclude files based on regex patterns (e.g., excluding .php files or specific JSON files).

    return [
        // Include specific folders/files
        'include_files' => [
            'public' => '',
        ],
    
        // Exclude files matching these patterns
        'exclude_file_patterns' => [
            '/\.php$/',
            '/mix-manifest\.json$/',
        ],
    ];
  7. Configure the exporter via code

    main

    You can inject the Spatie\Export\Exporter class into a Service Provider to dynamically configure export settings at runtime.

    use Illuminate\Support\ServiceProvider;
    use Spatie\Export\Exporter;
    
    class AppServiceProvider extends ServiceProvider
    {
        public function boot(Exporter $exporter)
        {
            $exporter->crawl(false);
            $exporter->useStreaming(true);
    
            $exporter->paths(['', 'about', 'contact', 'posts']);
            $exporter->paths(Post::all()->pluck('slug'));
        }
    }
  8. Run the export command and skip hooks

    main

    To generate the static bundle, run the export Artisan command. You can skip specific hooks or all hooks using CLI flags.

    # Standard export
    php artisan export
    
    # Skip a specific hook (e.g., 'deploy' from the config)
    php artisan export --skip-deploy
    
    # Skip before, after, or all hooks
    php artisan export --skip-before
    php artisan export --skip-after
    php artisan export --skip-all
  9. Configure Exporter options

    main

    The Exporter class provides several methods to customize the export behavior:

    • cleanBeforeExport(bool $cleanBeforeExport): If set to true, the destination directory will be cleaned before the export starts.
    • crawl(bool $crawl): If set to true, the exporter will attempt to crawl the site to discover links.
    • useStreaming(bool $useStreaming): When used in conjunction with crawl(), determines if streaming should be used.
    • paths(...$paths): Specifies the relative paths to be exported. Accepts multiple arguments or a single array of strings.
    • urls(...$urls): Converts full URLs into relative paths for the exporter. It automatically strips the application's base URL.
    • includeFiles(array $includeFiles): Adds specific files to the export. The array should be a mapping of source => target (e.g., ['public/assets/logo.png' => 'assets/logo.png']).
    • excludeFilePatterns(array $excludeFilePatterns): Defines patterns of files to exclude from the export.