Laraship Full Page Caching

Laraship Full Page Caching

Introduction

When Serving static URL in Laraship, like Pages, Blogs,.. and even some routes that are user-independent, it’s possible to implement full page caching where the rendered page is stored in the server as HTML and served instead of rendering the PHP code every time the page requested.

That said, for truly static pages on a site, there really is no reason to have to boot up a full Laravel core and application just to serve a static page. Serving a simple HTML page from disk is infinitely faster and less taxing on the server.

So the solution is to implement Full-page caching for instantly loading such pages.

We will demonstrate how to cache public requests for Laraship such as pages, blogs, posts and any selective route you need.

Installation

Install the page-cache package with composer:

Middleware

URL rewriting

In order to serve the static files directly once they’ve been cached, you need to properly configure your webserver to check for those static files.

  • For nginx:Update your location block’s try_files directive to include a check in the page-cache directory:
  • For apache:Open public/.htaccess and add the following before the block labeled Handle Front Controller:

Ignoring the cached files

To make sure you don’t commit your locally cached files to your git repository, add this line to your .gitignore file:

Usage

Using the PublicBaseController

Since public controllers in Laraship are extending the Corals/core/Foundation/Http/Controllers/PublicBaseController.php

add page-cache middleware to list of middlewares

 

Using the middleware

if you want to apply page cache to a specific route, you need to add the page-cache middleware to that route definition at web.php

To cache the response of a given request, use the page-cache middleware:

Every post will now be cached to a file under the public/page-cache directory, closely matching the URL structure of the request. All subsequent requests for this post will be served directly from disk, never even hitting your app!

Clearing the cache

Since the responses are cached to disk as static files, any updates to those pages in your app will not be reflected on your site. To update pages on your site, you should clear the cache with the following command:

You may optionally pass a URL slug to the command, to only delete the cache for a specific page:

Also if you like to be able to clear the cache from Cache management console in admin, edit the file Corals/core/Settings/config/settings.php

and the following code to the end of the array

No Comments

Post a Comment

Comment
Name
Email
Website

UP TO 60% OFF

LIMITED TIME OFFER
SHOP NOW
Offer Expires Nov 30
close-link