How it works | laravel-og-image-go | Spatie       

 SPATIE  

laravel-og-image-go
===================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-og-image-go](https://spatie.be/docs/laravel-og-image-go/v1)  Basic-usage  How it works

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-og-image-go/v1) 

  How it works    
- [ Introduction ](https://spatie.be/docs/laravel-og-image-go/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-og-image-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-og-image-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-og-image-go/v1/questions-issues)
- [ Testing ](https://spatie.be/docs/laravel-og-image-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-og-image-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-og-image-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-og-image-go/v1/support-us)
- [ About us ](https://spatie.be/docs/laravel-og-image-go/v1/about-us)

Basic usage
-----------

- [ How it works ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/how-it-works)
- [ Getting started ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/getting-started)
- [ Customizing screenshots ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/customizing-screenshots)
- [ Defining fallback images ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/defining-fallback-images)
- [ Caching and storage ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/managing-caching-and-storage)
- [ Pre-generating images ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/pre-generating-images)
- [ Clearing generated images ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/clearing-generated-images)

Advanced usage
--------------

- [ Customizing the page URL ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/customizing-the-page-url)
- [ Using a custom screenshot driver ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/using-a-custom-screenshot-driver)
- [ Customizing the screenshot layout ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/customizing-the-screenshot-layout)
- [ Customizing actions ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/customizing-actions)
- [ Troubleshooting ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/troubleshooting)
- [ Using a hosted solution ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/using-a-hosted-solution)
- [ Using Laravel Boost ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/using-laravel-boost)

 How it works
============

###  On this page 

1. [ Stage 1: Your page renders for the first time ](#content-stage-1-your-page-renders-for-the-first-time)
2. [ Stage 2: A crawler requests the image ](#content-stage-2-a-crawler-requests-the-image)
3. [ Stage 3: Subsequent requests ](#content-stage-3-subsequent-requests)
4. [ Performance ](#content-performance)

When you share a link on social media, platforms like Twitter, Facebook, and LinkedIn display a preview image. These are called Open Graph images. This package lets you define that image as HTML in your views and screenshots it to generate the image file.

Stage 1: Your page renders for the first time
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Your view renders a hidden `<template data-og-image>`. The web middleware hashes its exact inner HTML, stores the page URL in Laravel's cache, and adds `og:image`, `twitter:image`, and `twitter:card` tags to `<head>`.

The template is natively invisible. Image URLs point to `/og-image/{hash}.{format}`. No browser process starts while the page renders.

Stage 2: A crawler requests the image
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

When a crawler requests the image URL, the package checks the configured disk. If the image is missing, it looks up the original page URL in cache and acquires a lock for that page.

The browser visits the page with `?ogimage` appended. The web middleware replaces that response with a minimal document containing your original `<head>` and the OG template content. Chrome captures that document and the package saves the image.

For local disks, the application serves the file. For S3, Laravel Cloud, or custom disks configured as remote, it returns a 301 redirect to the disk's public URL. Both responses carry cache headers.

Stage 3: Subsequent requests
------------------------------------------------------------------------------------------------------------------------------------------

Subsequent requests read the existing file without taking another screenshot. Content-hashed URLs work with page caches and CDNs.

Performance
-----------------------------------------------------------------------------------------

The image endpoint has no web group middleware. Cache locks prevent simultaneous generation of the same page. Use a shared database or Redis cache across multiple processes. The in-memory cache coordinates only within one process and loses metadata on restart.

Only successful buffered HTML responses are processed. JSON, redirects, file responses, streams, and encoded responses are left alone. Use complete HTML documents with closing `</head>` and `</body>` tags.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
