Search code examples
phplaravel-artisanlaravel-9

Laravel maintenance mode refresh and render parameter


I am trying to put my application into maintenance mode via php artisan down.

php artisan down --refresh=5 works fine and my browser refreshes after 5 seconds

If I want to render a custom maintenance view with php artisan down --render="maintenance.index" it renders the view located in resources/views/maintenance/index.blade.php like I want it to do.

But when I am using both parameters like this

php artisan down --render="maintenance.index" --refresh=5

It just renders the custom view but is not refreshing after 5 seconds. I thought maybe it matters in which order I type in the parameters. But it doesn't work either way.

php artisan down --refresh=5 --render="maintenance.index"

Is there a possibility to put my app into maintenance mode, rendering a custom view and refreshing the page every 5 seconds without editing the default 503.blade.php file?


Solution

  • If you need the 5 seconds at anytime for your custom maintenance view, simply add the refresh html tag

    <head>
        <title>{{ config('app.name') }}</title>
        <meta charset="utf-8">
        <meta http-equiv="refresh" content="5">
    </head>
    

    On top of my head, I'm not sure what the refresh attribute changes in laravel to apply this dynamically in your template, but I'll investigate this further a little bit later today.

    Edit 1:

    Laravel stores stub file for the maintenance mode but I could not find a result which affects the refresh argument in combination with render.

    However I found a workaround to archive the same. The refresh attribute is in the $_SERVER['argv'] environment variable, which can easily parsed and added to your blade view:

    @php
        $result = array_values(preg_grep('/(--refresh)/', array_values( $_SERVER['argv'] ) ));
        if(!empty($result)){
          $refresh = substr($result[0], strpos($result[0], "=") + 1);
        }
    @endphp
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>{{ config('app.name') }}</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf-token" content="{{ csrf_token() }}">
        @if(isset($refresh))
            <meta http-equiv="refresh" content="{{$refresh}}">
        @endif
        <!-- Fonts -->
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">
        <!-- Styles -->
        <link rel="stylesheet" href="{{ asset('css/app.css') }}">
    
    <!-- Scripts -->
        <script src="{{ asset('js/app.js') }}" defer></script>
    </head>
    <body>
    <!-- your content here... -->
    
    
    </body>
    </html>
    

    Edit 2:

    This issue is now fixed in Laravel PR [8.x] Fix refresh during down #42217