All Projects → ctf0 → Blazar

ctf0 / Blazar

Licence: mit
Pre-Render Pages on the Fly in Laravel

Projects that are alternatives of or similar to Blazar

Laravel Multisite
Multiple sites on one codebase
Stars: ✭ 214 (+1428.57%)
Mutual labels:  middleware, laravel
Cors
🔮Supported(Laravel/Lumen/PSR-15/Swoft/Slim/ThinkPHP) - PHP CORS (Cross-origin resource sharing) middleware.
Stars: ✭ 266 (+1800%)
Mutual labels:  middleware, laravel
phantom-lord
Handy API for Headless Chromium
Stars: ✭ 24 (+71.43%)
Mutual labels:  phantomjs, puppeteer
Laravel Authorize
A middleware to check authorization
Stars: ✭ 179 (+1178.57%)
Mutual labels:  middleware, laravel
Puppetron
Puppeteer (Headless Chrome Node API)-based rendering solution.
Stars: ✭ 429 (+2964.29%)
Mutual labels:  puppeteer, prerender
Vue Gates
🔒 A Vue.js & Nuxt.js plugin that allows you to use roles and permissions in your components or DOM elements, also compatible as middleware and methods.
Stars: ✭ 184 (+1214.29%)
Mutual labels:  middleware, laravel
Laravel Demo Mode
A package to protect your work in progress from prying eyes
Stars: ✭ 259 (+1750%)
Mutual labels:  middleware, laravel
Has Parameters
A trait that allows you to pass arguments to Laravel middleware in a more PHP'ish way.
Stars: ✭ 149 (+964.29%)
Mutual labels:  middleware, laravel
Defender
Roles & Permissions for Laravel 8 / 7 / 6 / 5
Stars: ✭ 403 (+2778.57%)
Mutual labels:  middleware, laravel
Jwt Auth Guard
JWT Auth Guard for Laravel and Lumen Frameworks.
Stars: ✭ 319 (+2178.57%)
Mutual labels:  middleware, laravel
Laravel Http2 Server Push
A middleware package for Laravel to enable server push for your script, style, and image assets.
Stars: ✭ 174 (+1142.86%)
Mutual labels:  middleware, laravel
Laravel Caffeine
Keeping Your Laravel Forms Awake.
Stars: ✭ 723 (+5064.29%)
Mutual labels:  middleware, laravel
Laravel Rate Limited Job Middleware
A job middleware to rate limit jobs
Stars: ✭ 166 (+1085.71%)
Mutual labels:  middleware, laravel
Laravel Shield
A HTTP basic auth middleware for Laravel
Stars: ✭ 193 (+1278.57%)
Mutual labels:  middleware, laravel
Request Migrations
HTTP Request Migrations for API Versioning like Stripe
Stars: ✭ 149 (+964.29%)
Mutual labels:  middleware, laravel
pppr
pppr is a prerender service
Stars: ✭ 18 (+28.57%)
Mutual labels:  prerender, puppeteer
L5 Very Basic Auth
Stateless HTTP basic auth for Laravel without the need for a database.
Stars: ✭ 127 (+807.14%)
Mutual labels:  middleware, laravel
Laravel Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.
Stars: ✭ 136 (+871.43%)
Mutual labels:  middleware, laravel
Laravel Http2serverpush
A HTTP2 SeverPush Middleware for Laravel 5
Stars: ✭ 294 (+2000%)
Mutual labels:  middleware, laravel
Iris
The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | 谢谢 https://github.com/kataras/iris/issues/1329 |
Stars: ✭ 21,587 (+154092.86%)
Mutual labels:  middleware, laravel

This project is abandoned due to impracticality, i would highly recommend to try using ServiceWorkers and here is a little article to get you started.

Blazar

Latest Stable Version Total Downloads

Automate pre-rendering pages on the fly through utilizing puppeteer which runs in the background when needed without adding any overhead to the server nor to the user experience.


Installation

  • install puppeteer

  • composer require ctf0/blazar

  • (Laravel < 5.5) add the service provider to config/app.php

'providers' => [
    ctf0\Blazar\BlazarServiceProvider::class,
]
  • publish the package assets

php artisan vendor:publish --provider="ctf0\Blazar\BlazarServiceProvider"

  • add the middlewares to app/Http/Kernel.php
protected $middlewareGroups = [
    // ...
    \ctf0\Blazar\Middleware\Blazar::class,
];

protected $routeMiddleware = [
    // ...
    'dont-pre-render' => \ctf0\Blazar\Middleware\DontPreRender::class,
];
  • the package use caching through Redis to store the rendered results, so make sure to check the docs for installation & configuration.

Config

config/blazar.php

return [
    /*
     * puppeteer bin path
     */
    'puppeteer_path' => '',

    /*
     * puppeteer script path
     *
     * leave it empty to the use the one from the package
     */
    'script_path' => '',

    /*
     * prerender the page only if the url is being visited from a bot/crawler
     */
    'bots_only' => false,

    /*
     * log the url when its processed by puppeteer
     */
    'debug' => true,

    /**
     *  clear user cache on logout
     */
    'clear_user_cache' => true
];

Usage

  • we use Queues to pre-render the visited pages in the background for more than one reason

    • avoid latency when the page is being accessed for the first time.
    • don't keep the user waiting in case puppeteer took long to render the page or when something goes wrong.
    • after puppeteer has finished rendering, the page is cached to optimize server load even further.
    • make your website SEO friendly, because instead of serving the normal pages that usually produce issues for crawlers, we are now serving the pre-renderd version. ReadMore
    • even for websites with some traffic, we are still going to process each visited page without any problems.

# Render Pages Automatically

Atm in order to pre-render any page, it have to be visited first but if you want to make sure that all is working from day one, you can use the excellent package laravel-link-checker by Spatie

  • which by simply running php artisan link-checker:run you will

    • check which "url/link" on the website is not working.
    • pre-render all pages at once.

# Flushing The Cache

  • to clear the whole package cache, you can use
php artisan blazar:flush

or from within your app

Artisan::call('blazar:flush');

# Bots Only

we now use CrawlerDetect instead of relying on '?_escaped_fragment_'

if you decided to pre-render the pages for bots only, no need to the run the queue as the page will remain busy "stalled response" until rendered by puppeteer, which happens on the fly.

however because we are caching the result, so this will only happen once per page.

also note that we are saving the page cache equal to the url so even if you switched off the bots_only option, if the page is cached, we will always serve the cached result.


Notes

# Queues

the worker should only fires when a url is visited & if this url is not cached, however if you have an unfinished old process, the queue will start processing pages on its own, so to fix that, simply restart the queue server beanstalkd, redis, etc...

# ex. using Homebrew

brew services restart beanstalkd

# Auth

as i dont know how to make laravel think that a page visited through puppeteer is the same as the current logged in user.

so trying to pre-render pages with auth middleware will be cached as if the user was redirected to the home page or whatever you've set to redirectTo under Constollers/Auth/LoginController.php & Middleware/RedirectIfAuthenticated.php

so to solve that, simply add dont-pre-render middleware to those routes and everything will work as expected. also make sure to add the same middleware to any route that needs fresh csrf-token for each user "login, register, etc.." to avoid getting CSRF Token Mismatch for other users trying to use those pages.

# More Reading

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].