Skip to content

How to publish the Laravel maintenance mode view

Posted by author

Publish the Laravel maintenance mode view

Wondering how to customize the maintenance mode view in Laravel? It's simple, just publish the 503 error page!

1php artisan vendor:publish --tag=laravel-errors

Since all you need is the resources/views/errors/ 503.blade.php file, you can also just paste the following Blade (which is the default Laravel 11 view):

1@extends('errors::minimal')
2 
3@section('title', __('Service Unavailable'))
4@section('code', '503')
5@section('message', __('Service Unavailable'))

Pro tip: Poll to refresh

While Laravel has a feature to send refresh headers to refresh the page at an interval, this can be a bit janky as it forces a browser refresh.

A more elegant solution (assuming your users have JavaScript enabled) is to poll the server to check if the service is back up.

Here's a simple script you can add to the 503 error page to automatically reload the page when the service is back up:

1@extends('errors::minimal')
2 
3@section('title', __('Service Unavailable'))
4@section('code', '503')
5@section('message', __('Service Unavailable'))
6 
7<script>
8 // Automatically reload the page when the service is back up
9 (function() {
10 // Configuration
11 const checkInterval = 1000; // Time between checks in milliseconds, feel free to adjust
12 const checkUrl = '/up'; // URL to check (the Laravel 11 Health Check route)
13 
14 function checkService() {
15 fetch(checkUrl, { method: 'HEAD' })
16 .then(response => {
17 if (response.ok) {
18 // If the request was successful, refresh the page
19 window.location.reload();
20 } else {
21 // If the request failed, schedule another check
22 setTimeout(checkService, checkInterval);
23 }
24 })
25 .catch(error => {
26 // If there was a network error, schedule another check
27 setTimeout(checkService, checkInterval);
28 });
29 }
30 
31 // Start checking
32 setTimeout(checkService, checkInterval);
33 })();
34</script>

It uses the Fetch API to send a HEAD request to the /up which is added in all Laravel 11 projects by default. If the request is successful, it reloads the page. If it fails, it schedules another check.

Feel free to adjust the checkInterval to your liking. Lower values leads to less waiting time, but adds more server requests. So balance it out according to your needs.

Conclusion

If any part of this post was helpful, please let me know and give me a follow on Twitter/X where I'm @CodeWithCaen


Syntax highlighting by Torchlight.dev

End of article